|
2 | 2 |
|
3 | 3 | const _ = require('lodash');
|
4 | 4 | const Ajv = require('ajv');
|
| 5 | +const Ajv04 = require('ajv-draft-04'); |
5 | 6 | const {
|
6 | 7 | CONST,
|
7 | 8 | errors: {
|
@@ -210,28 +211,40 @@ exports.validateLastOperationRequest = function () {
|
210 | 211 | exports.validateSchemaForRequest = function (target, operation) {
|
211 | 212 | return function (req, res, next) {
|
212 | 213 | const plan = getPlanFromRequest(req);
|
213 |
| - |
| 214 | + |
214 | 215 | const schema = _.get(plan, `schemas.${target}.${operation}.parameters`);
|
215 |
| - |
| 216 | + |
216 | 217 | if (schema) {
|
217 | 218 | const parameters = _.get(req, 'body.parameters', {});
|
218 |
| - |
| 219 | + |
219 | 220 | const schemaVersion = schema.$schema || '';
|
220 |
| - const validator = new Ajv({ schemaId: 'auto' }); |
| 221 | + const validator = new Ajv({ $schemaId: 'auto' }); |
| 222 | + const ajv04Validator = new Ajv04(); |
| 223 | + logger.info('Using Ajv04 for validating draft-04 schema'); |
| 224 | + let validate; |
221 | 225 | if (schemaVersion.includes('draft-06')) {
|
222 | 226 | validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
|
| 227 | + validate = validator.compile(schema); |
223 | 228 | } else if (schemaVersion.includes('draft-04')) {
|
224 |
| - validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); |
225 |
| - } else if (!schemaVersion.includes('draft-07')) { |
226 |
| - validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); |
227 |
| - validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); |
| 229 | + validate = ajv04Validator.compile(schema); |
228 | 230 | }
|
229 |
| - const validate = validator.compile(schema); |
230 |
| - |
231 |
| - const isValid = validate(parameters); |
232 |
| - if (!isValid) { |
233 |
| - const reason = _.map(validate.errors, ({ dataPath, message }) => `${dataPath} ${message}`).join(', '); |
234 |
| - return next(new InvalidServiceParameters(`Failed to validate service parameters, reason: ${reason}`)); |
| 231 | + if (schemaVersion == '') { |
| 232 | + validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); |
| 233 | + validate = validator.compile(schema); |
| 234 | + const ajv04Validate = ajv04Validator.compile(schema); |
| 235 | + if (!ajv04Validate(parameters) && !validate(parameters)) { |
| 236 | + let reason = _.map(validate.errors, ({ dataPath, message }) => `${dataPath} ${message}`).join(', '); |
| 237 | + if (reason == '') { |
| 238 | + reason = _.map(ajv04Validate.errors, ({ dataPath, message }) => `${dataPath} ${message}`).join(', '); |
| 239 | + } |
| 240 | + return next(new InvalidServiceParameters(`Failed to validate service parameters, reason: ${reason}`)); |
| 241 | + } |
| 242 | + } else { |
| 243 | + const isValid = validate(parameters); |
| 244 | + if (!isValid) { |
| 245 | + const reason = _.map(validate.errors, ({ dataPath, message }) => `${dataPath} ${message}`).join(', '); |
| 246 | + return next(new InvalidServiceParameters(`Failed to validate service parameters, reason: ${reason}`)); |
| 247 | + } |
235 | 248 | }
|
236 | 249 | }
|
237 | 250 | next();
|
|
0 commit comments