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