ajv-keywords
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -10,3 +10,5 @@ 'use strict'; | ||
dynamicDefaults: require('./dynamicDefaults'), | ||
'if': require('./if') | ||
'if': require('./if'), | ||
deepProperties: require('./deepProperties'), | ||
deepRequired: require('./deepRequired') | ||
// formatMinimum: require('./formatMinimum'), | ||
@@ -13,0 +15,0 @@ // formatMaximum: require('./formatMaximum'), |
{ | ||
"name": "ajv-keywords", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Custom JSON-Schema keywords for ajv validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,2 +21,4 @@ # ajv-keywords | ||
- [if/then/else](#ifthenelse) | ||
- [deepProperties](#deepproperties) | ||
- [deepRequired](#deeprequired) | ||
- [regexp](#regexp) | ||
@@ -162,3 +164,3 @@ - [dynamicDefaults](#dynamicdefaults) | ||
__ Please note__: This keyword will be added to the next version of the JSON-Schema standard (draft-6), after it is published the keyword will be included in Ajv as standard validation keyword. | ||
__Please note__: This keyword will be added to the next version of the JSON-Schema standard (draft-6), after it is published the keyword will be included in Ajv as standard validation keyword. | ||
@@ -173,2 +175,4 @@ | ||
```javascript | ||
require('ajv-keywords')(ajv, 'if'); | ||
var schema = { | ||
@@ -188,2 +192,5 @@ type: 'array', | ||
var invalidItems = [ 1, 3, 5, 11, 12 ]; // etc. | ||
ajv.validate(schema, validItems); // true | ||
ajv.validate(schema, invalidItems); // false | ||
``` | ||
@@ -194,2 +201,87 @@ | ||
### `deepRequired` | ||
This keyword allows to check that some deep properties (identified by JSON pointers) are available. The value should be an array of JSON pointers to the data, starting from the current position in data. | ||
```javascript | ||
var schema = { | ||
type: 'object', | ||
deepRequired: ["users/1/role"] | ||
}; | ||
var validData = { | ||
users: [ | ||
{}, | ||
{ | ||
id: 123, | ||
role: 'admin' | ||
} | ||
] | ||
}; | ||
var invalidData = { | ||
users: [ | ||
{}, | ||
{ | ||
id: 123 | ||
} | ||
] | ||
}; | ||
``` | ||
See [json-schema-org/json-schema-spec#203](https://github.com/json-schema-org/json-schema-spec/issues/203#issue-197211916) for an example of the equivalent schema without `deepRequired` keyword. | ||
## `deepProperties` | ||
This keyword allows to validate deep properties (identified by JSON pointers). The value should be an object, where keys are JSON pointers to the data, starting from the current position in data, and the values are corresponding schemas. | ||
```javascript | ||
var schema = { | ||
type: 'object', | ||
deepProperties: { | ||
"users/1/role": { "enum": ["admin"] } | ||
} | ||
}; | ||
var validData = { | ||
users: [ | ||
{}, | ||
{ | ||
id: 123, | ||
role: 'admin' | ||
} | ||
] | ||
}; | ||
var alsoValidData = { | ||
users: { | ||
"1": { | ||
id: 123, | ||
role: 'admin' | ||
} | ||
} | ||
}; | ||
var invalidData = { | ||
users: [ | ||
{}, | ||
{ | ||
id: 123, | ||
role: 'user' | ||
} | ||
] | ||
}; | ||
var alsoInvalidData = { | ||
users: { | ||
"1": { | ||
id: 123, | ||
role: 'user' | ||
} | ||
} | ||
}; | ||
``` | ||
### `regexp` | ||
@@ -196,0 +288,0 @@ |
50525
26
884
421