-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JSON Schema validations for /tags (#10486)
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
Showing
8 changed files
with
396 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/server/api/v2/utils/validators/input/schemas/tags-add.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
core/server/api/v2/utils/validators/input/schemas/tags-edit.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
core/server/api/v2/utils/validators/input/schemas/tags.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
core/server/api/v2/utils/validators/utils/strip-keyword.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.