Socket
Socket
Sign inDemoInstall

ajv-keywords

Package Overview
Dependencies
6
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.5.0 to 2.0.0-beta.0

3

keywords/_formatLimit.js

@@ -15,5 +15,2 @@ 'use strict';

return function defFunc(ajv) {
if (ajv.RULES.keywords[keyword])
return console.warn('Keyword', keyword, 'is already defined');
defFunc.definition = {

@@ -20,0 +17,0 @@ type: 'string',

4

keywords/deepProperties.js

@@ -16,5 +16,3 @@ 'use strict';

'^(\\/([^~\\/]|~0|~1)*)*(\\/)?$': {
$ref: ajv._opts.v5
? 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#'
: 'http://json-schema.org/draft-04/schema#'
$ref: 'http://json-schema.org/draft-06/schema#'
}

@@ -21,0 +19,0 @@ },

@@ -19,3 +19,3 @@ 'use strict';

var $schemaFormat = it.schema.format,
$isDataFormat = it.opts.v5 && $schemaFormat.$data,
$isDataFormat = it.opts.$data && $schemaFormat.$data,
$closingBraces = '';

@@ -38,6 +38,6 @@ if ($isDataFormat) {

$schemaExcl = it.schema[$exclusiveKeyword],
$isDataExcl = it.opts.v5 && $schemaExcl && $schemaExcl.$data,
$isDataExcl = it.opts.$data && $schemaExcl && $schemaExcl.$data,
$op = $isMax ? '<' : '>',
$result = 'result' + $lvl;
var $isData = it.opts.v5 && $schema && $schema.$data,
var $isData = it.opts.$data && $schema && $schema.$data,
$schemaValue;

@@ -44,0 +44,0 @@ if ($isData) {

@@ -44,3 +44,3 @@ 'use strict';

it.compositeRule = $it.compositeRule = $wasComposite;
out += ' ' + ($ifPassed) + ' = valid' + ($it.level) + '; if (' + ($ifPassed) + ') { ';
out += ' ' + ($ifPassed) + ' = ' + ($nextValid) + '; if (' + ($ifPassed) + ') { ';
if (typeof $sch.then == 'boolean') {

@@ -75,3 +75,3 @@ if ($sch.then === false) {

}
out += ' var valid' + ($it.level) + ' = ' + ($sch.then) + '; ';
out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
} else {

@@ -116,3 +116,3 @@ $it.schema = $sch.then;

}
out += ' var valid' + ($it.level) + ' = ' + ($sch.then) + '; ';
out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
} else {

@@ -129,5 +129,5 @@ $it.schema = $sch.then;

}
out += '' + ($closingBraces) + 'var ' + ($valid) + ' = valid' + ($it.level) + '; ';
out += '' + ($closingBraces) + 'var ' + ($valid) + ' = ' + ($nextValid) + '; ';
out = it.util.cleanUpCode(out);
return out;
}
'use strict';
module.exports = function defFunc(ajv) {
if (!ajv._opts.v5) console.warn('keywords if/then/else require v5 option');
if (!ajv.RULES.keywords.switch) require('./switch')(ajv);

@@ -6,0 +6,0 @@ defFunc.definition = {

@@ -5,3 +5,2 @@ 'use strict';

'instanceof': require('./instanceof'),
propertyNames: require('./propertyNames'),
range: require('./range'),

@@ -14,7 +13,7 @@ regexp: require('./regexp'),

deepProperties: require('./deepProperties'),
deepRequired: require('./deepRequired')
// formatMinimum: require('./formatMinimum'),
// formatMaximum: require('./formatMaximum'),
// patternRequired: require('./patternRequired'),
// 'switch': require('./switch')
deepRequired: require('./deepRequired'),
formatMinimum: require('./formatMinimum'),
formatMaximum: require('./formatMaximum'),
patternRequired: require('./patternRequired'),
'switch': require('./switch')
};
'use strict';
module.exports = function defFunc(ajv) {
if (ajv.RULES.keywords.patternRequired)
return console.warn('Keyword patternRequired is already defined');
defFunc.definition = {

@@ -8,0 +5,0 @@ type: 'object',

'use strict';
module.exports = function defFunc(ajv) {
if (ajv.RULES.keywords.switch)
return console.warn('Keyword switch is already defined');
if (ajv.RULES.keywords.switch && ajv.RULES.keywords.if) return;
var metaSchemaUri = ajv._opts.v5
? 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#'
: 'http://json-schema.org/draft-04/schema#';
var metaSchemaUri = 'http://json-schema.org/draft-06/schema#';

@@ -11,0 +8,0 @@ defFunc.definition = {

{
"name": "ajv-keywords",
"version": "1.5.0",
"description": "Custom JSON-Schema keywords for ajv validator",
"version": "2.0.0-beta.0",
"description": "Custom JSON-Schema keywords for Ajv validator",
"main": "index.js",

@@ -34,6 +34,6 @@ "scripts": {

"peerDependencies": {
"ajv": ">=4.10.0"
"ajv": ">=5.0.1-beta.0"
},
"devDependencies": {
"ajv": "^4.10.0",
"ajv": "^5.0.1-beta.0",
"ajv-pack": "^0.2.0",

@@ -47,3 +47,3 @@ "chai": "^3.5.0",

"js-beautify": "^1.6.4",
"json-schema-test": "^1.2.1",
"json-schema-test": "^1.3.0",
"mocha": "^3.0.2",

@@ -50,0 +50,0 @@ "pre-commit": "^1.1.3",

# ajv-keywords
Custom JSON-Schema keywords for [ajv](https://github.com/epoberezkin/ajv) validator
Custom JSON-Schema keywords for [Ajv](https://github.com/epoberezkin/ajv) validator

@@ -19,4 +19,5 @@ [![Build Status](https://travis-ci.org/epoberezkin/ajv-keywords.svg?branch=master)](https://travis-ci.org/epoberezkin/ajv-keywords)

- [range and exclusiveRange](#range-and-exclusiverange)
- [propertyNames](#propertynames)
- [if/then/else](#ifthenelse)
- [switch](#switch)
- [patternRequired](#patternrequired)
- [prohibited](#prohibited)

@@ -26,2 +27,3 @@ - [deepProperties](#deepproperties)

- [regexp](#regexp)
- [formatMaximum / formatMinimum and formatExclusiveMaximum / formatExclusiveMinimum](#formatmaximum--formatminimum-and-formatexclusivemaximum--formatexclusiveminimum)
- [dynamicDefaults](#dynamicdefaults)

@@ -138,36 +140,5 @@ - [License](#license)

### `propertyNames`
This keyword allows to define the schema for the property names of the object. The value of this keyword should be a valid JSON schema (v5 schemas are supported with Ajv option `{v5: true}`).
```javascript
var schema = {
type: 'object'
propertyNames: {
anyOf: [
{ format: ipv4 },
{ format: hostname }
]
}
};
var validData = {
'192.128.0.1': {},
'test.example.com': {}
};
var invalidData = {
'1.2.3': {}
};
ajv.validate(schema, validData); // true
ajv.validate(schema, invalidData); // false
```
__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.
### `if`/`then`/`else`
These keywords allow to implement conditional validation. Their values should be valid JSON-schemas. At the moment it requires using Ajv with v5 option.
These keywords allow to implement conditional validation. Their values should be valid JSON-schemas.

@@ -201,2 +172,89 @@ If the data is valid according to the sub-schema in `if` keyword, then the result is equal to the result of data validation against the sub-schema in `then` keyword, otherwise - in `else` keyword (if `else` is absent, the validation succeeds).

### `switch`
This keyword allows to perform advanced conditional validation.
The value of the keyword is the array of if/then clauses. Each clause is the object with the following properties:
- `if` (optional) - the value is JSON-schema
- `then` (required) - the value is JSON-schema or boolean
- `continue` (optional) - the value is boolean
The validation process is dynamic; all clauses are executed sequentially in the following way:
1. `if`:
1. `if` property is JSON-schema according to which the data is:
1. valid => go to step 2.
2. invalid => go to the NEXT clause, if this was the last clause the validation of `switch` SUCCEEDS.
2. `if` property is absent => go to step 2.
2. `then`:
1. `then` property is `true` or it is JSON-schema according to which the data is valid => go to step 3.
2. `then` property is `false` or it is JSON-schema according to which the data is invalid => the validation of `switch` FAILS.
3. `continue`:
1. `continue` property is `true` => go to the NEXT clause, if this was the last clause the validation of `switch` SUCCEEDS.
2. `continue` property is `false` or absent => validation of `switch` SUCCEEDS.
```javascript
var schema = {
type: 'array',
items: {
type: 'integer',
'switch': [
{ if: { not: { minimum: 1 } }, then: false },
{ if: { maximum: 10 }, then: true },
{ if: { maximum: 100 }, then: { multipleOf: 10 } },
{ if: { maximum: 1000 }, then: { multipleOf: 100 } },
{ then: false }
]
}
};
var validItems = [1, 5, 10, 20, 50, 100, 200, 500, 1000];
var invalidItems = [1, 0, 2000, 11, 57, 123, 'foo'];
```
__Please note__: this keyword is moved here from Ajv, mainly to preserve beckward compatibility. It is unlikely to become a standard. It's preferreable to use `if`/`then`/`else` keywords if possible, as they are likely to be added to the standard. The above schema is equivalent to (for example):
```javascript
{
type: 'array',
items: {
type: 'integer',
if: { minimum: 1, maximum: 10 },
then: true,
else: {
if: { maximum: 100 },
then: { multipleOf: 10 },
else: {
if: { maximum: 1000 },
then: { multipleOf: 100 },
else: false
}
}
}
}
```
## `patternRequired`
This keyword allows to require the presense of properties that match some pattern(s).
This keyword applies only to objects. If the data is not an object, the validation succeeds.
The value of this keyword should be an array of strings, each string being a regular expression. For data object to be valid each regular expression in this array should match at least one property name in the data object.
If the array contains multiple regular expressions, more than one expression can match the same property name.
```javascript
var schema = { patternRequired: [ 'f.*o', 'b.*r' ] };
var validData = { foo: 1, bar: 2 };
var alsoValidData = { foobar: 3 };
var invalidDataList = [ {}, { foo: 1 }, { bar: 2 } ];
```
### `prohibited`

@@ -224,10 +282,16 @@

### `deepRequired`
## `deepProperties`
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.
This keyword allows to validate deep properties (identified by JSON pointers).
This keyword applies only to objects. If the data is not an object, the validation succeeds.
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 JSON schemas. For data object to be valid the value of each JSON pointer should be valid according to the corresponding schema.
```javascript
var schema = {
type: 'object',
deepRequired: ["/users/1/role"]
deepProperties: {
"/users/1/role": { "enum": ["admin"] }
}
};

@@ -245,2 +309,11 @@

var alsoValidData = {
users: {
"1": {
id: 123,
role: 'admin'
}
}
};
var invalidData = {

@@ -250,21 +323,31 @@ users: [

{
id: 123
id: 123,
role: 'user'
}
]
};
var alsoInvalidData = {
users: {
"1": {
id: 123,
role: 'user'
}
}
};
```
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.
### `deepRequired`
## `deepProperties`
This keyword allows to check that some deep properties (identified by JSON pointers) are available.
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.
This keyword applies only to objects. If the data is not an object, the validation succeeds.
The value should be an array of JSON pointers to the data, starting from the current position in data. For data object to be valid each JSON pointer should be some existing part of the data.
```javascript
var schema = {
type: 'object',
deepProperties: {
"/users/1/role": { "enum": ["admin"] }
}
deepRequired: ["/users/1/role"]
};

@@ -282,11 +365,2 @@

var alsoValidData = {
users: {
"1": {
id: 123,
role: 'admin'
}
}
};
var invalidData = {

@@ -296,23 +370,19 @@ users: [

{
id: 123,
role: 'user'
id: 123
}
]
};
var alsoInvalidData = {
users: {
"1": {
id: 123,
role: 'user'
}
}
};
```
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.
### `regexp`
This keyword allows to use regular expressions with flags in schemas (the standard `pattern` keyword does not support flags). The value of this keyword can be either a string (the result of `regexp.toString()`) or an object with the properties `pattern` and `flags` (the same strings that should be passed to RegExp constructor).
This keyword allows to use regular expressions with flags in schemas (the standard `pattern` keyword does not support flags).
This keyword applies only to strings. If the data is not a string, the validation succeeds.
The value of this keyword can be either a string (the result of `regexp.toString()`) or an object with the properties `pattern` and `flags` (the same strings that should be passed to RegExp constructor).
```javascript

@@ -339,2 +409,30 @@ var schema = {

### `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum`
These keywords allow to define minimum/maximum constraints when the format keyword defines ordering.
These keywords apply only to strings. If the data is not a string, the validation succeeds.
The value of keyword `formatMaximum` (`formatMinimum`) should be a string. This value is the maximum (minimum) allowed value for the data to be valid as determined by `format` keyword.
When this keyword is added, it defines comparison rules for formats `"date"`, `"time"` and `"date-time". Custom formats also can have comparison rules. See [addFormat](https://github.com/epoberezkin/ajv#api-addformat) method.
The value of keyword `formatExclusiveMaximum` (`formatExclusiveMinimum`) should be a boolean value. These keyword cannot be used without `formatMaximum` (`formatMinimum`). If this keyword value is equal to `true`, the data to be valid should not be equal to the value in `formatMaximum` (`formatMinimum`) keyword.
```javascript
require('ajv-keywords')(ajv, ['formatMinimum', 'formatMaximum']);
var schema = {
format: 'date',
formatMinimum: '2016-02-06',
formatMaximum: '2016-12-27',
formatExclusiveMaximum: true
}
var validDataList = ['2016-02-06', '2016-12-26', 1];
var invalidDataList = ['2016-02-05', '2016-12-27', 'abc'];
```
### `dynamicDefaults`

@@ -341,0 +439,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc