Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ajv-keywords

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv-keywords - npm Package Compare versions

Comparing version 2.0.0-beta.2 to 2.0.0

keywords/select.js

3

keywords/_formatLimit.js

@@ -53,3 +53,4 @@ 'use strict';

var format = formats[name];
if (typeof format != 'object')
// the last condition is needed if it's RegExp from another window
if (typeof format != 'object' || format instanceof RegExp || !format.validate)
format = formats[name] = { validate: format };

@@ -56,0 +57,0 @@ if (!format.compare)

@@ -16,6 +16,7 @@ 'use strict';

type: 'object',
patternProperties: {
'^(\\/([^~\\/]|~0|~1)*)*(\\/)?$': util.metaSchemaRef(ajv)
propertyNames: {
type: 'string',
format: 'json-pointer'
},
additionalProperties: false
additionalProperties: util.metaSchemaRef(ajv)
}

@@ -22,0 +23,0 @@ };

'use strict';
module.exports = function generate__formatLimit(it, $keyword) {
module.exports = function generate__formatLimit(it, $keyword, $ruleType) {
var out = ' ';

@@ -4,0 +4,0 @@ var $lvl = it.level;

'use strict';
module.exports = function generate_patternRequired(it, $keyword) {
module.exports = function generate_patternRequired(it, $keyword, $ruleType) {
var out = ' ';

@@ -14,6 +14,11 @@ var $lvl = it.level;

var $key = 'key' + $lvl,
$idx = 'idx' + $lvl,
$matched = 'patternMatched' + $lvl,
$dataProperties = 'dataProperties' + $lvl,
$closingBraces = '',
$ownProperties = it.opts.ownProperties;
out += 'var ' + ($valid) + ' = true;';
if ($ownProperties) {
out += ' var ' + ($dataProperties) + ' = undefined;';
}
var arr1 = $schema;

@@ -25,5 +30,7 @@ if (arr1) {

$pProperty = arr1[i1 += 1];
out += ' var ' + ($matched) + ' = false; for (var ' + ($key) + ' in ' + ($data) + ') { ';
out += ' var ' + ($matched) + ' = false; ';
if ($ownProperties) {
out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
} else {
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
}

@@ -30,0 +37,0 @@ out += ' ' + ($matched) + ' = ' + (it.usePattern($pProperty)) + '.test(' + ($key) + '); if (' + ($matched) + ') break; } ';

'use strict';
module.exports = function generate_switch(it, $keyword) {
module.exports = function generate_switch(it, $keyword, $ruleType) {
var out = ' ';

@@ -4,0 +4,0 @@ var $lvl = it.level;

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

patternRequired: require('./patternRequired'),
'switch': require('./switch')
'switch': require('./switch'),
select: require('./select')
};

@@ -13,8 +13,5 @@ 'use strict';

return {
minimum: min,
exclusiveMinimum: exclusive,
maximum: max,
exclusiveMaximum: exclusive
};
return exclusive === true
? {exclusiveMinimum: min, exclusiveMaximum: max}
: {minimum: min, maximum: max};
},

@@ -21,0 +18,0 @@ metaSchema: {

{
"name": "ajv-keywords",
"version": "2.0.0-beta.2",
"version": "2.0.0",
"description": "Custom JSON-Schema keywords for Ajv validator",

@@ -34,6 +34,6 @@ "main": "index.js",

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

@@ -40,0 +40,0 @@ "chai": "^3.5.0",

@@ -21,2 +21,3 @@ # ajv-keywords

- [switch](#switch)
- [select/selectCases/selectDefault](#selectselectcasesselectdefault) (BETA)
- [patternRequired](#patternrequired)

@@ -195,2 +196,4 @@ - [prohibited](#prohibited)

```javascript
require('ajv-keywords')(ajv, 'switch');
var schema = {

@@ -238,4 +241,80 @@ type: 'array',

## `patternRequired`
### `select`/`selectCases`/`selectDefault`
These keywords allow to choose the schema to validate the data based on the value of some property in the validated data.
These keywords must be present in the same schema object (`selectDefault` is optional).
The value of `select` keyword should be a [$data reference](https://github.com/epoberezkin/ajv/tree/5.0.2-beta.0#data-reference) that points to any primitive JSON type (string, number, boolean or null) in the data that is validated. You can also use a constant of primitive type as the value of this keyword (e.g., for debugging purposes).
The value of `selectCases` keyword must be an object where each property name is a possible string representation of the value of `select` keyword and each property value is a corresponding schema (from draft-06 it can be boolean) that must be used to validate the data.
The value of `selectDefault` keyword is a schema (from draft-06 it can be boolean) that must be used to validate the data in case `selectCases` has no key equal to the stringified value of `select` keyword.
The validation succeeds in one of the following cases:
- the validation of data using selected schema succeeds,
- none of the schemas is selected for validation,
- the value of select is undefined (no property in the data that the data reference points to).
If `select` value (in data) is not a primitive type the validation fails.
__Please note__: these keywords require Ajv `$data` option to support [$data reference](https://github.com/epoberezkin/ajv/tree/5.0.2-beta.0#data-reference).
```javascript
require('ajv-keywords')(ajv, 'select');
var schema = {
type: object,
required: ['kind'],
properties: {
kind: { type: 'string' }
},
select: { $data: '0/kind' },
selectCases: {
foo: {
required: ['foo'],
properties: {
kind: {},
foo: { type: 'string' }
},
additionalProperties: false
},
bar: {
required: ['bar'],
properties: {
kind: {},
bar: { type: 'number' }
},
additionalProperties: false
}
},
selectDefault: {
propertyNames: {
not: { enum: ['foo', 'bar'] }
}
}
};
var validDataList = [
{ kind: 'foo', foo: 'any' },
{ kind: 'bar', bar: 1 },
{ kind: 'anything_else', not_bar_or_foo: 'any value' }
];
var invalidDataList = [
{ kind: 'foo' }, // no propery foo
{ kind: 'bar' }, // no propery bar
{ kind: 'foo', foo: 'any', another: 'any value' }, // additional property
{ kind: 'bar', bar: 1, another: 'any value' }, // additional property
{ kind: 'anything_else', foo: 'any' } // property foo not allowed
{ kind: 'anything_else', bar: 1 } // property bar not allowed
];
```
__Please note__: the current implementation is BETA. It does not allow using relative URIs in $ref keywords in schemas in `selectCases` and `selectDefault` that point ouside of these schemas. The workaround is to use absolute URIs (that can point to any (sub-)schema added to Ajv, including those inside the current root schema where `select` is used). See [tests](https://github.com/epoberezkin/ajv-keywords/blob/v2.0.0/spec/tests/select.json#L314).
### `patternRequired`
This keyword allows to require the presense of properties that match some pattern(s).

@@ -281,3 +360,3 @@

## `deepProperties`
### `deepProperties`

@@ -284,0 +363,0 @@ This keyword allows to validate deep properties (identified by JSON pointers).

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc