1 | 'use strict'
|
2 |
|
3 | const fastClone = require('rfdc')({ circles: false, proto: true })
|
4 |
|
5 | const {
|
6 | codes: {
|
7 | FST_ERR_SCH_MISSING_ID,
|
8 | FST_ERR_SCH_ALREADY_PRESENT,
|
9 | FST_ERR_SCH_NOT_PRESENT
|
10 | }
|
11 | } = require('./errors')
|
12 |
|
13 | const URI_NAME_FRAGMENT = /^#[A-Za-z]{1}[\w-:.]{0,}$/
|
14 |
|
15 | function Schemas () {
|
16 | this.store = {}
|
17 | }
|
18 |
|
19 | Schemas.prototype.add = function (inputSchema) {
|
20 | const schema = fastClone(inputSchema)
|
21 | const id = schema['$id']
|
22 | if (id === undefined) {
|
23 | throw new FST_ERR_SCH_MISSING_ID()
|
24 | }
|
25 |
|
26 | if (this.store[id] !== undefined) {
|
27 | throw new FST_ERR_SCH_ALREADY_PRESENT(id)
|
28 | }
|
29 |
|
30 | this.store[id] = this.resolveRefs(schema, true)
|
31 | }
|
32 |
|
33 | Schemas.prototype.resolve = function (id) {
|
34 | if (this.store[id] === undefined) {
|
35 | throw new FST_ERR_SCH_NOT_PRESENT(id)
|
36 | }
|
37 | return this.store[id]
|
38 | }
|
39 |
|
40 | Schemas.prototype.resolveRefs = function (routeSchemas, dontClearId) {
|
41 |
|
42 | for (const key of ['headers', 'querystring', 'params', 'body']) {
|
43 | if (typeof routeSchemas[key] === 'object' && Object.getPrototypeOf(routeSchemas[key]) !== Object.prototype) {
|
44 | return routeSchemas
|
45 | }
|
46 | }
|
47 |
|
48 | try {
|
49 |
|
50 |
|
51 | this.traverse(routeSchemas)
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | if (dontClearId !== true) {
|
58 | this.cleanId(routeSchemas)
|
59 | }
|
60 | } catch (err) {
|
61 |
|
62 |
|
63 | if (/FST_ERR_SCH_*/.test(err.code)) throw err
|
64 | }
|
65 |
|
66 | if (routeSchemas.headers) {
|
67 | routeSchemas.headers = this.getSchemaAnyway(routeSchemas.headers)
|
68 | }
|
69 |
|
70 | if (routeSchemas.querystring) {
|
71 | routeSchemas.querystring = this.getSchemaAnyway(routeSchemas.querystring)
|
72 | }
|
73 |
|
74 | if (routeSchemas.params) {
|
75 | routeSchemas.params = this.getSchemaAnyway(routeSchemas.params)
|
76 | }
|
77 |
|
78 | return routeSchemas
|
79 | }
|
80 |
|
81 | Schemas.prototype.traverse = function (schema) {
|
82 | for (var key in schema) {
|
83 |
|
84 | if (typeof schema[key] === 'string' && key !== '$schema' && key !== '$ref' && schema[key].slice(-1) === '#') {
|
85 | schema[key] = this.resolve(schema[key].slice(0, -1))
|
86 | }
|
87 |
|
88 | if (schema[key] !== null && typeof schema[key] === 'object') {
|
89 | this.traverse(schema[key])
|
90 | }
|
91 | }
|
92 | }
|
93 |
|
94 | Schemas.prototype.cleanId = function (schema) {
|
95 | for (var key in schema) {
|
96 | if (key === '$id' && !URI_NAME_FRAGMENT.test(schema[key])) {
|
97 | delete schema[key]
|
98 | }
|
99 | if (schema[key] !== null && typeof schema[key] === 'object') {
|
100 | this.cleanId(schema[key])
|
101 | }
|
102 | }
|
103 | }
|
104 |
|
105 | Schemas.prototype.getSchemaAnyway = function (schema) {
|
106 | if (schema.oneOf || schema.allOf || schema.anyOf) return schema
|
107 | if (!schema.type || !schema.properties) {
|
108 | return {
|
109 | type: 'object',
|
110 | properties: schema
|
111 | }
|
112 | }
|
113 | return schema
|
114 | }
|
115 |
|
116 | Schemas.prototype.getSchemas = function () {
|
117 | return Object.assign({}, this.store)
|
118 | }
|
119 |
|
120 | Schemas.prototype.getJsonSchemas = function (options) {
|
121 | const store = this.getSchemas()
|
122 | const schemasArray = Object.keys(store).map(schemaKey => {
|
123 |
|
124 | if (store[schemaKey]['$id'] === undefined) {
|
125 | store[schemaKey]['$id'] = schemaKey
|
126 | }
|
127 | return store[schemaKey]
|
128 | })
|
129 |
|
130 | if (options && options.onlyAbsoluteUri === true) {
|
131 |
|
132 | return schemasArray.filter(_ => !/^\w*$/g.test(_.$id))
|
133 | }
|
134 | return schemasArray
|
135 | }
|
136 |
|
137 | function buildSchemas (s) {
|
138 | const schema = new Schemas()
|
139 | s.getJsonSchemas().forEach(_ => schema.add(_))
|
140 | return schema
|
141 | }
|
142 |
|
143 | module.exports = { Schemas, buildSchemas }
|