Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit eb5a282

Browse files
authored
Bump ajv from v6.12.6 to v8.10.0 (#1556)
* Bump ajv from 6.12.6 to 8.10.0 * Add docs for schema validation using ajv library
1 parent 139d074 commit eb5a282

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

broker/applications/osb-broker/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@sf/express-commons": "*",
1313
"@sf/logger": "*",
1414
"@sf/models": "*",
15-
"ajv": "6.12.6",
15+
"ajv": "8.10.0",
16+
"ajv-draft-04": "1.0.0",
1617
"bluebird": "3.7.2",
1718
"camelcase-keys": "7.0.2",
1819
"express": "4.17.3",

broker/applications/osb-broker/src/api-controllers/middleware/index.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const _ = require('lodash');
44
const Ajv = require('ajv');
5+
const Ajv04 = require('ajv-draft-04');
56
const {
67
CONST,
78
errors: {
@@ -210,28 +211,40 @@ exports.validateLastOperationRequest = function () {
210211
exports.validateSchemaForRequest = function (target, operation) {
211212
return function (req, res, next) {
212213
const plan = getPlanFromRequest(req);
213-
214+
214215
const schema = _.get(plan, `schemas.${target}.${operation}.parameters`);
215-
216+
216217
if (schema) {
217218
const parameters = _.get(req, 'body.parameters', {});
218-
219+
219220
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;
221225
if (schemaVersion.includes('draft-06')) {
222226
validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
227+
validate = validator.compile(schema);
223228
} 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);
228230
}
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+
}
235248
}
236249
}
237250
next();

broker/applications/osb-broker/test/acceptance/service-broker-api.instances.director.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ describe('service-broker-api', function () {
232232
.then(res => {
233233
expect(res).to.have.status(400);
234234
expect(res.body.error).to.be.eql('Bad Request');
235-
expect(res.body.description).to.be.eql('Failed to validate service parameters, reason: .enum_foo should be equal to one of the allowed values');
235+
expect(res.body.description).to.be.eql('Failed to validate service parameters, reason: undefined must be equal to one of the allowed values');
236236
});
237237
});
238238

docs/Interoperator-Features.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,30 @@ spec
140140
# List valid plan ids from which upgrade is accepted
141141
update_predecessors:
142142
- '39d7d4c8-6fe2-4c2a-a5ca-b826937d5a88' # default plan
143-
```
143+
```
144+
145+
## Service plan schema validation
146+
147+
Interoperator uses Ajv JSON schema validator for validating the schema defined for a given plan. It supports JSON Schema draft-04 & draft-06. draft-04 support requires ajv-draft-04 package.
148+
149+
Sample drafts-04 schema supported:
150+
151+
```yaml
152+
schemas: &blueprint_schemas
153+
service_instance:
154+
create:
155+
parameters:
156+
"$schema": "http://json-schema.org/draft-04/schema#"
157+
additionalProperties: true
158+
```
159+
160+
If $schema is not explicitly defined in the plan template, we try validating both draft-04 & draft-06 schemas. The schema validation fails if it fails for both the draft versions.
161+
Sample template without $schema tag:
162+
163+
```yaml
164+
schemas: &blueprint_schemas
165+
service_instance:
166+
create:
167+
parameters:
168+
additionalProperties: true
169+
```

0 commit comments

Comments
 (0)