Skip to content

Commit

Permalink
Added JSON Schema validations for /tags (#10486)
Browse files Browse the repository at this point in the history
Added JSON Schema validations for /tags endpoints

refs #10438
refs #9100

- Added JSON Schemas for POST/PUT /tags endpoints
- Added 'strip' keyword definition schema allowing to strip data and not throw errors on further validation stages
  • Loading branch information
naz committed Feb 13, 2019
1 parent f8b62a0 commit 40cc6e6
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/server/api/v2/utils/validators/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = {
return require('./settings');
},

get tags() {
return require('./tags');
},

get users() {
return require('./users');
}
Expand Down
23 changes: 23 additions & 0 deletions core/server/api/v2/utils/validators/input/schemas/tags-add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "tags.add",
"title": "tags.add",
"description": "Schema for tags.add",
"type": "object",
"additionalProperties": false,
"properties": {
"tags": {
"type": "array",
"minItems": 1,
"maxItems": 1,
"additionalProperties": false,
"items": {
"type": "object",
"allOf": [{"$ref": "tags#/definitions/tag"}],
"required": ["name"]
}
}
},
"required": [ "tags" ]
}
18 changes: 18 additions & 0 deletions core/server/api/v2/utils/validators/input/schemas/tags-edit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "tags.edit",
"title": "tags.edit",
"description": "Schema for tags.edit",
"type": "object",
"additionalProperties": false,
"properties": {
"tags": {
"type": "array",
"minItems": 1,
"maxItems": 1,
"items": {"$ref": "tags#/definitions/tag"}
}
},
"required": [ "tags" ]
}
66 changes: 66 additions & 0 deletions core/server/api/v2/utils/validators/input/schemas/tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "tags",
"title": "tags",
"description": "Base tags definitions",
"definitions": {
"tag": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"maxLength": 191,
"pattern": "^([^,]|$)"
},
"slug": {
"type": ["string", "null"],
"maxLength": 191
},
"description": {
"type": ["string", "null"],
"maxLength": 500
},
"feature_image": {
"type": ["string", "null"],
"format": "uri-reference",
"maxLength": 2000
},
"visibility": {
"type": "string",
"enum": ["public", "internal"]
},
"meta_title": {
"type": ["string", "null"],
"maxLength": 300
},
"meta_description": {
"type": ["string", "null"],
"maxLength": 500
},
"id": {
"strip": true
},
"parent": {
"strip": true
},
"parent_id": {
"strip": true
},
"created_at": {
"strip": true
},
"created_by": {
"strip": true
},
"updated_at": {
"strip": true
},
"updated_by": {
"strip": true
}
}
}
}
}
15 changes: 15 additions & 0 deletions core/server/api/v2/utils/validators/input/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const jsonSchema = require('../utils/json-schema');

module.exports = {
add(apiConfig, frame) {
const schema = require('./schemas/tags-add');
const definitions = require('./schemas/tags');
return jsonSchema.validate(schema, definitions, frame.data);
},

edit(apiConfig, frame) {
const schema = require('./schemas/tags-edit');
const definitions = require('./schemas/tags');
return jsonSchema.validate(schema, definitions, frame.data);
}
};
7 changes: 5 additions & 2 deletions core/server/api/v2/utils/validators/utils/json-schema.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const Ajv = require('ajv');
const stripKeyword = require('./strip-keyword');
const common = require('../../../../../lib/common');

const validate = (schema, definitions, json) => {
const validate = (schema, definitions, data) => {
const ajv = new Ajv({
allErrors: true
});

stripKeyword(ajv);

const validation = ajv.addSchema(definitions).compile(schema);

validation(json);
validation(data);

if (validation.errors) {
return Promise.reject(new common.errors.ValidationError({
Expand Down
22 changes: 22 additions & 0 deletions core/server/api/v2/utils/validators/utils/strip-keyword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 'strip' keyword is introduced into schemas for following behavior:
* properties that are 'known' but should not be present in the model
* should be stripped from data and not throw validation errors.
*
* An example of such property is `tag.parent` which we want to ignore
* but not necessarily throw a validation error as it was present in
* responses in previous versions of API
*/
module.exports = function defFunc(ajv) {
defFunc.definition = {
errors: false,
modifying: true,
valid: true,
validate: function (schema, data, parentSchema, dataPath, parentData, propName) {
delete parentData[propName];
}
};

ajv.addKeyword('strip', defFunc.definition);
return ajv;
};

0 comments on commit 40cc6e6

Please sign in to comment.