forked from BRIKEV/openapi-validator-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (195 loc) · 7.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const Ajv = require('ajv').default;
const addFormats = require('ajv-formats');
const defaultOptions = require('./config/default');
const {
formatComponents,
configError,
sanitizeValueSchema,
} = require('./utils');
const {
argsValidation,
ajvErrors,
inputValidation,
endpointValidation,
optionsValidation,
responseArgsValidation,
} = require('./validators');
/**
* @name ValidateRequest
* @function
* @param {*} value Value we want to validate
* @param {string} endpoint OpenApi endpoint we want to validate
* @param {string} method OpenApi method we want to validate
* @param {string} contentType Content api of the request we want to validate
*/
/**
* @name ValidateParams
* @function
* @param {*} value Value we want to validate
* @param {string} endpoint OpenApi endpoint we want to validate
* @param {string} method OpenApi method we want to validate
* @param {string} contentType Content api of the request we want to validate
*/
/**
* @name ValidateResponse
* @function
* @param {*} value Value we want to validate
* @param {string} endpoint OpenApi endpoint we want to validate
* @param {string} method OpenApi method we want to validate
* @param {string} status OpenApi status we want to validate
* @param {string} contentType Content api of the request we want to validate
*/
/**
* @name IsRequestRequired
* @function
* @param {string} endpoint OpenApi endpoint we want to validate
* @param {string} method OpenApi method we want to validate
* @param {string} contentType Content api of the request we want to validate
*/
/**
* @name ValidateRequiredValues
* @function
* @param {*} value Values we want to see if are send as required parameters
* @param {string} endpoint OpenApi endpoint we want to validate
* @param {string} method OpenApi method we want to validate
*/
/**
* Validator methods
* @typedef {object} ValidatorMethods
* @property {ValidateRequest} validateRequest
* @property {ValidateParams} validateQueryParam
* @property {ValidateParams} validatePathParam
* @property {ValidateParams} validateHeaderParam
* @property {ValidateResponse} validateResponse
* @property {IsRequestRequired} isRequestRequired
* @property {ValidateRequiredValues} validateRequiredValues
*/
/**
* Validate method
* @param {object} openApiDef OpenAPI definition
* @param {object} options Options to extend the errorHandler or Ajv configuration
* @returns {ValidatorMethods} validator methods
*/
const validate = (openApiDef, userOptions = {}) => {
const options = {
...defaultOptions,
...(userOptions || {}),
};
const { errorHandler } = options;
const inputValidationError = inputValidation(openApiDef);
configError(inputValidationError, errorHandler);
const optionsValidationError = optionsValidation(userOptions);
configError(optionsValidationError, errorHandler);
const schemaOptions = { strictValidation: options.strictValidation };
const defsSchema = {
$id: 'defs.json',
definitions: {
components: formatComponents(openApiDef.components, schemaOptions),
},
};
const ajv = new Ajv({
schemas: [defsSchema],
...(options.ajvConfig || {}),
});
addFormats(ajv);
// Obtain method to validate against a given schema
const getSchemaValidator = (schemaName, schema) => {
// Slashes (/) are not supported in schema names, so we must replace them
const validSchemaName = schemaName.replace(/\//g, '-');
let validateSchema = ajv.getSchema(validSchemaName);
// Compile schema just once, only if it hasn't been defined previously
if (!validateSchema) {
ajv.addSchema(schema, validSchemaName);
validateSchema = ajv.getSchema(validSchemaName);
}
return validateSchema;
};
// Validate is current value matches the structure in the selected schema
const schemaValidation = (value, schema, type, schemaName) => {
const validateSchema = getSchemaValidator(schemaName, schema);
const valid = validateSchema(value);
if (!valid) {
return ajvErrors(validateSchema.errors, value, type, errorHandler);
}
return true;
};
const validateRequiredValues = (values, endpoint, method) => {
const argsValidationError = argsValidation(values, endpoint, method);
configError(argsValidationError, errorHandler);
const requiredParamsError = endpointValidation.requiredParams(
values,
openApiDef,
endpoint,
method,
);
configError(requiredParamsError, errorHandler);
return true;
};
const validateResponse = (value, endpoint, method, status, contentType = 'application/json') => {
const argsValidationError = responseArgsValidation(value, endpoint, method, status);
configError(argsValidationError, errorHandler);
const responseEndpoint = endpointValidation.response(
openApiDef,
endpoint,
method,
status,
contentType,
);
configError(responseEndpoint, errorHandler);
let responseSchema = {
...openApiDef.paths[endpoint][method].responses[status].content[contentType].schema,
};
responseSchema = formatComponents(responseSchema);
const schemaName = `${method}-${endpoint}-${status}-${contentType}-response`;
return schemaValidation(value, responseSchema, 'response', schemaName);
};
const isRequestRequired = (endpoint, method, contentType = 'application/json') => {
try {
if (method === 'get') return false;
const argsValidationError = argsValidation('request', endpoint, method);
configError(argsValidationError, errorHandler);
const requestEndpoint = endpointValidation.request(openApiDef, endpoint, method, contentType);
configError(requestEndpoint, errorHandler);
return !!openApiDef.paths[endpoint][method].requestBody.required;
} catch (error) {
// When we receive this is because it was not documented
// Document request body it is not required there might be endpoints
// where you don't want request body
if (error.message.includes('does not have requestBody definition')) return false;
throw error;
}
};
const validateRequest = (value, endpoint, method, contentType = 'application/json') => {
const argsValidationError = argsValidation(value, endpoint, method);
configError(argsValidationError, errorHandler);
const requestEndpoint = endpointValidation.request(openApiDef, endpoint, method, contentType);
configError(requestEndpoint, errorHandler);
let requestBodySchema = {
...openApiDef.paths[endpoint][method].requestBody.content[contentType].schema,
};
requestBodySchema = formatComponents(requestBodySchema);
const schemaName = `${method}-${endpoint}-${contentType}-request`;
return schemaValidation(value, requestBodySchema, 'request', schemaName);
};
const validateParam = type => (value, key, endpoint, method) => {
const argsValidationError = argsValidation(value, endpoint, method, key);
configError(argsValidationError, errorHandler);
const paramEndpoint = endpointValidation.params(openApiDef, endpoint, method, key, type);
configError(paramEndpoint, errorHandler);
let parametersSchema = paramEndpoint.parameter.schema;
parametersSchema = formatComponents(parametersSchema);
const sanitizeValue = sanitizeValueSchema(value, parametersSchema);
const schemaName = `${method}-${endpoint}-${key}-${type}-param`;
return schemaValidation(sanitizeValue, parametersSchema, type, schemaName);
};
return {
validateRequest,
validateQueryParam: validateParam('query'),
validatePathParam: validateParam('path'),
validateHeaderParam: validateParam('header'),
validateResponse,
validateRequiredValues,
isRequestRequired,
};
};
module.exports = validate;