refactor: rename keyword "constant" to "const", #367

master
Evgeny Poberezkin 2016-12-21 21:50:10 +00:00
parent ab488a4f57
commit 9f487e2833
13 changed files with 50 additions and 47 deletions

View File

@ -39,7 +39,7 @@ This way to define keywords is useful for:
__Please note__: In cases when validation flow is different depending on the schema and you have to use `if`s, this way to define keywords will have worse performance than compiled keyword returning different validation functions depending on the schema.
Example. `constant` keyword from version 5 proposals (that is equivalent to `enum` keyword with one item):
Example. `constant` keyword (a synonym for draft6 keyword `const`, it is equivalent to `enum` keyword with one item):
```javascript
ajv.addKeyword('constant', { validate: function (schema, data) {
@ -59,7 +59,7 @@ console.log(validate({foo: 'bar'})); // true
console.log(validate({foo: 'baz'})); // false
```
`constant` keyword is already available in Ajv with option `v5: true`.
`const` keyword is already available in Ajv.
__Please note:__ If the keyword does not define custom errors (see [Reporting errors in custom keywords](#reporting-errors-in-custom-keywords)) pass `errors: false` in its definition; it will make generated code more efficient.

View File

@ -34,7 +34,8 @@ The keywords and their values define what rules the data should satisfy to be va
- [patternRequired](#patternrequired-v5-proposal) (v5)
- [Keywords for all types](#keywords-for-all-types)
- [enum](#enum)
- [constant](#constant-v5-proposal) (v5)
- [const](#const)
- [Compound keywords](#compound-keywords)
- [not](#not)
- [oneOf](#oneof)
- [anyOf](#anyof)
@ -653,20 +654,20 @@ _invalid_: `1`, `"bar"`, `{"foo": "baz"}`, `[1, 2, 3, 4]`, any value not in the
### `constant` (v5 proposal)
### `const`
The value of this keyword can be anything. The data is valid if it is deeply equal to the value of the keyword.
__Example__
_schema_: `{ "constant": "foo" }`
_schema_: `{ "const": "foo" }`
_valid_: `"foo"`
_invalid_: any other value
The same can be achieved with `enum` keyword using the array with one item. But `constant` keyword is more that just a syntax sugar for `enum`. In combination with the [$data reference](https://github.com/epoberezkin/ajv#data-reference) it allows to define equality relations between different parts of the data. This cannot be achieved with `enum` keyword even with `$data` reference because `$data` cannot be used in place of one item - it can only be used in place of the whole array in `enum` keyword.
The same can be achieved with `enum` keyword using the array with one item. But `const` keyword is more that just a syntax sugar for `enum`. In combination with the [$data reference](https://github.com/epoberezkin/ajv#data-reference) it allows to define equality relations between different parts of the data. This cannot be achieved with `enum` keyword even with `$data` reference because `$data` cannot be used in place of one item - it can only be used in place of the whole array in `enum` keyword.
__Example__
@ -677,7 +678,7 @@ _schema_:
{
"properties": {
"foo": { "type": "number" },
"bar": { "constant": { "$data": "1/foo" } }
"bar": { "const": { "$data": "1/foo" } }
}
}
```
@ -688,6 +689,8 @@ _invalid_: `{ "foo": 1 }`, `{ "bar": 1 }`, `{ "foo": 1, "bar": 2 }`
## Compound keywords
### `not`
The value of the keyword should be a JSON schema. The data is valid if it is invalid according to this schema.

View File

@ -78,7 +78,7 @@ Performace of different validators by [json-schema-benchmark](https://github.com
- [assigning defaults](#assigning-defaults) to missing properties and items
- [coercing data](#coercing-data-types) to the types specified in `type` keywords
- [custom keywords](#defining-custom-keywords)
- keywords `switch`, `constant`, `contains`, `patternGroups`, `patternRequired`, `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum` from [JSON-schema v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals) with [option v5](#options)
- keywords `switch`, `const`, `contains`, `patternGroups`, `patternRequired`, `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum` from [JSON-schema v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals) with [option v5](#options)
- [v5 meta-schema](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#) for schemas using v5 keywords
- [v5 $data reference](#data-reference) to use values from the validated data as values for the schema keywords
- [asynchronous validation](#asynchronous-validation) of custom formats and keywords
@ -188,13 +188,13 @@ Ajv supports all validation keywords from draft 4 of JSON-schema standard:
- [for strings](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#keywords-for-strings) - maxLength, minLength, pattern, format
- [for arrays](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#keywords-for-arrays) - maxItems, minItems, uniqueItems, items, additionalItems
- [for objects](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#keywords-for-objects) - maxProperties, minproperties, required, properties, patternProperties, additionalProperties, dependencies
- [compound keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#keywords-for-all-types) - enum, not, oneOf, anyOf, allOf
- [for all types](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#keywords-for-all-types) - enum, [const](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#const)
- [compound keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#compound-keywords) - not, oneOf, anyOf, allOf
With option `v5: true` Ajv also supports all validation keywords and [$data reference](#data-reference) from [v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals) for JSON-schema standard:
- [switch](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#switch-v5-proposal) - conditional validation with a sequence of if/then clauses
- [contains](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#contains-v5-proposal) - check that array contains a valid item
- [constant](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#constant-v5-proposal) - check that data is equal to some value
- [patternGroups](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#patterngroups-v5-proposal) - a more powerful alternative to patternProperties
- [patternRequired](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#patternrequired-v5-proposal) - like `required` but with patterns that some property should match.
- [formatMaximum, formatMinimum, formatExclusiveMaximum, formatExclusiveMinimum](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#formatmaximum--formatminimum-and-exclusiveformatmaximum--exclusiveformatminimum-v5-proposal) - setting limits for date, time, etc.
@ -232,7 +232,7 @@ You can find patterns used for format validation and the sources that were used
With `v5` option you can use values from the validated data as the values for the schema keywords. See [v5 proposal](https://github.com/json-schema/json-schema/wiki/$data-(v5-proposal)) for more information about how it works.
`$data` reference is supported in the keywords: constant, enum, format, maximum/minimum, exclusiveMaximum / exclusiveMinimum, maxLength / minLength, maxItems / minItems, maxProperties / minProperties, formatMaximum / formatMinimum, formatExclusiveMaximum / formatExclusiveMinimum, multipleOf, pattern, required, uniqueItems.
`$data` reference is supported in the keywords: const, enum, format, maximum/minimum, exclusiveMaximum / exclusiveMinimum, maxLength / minLength, maxItems / minItems, maxProperties / minProperties, formatMaximum / formatMinimum, formatExclusiveMaximum / formatExclusiveMinimum, multipleOf, pattern, required, uniqueItems.
The value of "$data" should be a [JSON-pointer](https://tools.ietf.org/html/rfc6901) to the data (the root is always the top level data object, even if the $data reference is inside a referenced subschema) or a [relative JSON-pointer](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00) (it is relative to the current point in data; if the $data reference is inside a referenced subschema it cannot point to the data outside of the root level for this subschema).
@ -273,7 +273,7 @@ var validData = {
}
```
`$data` reference is resolved safely - it won't throw even if some property is undefined. If `$data` resolves to `undefined` the validation succeeds (with the exclusion of `constant` keyword). If `$data` resolves to incorrect type (e.g. not "number" for maximum keyword) the validation fails.
`$data` reference is resolved safely - it won't throw even if some property is undefined. If `$data` resolves to `undefined` the validation succeeds (with the exclusion of `const` keyword). If `$data` resolves to incorrect type (e.g. not "number" for maximum keyword) the validation fails.
## $merge and $patch keywords
@ -989,7 +989,7 @@ Defaults:
##### Validation and reporting options
- _v5_: add keywords `switch`, `constant`, `contains`, `patternGroups`, `patternRequired`, `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum` from [JSON-schema v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals). With this option added schemas without `$schema` property are validated against [v5 meta-schema](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#). `false` by default.
- _v5_: add keywords `switch`, `const`, `contains`, `patternGroups`, `patternRequired`, `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum` from [JSON-schema v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals). With this option added schemas without `$schema` property are validated against [v5 meta-schema](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#). `false` by default.
- _allErrors_: check all rules collecting all errors. Default is to return after the first error.
- _verbose_: include the reference to the part of the schema (`schema` and `parentSchema`) and validated data in errors (false by default).
- _jsonPointers_: set `dataPath` propery of errors using [JSON Pointers](https://tools.ietf.org/html/rfc6901) instead of JavaScript property access notation.

View File

@ -19,7 +19,7 @@ var KEYWORDS = [
'additionalProperties',
'enum',
'format',
'constant'
'const'
];
module.exports = function (metaSchema, keywordsJsonPointer) {

View File

@ -62,8 +62,8 @@ function Ajv(opts) {
this._compilations = [];
this.RULES = rules();
this.addKeyword('constant', {
inline: require('./dotjs/constant'),
this.addKeyword('const', {
inline: require('./dotjs/const'),
statements: true,
errors: 'full'
});

View File

@ -7,4 +7,4 @@
var schema{{=$lvl}} = validate.schema{{=$schemaPath}};
{{?}}
var {{=$valid}} = equal({{=$data}}, schema{{=$lvl}});
{{# def.checkError:'constant' }}
{{# def.checkError:'const' }}

View File

@ -114,7 +114,7 @@
patternGroups: "'should NOT have {{=$moreOrLess}} than {{=$limit}} properties matching pattern \"{{=it.util.escapeQuotes($pgProperty)}}\"'",
patternRequired: "'should have property matching pattern \\'{{=$missingPattern}}\\''",
switch: "'should pass \"switch\" keyword validation'",
constant: "'should be equal to constant'",
const: "'should be equal to constant'",
_formatLimit: "'should be {{=$opStr}} \"{{#def.concatSchemaEQ}}\"'",
_formatExclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'"
} #}}
@ -147,7 +147,7 @@
patternGroups: "validate.schema{{=$schemaPath}}",
patternRequired: "validate.schema{{=$schemaPath}}",
switch: "validate.schema{{=$schemaPath}}",
constant: "validate.schema{{=$schemaPath}}",
const: "validate.schema{{=$schemaPath}}",
_formatLimit: "{{#def.schemaRefOrQS}}",
_formatExclusiveLimit: "validate.schema{{=$schemaPath}}"
} #}}
@ -179,7 +179,7 @@
patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
patternRequired: "{ missingPattern: '{{=$missingPattern}}' }",
switch: "{ caseIndex: {{=$caseIndex}} }",
constant: "{}",
const: "{}",
_formatLimit: "{ comparison: {{=$opExpr}}, limit: {{#def.schemaValueQS}}, exclusive: {{=$exclusive}} }",
_formatExclusiveLimit: "{}"
} #}}

View File

@ -137,7 +137,7 @@
"description": "custom keyword in async schema",
"schema": {
"$async": true,
"constant": 5
"const": 5
},
"tests": [
{

View File

@ -4,7 +4,7 @@
"schema": {
"properties": {
"sameAs": {
"constant": {
"const": {
"$data": "/thisOne"
}
},
@ -71,17 +71,17 @@
"sameArr": {
"items": [
{
"constant": {
"const": {
"$data": "/arr/0"
}
},
{
"constant": {
"const": {
"$data": "/arr/1"
}
},
{
"constant": {
"const": {
"$data": "/arr/2"
}
}
@ -143,7 +143,7 @@
"list": {
"type": "array",
"contains": {
"constant": {
"const": {
"$data": "/name"
}
}
@ -386,7 +386,7 @@
"type": "object",
"properties": {
"foo": {
"constant": {
"const": {
"$data": "/bar"
}
},
@ -396,7 +396,7 @@
},
"properties": {
"foo": {
"constant": {
"const": {
"$data": "/bar"
}
},

View File

@ -3,7 +3,7 @@
"description": "property is equal to another property",
"schema": {
"properties": {
"sameAs": { "constant": { "$data": "1/thisOne" } },
"sameAs": { "const": { "$data": "1/thisOne" } },
"thisOne": {}
}
},
@ -46,7 +46,7 @@
"description": "property values are equal to property names",
"schema": {
"additionalProperties": {
"constant": { "$data": "0#" }
"const": { "$data": "0#" }
}
},
"tests": [
@ -66,7 +66,7 @@
"description": "items are equal to their indeces",
"schema": {
"items": {
"constant": { "$data": "0#" }
"const": { "$data": "0#" }
}
},
"tests": [
@ -92,9 +92,9 @@
},
"sameArr": {
"items": [
{ "constant": { "$data": "2/arr/0" } },
{ "constant": { "$data": "2/arr/1" } },
{ "constant": { "$data": "2/arr/2" } }
{ "const": { "$data": "2/arr/0" } },
{ "const": { "$data": "2/arr/1" } },
{ "const": { "$data": "2/arr/2" } }
],
"additionalItems": false
}
@ -122,7 +122,7 @@
{
"description": "any data is equal to itself",
"schema": {
"constant": { "$data": "0" }
"const": { "$data": "0" }
},
"tests": [
{
@ -154,7 +154,7 @@
"name": { "type": "string" },
"list": {
"type": "array",
"contains": { "constant": { "$data": "2/name" } }
"contains": { "const": { "$data": "2/name" } }
}
}
},

View File

@ -1,7 +1,7 @@
[
{
"description": "constant keyword requires the value to be equal to some constant",
"schema": { "constant": 2 },
"description": "const keyword requires the value to be equal to some constant",
"schema": { "const": 2 },
"tests": [
{
"description": "same value is valid",
@ -21,8 +21,8 @@
]
},
{
"description": "constant keyword requires the value to be equal to some object",
"schema": { "constant": { "foo": "bar", "baz": "bax" } },
"description": "const keyword requires the value to be equal to some object",
"schema": { "const": { "foo": "bar", "baz": "bax" } },
"tests": [
{
"description": "same object is valid",
@ -47,8 +47,8 @@
]
},
{
"description": "constant keyword with null",
"schema": { "constant": null },
"description": "const keyword with null",
"schema": { "const": null },
"tests": [
{
"description": "null is valid",

View File

@ -28,9 +28,9 @@
]
},
{
"description": "contains keyword with constant keyword requires a specific item to be present",
"description": "contains keyword with const keyword requires a specific item to be present",
"schema": {
"contains": { "constant": 5 }
"contains": { "const": 5 }
},
"tests": [
{

View File

@ -106,14 +106,14 @@ describe('issue #182, NaN validation', function() {
});
describe('issue #204, options schemas and v5 used together', function() {
describe('issue #204, options schemas and $data used together', function() {
it('should use v5 metaschemas by default', function() {
var ajv = new Ajv({
v5: true,
schemas: [{id: 'str', type: 'string'}],
$data: true
});
var schema = { constant: 42 };
var schema = { const: 42 };
var validate = ajv.compile(schema);
validate(42) .should.equal(true);