UNPKG

5.71 kBJavaScriptView Raw
1'use strict'
2
3const fastJsonStringify = require('fast-json-stringify')
4const Ajv = require('ajv')
5
6const bodySchema = Symbol('body-schema')
7const querystringSchema = Symbol('querystring-schema')
8const paramsSchema = Symbol('params-schema')
9const responseSchema = Symbol('response-schema')
10const headersSchema = Symbol('headers-schema')
11const kFluentSchema = Symbol.for('fluent-schema-object')
12
13function getValidatorForStatusCodeSchema (statusCodeDefinition, externalSchema) {
14 return fastJsonStringify(statusCodeDefinition, { schema: externalSchema })
15}
16
17function getResponseSchema (responseSchemaDefinition, sharedSchemas) {
18 var statusCodes = Object.keys(responseSchemaDefinition)
19 return statusCodes.reduce(function (r, statusCode) {
20 r[statusCode] = getValidatorForStatusCodeSchema(responseSchemaDefinition[statusCode], sharedSchemas)
21 return r
22 }, {})
23}
24
25function build (context, compile, schemas) {
26 if (!context.schema) {
27 return
28 }
29
30 generateFluentSchema(context.schema)
31 context.schema = schemas.resolveRefs(context.schema)
32
33 const headers = context.schema.headers
34
35 if (headers && Object.getPrototypeOf(headers) !== Object.prototype) {
36 // do not mess with non-literals, e.g. Joi schemas
37 context[headersSchema] = compile(headers)
38 } else if (headers) {
39 // The header keys are case insensitive
40 // https://tools.ietf.org/html/rfc2616#section-4.2
41 const headersSchemaLowerCase = {}
42 Object.keys(headers).forEach(k => { headersSchemaLowerCase[k] = headers[k] })
43 if (headersSchemaLowerCase.required instanceof Array) {
44 headersSchemaLowerCase.required = headersSchemaLowerCase.required.map(h => h.toLowerCase())
45 }
46 if (headers.properties) {
47 headersSchemaLowerCase.properties = {}
48 Object.keys(headers.properties).forEach(k => {
49 headersSchemaLowerCase.properties[k.toLowerCase()] = headers.properties[k]
50 })
51 }
52 context[headersSchema] = compile(headersSchemaLowerCase)
53 }
54
55 if (context.schema.response) {
56 context[responseSchema] = getResponseSchema(context.schema.response, schemas.getSchemas())
57 }
58
59 if (context.schema.body) {
60 context[bodySchema] = compile(context.schema.body)
61 }
62
63 if (context.schema.querystring) {
64 context[querystringSchema] = compile(context.schema.querystring)
65 }
66
67 if (context.schema.params) {
68 context[paramsSchema] = compile(context.schema.params)
69 }
70}
71
72function generateFluentSchema (schema) {
73 ;['params', 'body', 'querystring', 'query', 'headers'].forEach(key => {
74 if (schema[key] && (schema[key].isFluentSchema || schema[key][kFluentSchema])) {
75 schema[key] = schema[key].valueOf()
76 }
77 })
78
79 if (schema.response) {
80 Object.keys(schema.response).forEach(code => {
81 if (schema.response[code].isFluentSchema || schema.response[code][kFluentSchema]) {
82 schema.response[code] = schema.response[code].valueOf()
83 }
84 })
85 }
86}
87
88function validateParam (validatorFunction, request, paramName) {
89 var ret = validatorFunction && validatorFunction(request[paramName])
90 if (ret === false) return validatorFunction.errors
91 if (ret && ret.error) return ret.error
92 if (ret && ret.value) request[paramName] = ret.value
93 return false
94}
95
96function validate (context, request) {
97 var params = validateParam(context[paramsSchema], request, 'params')
98 if (params) {
99 return wrapValidationError(params, 'params')
100 }
101 var body = validateParam(context[bodySchema], request, 'body')
102 if (body) {
103 return wrapValidationError(body, 'body')
104 }
105 var query = validateParam(context[querystringSchema], request, 'query')
106 if (query) {
107 return wrapValidationError(query, 'querystring')
108 }
109 var headers = validateParam(context[headersSchema], request, 'headers')
110 if (headers) {
111 return wrapValidationError(headers, 'headers')
112 }
113 return null
114}
115
116function wrapValidationError (result, dataVar) {
117 if (result instanceof Error) {
118 return result
119 }
120 var error = new Error(schemaErrorsText(result, dataVar))
121 error.validation = result
122 return error
123}
124
125function serialize (context, data, statusCode) {
126 var responseSchemaDef = context[responseSchema]
127 if (!responseSchemaDef) {
128 return JSON.stringify(data)
129 }
130 if (responseSchemaDef[statusCode]) {
131 return responseSchemaDef[statusCode](data)
132 }
133 var fallbackStatusCode = (statusCode + '')[0] + 'xx'
134 if (responseSchemaDef[fallbackStatusCode]) {
135 return responseSchemaDef[fallbackStatusCode](data)
136 }
137 return JSON.stringify(data)
138}
139
140function isValidLogger (logger) {
141 if (!logger) {
142 return false
143 }
144
145 var result = true
146 const methods = ['info', 'error', 'debug', 'fatal', 'warn', 'trace', 'child']
147 for (var i = 0; i < methods.length; i += 1) {
148 if (!logger[methods[i]] || typeof logger[methods[i]] !== 'function') {
149 result = false
150 break
151 }
152 }
153 return result
154}
155
156function schemaErrorsText (errors, dataVar) {
157 var text = ''
158 var separator = ', '
159 for (var i = 0; i < errors.length; i++) {
160 var e = errors[i]
161 text += dataVar + (e.dataPath || '') + ' ' + e.message + separator
162 }
163 return text.slice(0, -separator.length)
164}
165
166function buildSchemaCompiler (externalSchemas, cache) {
167 // This instance of Ajv is private
168 // it should not be customized or used
169 const ajv = new Ajv({
170 coerceTypes: true,
171 useDefaults: true,
172 removeAdditional: true,
173 allErrors: true,
174 nullable: true,
175 cache
176 })
177
178 if (Array.isArray(externalSchemas)) {
179 externalSchemas.forEach(s => ajv.addSchema(s))
180 }
181
182 return ajv.compile.bind(ajv)
183}
184
185module.exports = { build, validate, serialize, isValidLogger, buildSchemaCompiler }
186module.exports.symbols = { bodySchema, querystringSchema, responseSchema, paramsSchema, headersSchema }