Skip to content

Commit

Permalink
i18n support for ajv-i18n package
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 13, 2015
1 parent 6723287 commit 34993de
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 27 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ajv - Another JSON Schema Validator

One of the fastest JSON Schema validators for node.js and browser.
Currently the fastest JSON Schema validator for node.js and browser.

It uses precompiled [doT templates](https://github.com/olado/doT) to generate super-fast validating functions.

Expand Down Expand Up @@ -255,6 +255,31 @@ Options can have properties `separator` (string used to separate errors, ", " by

## Options

Defaults:

```
{
allErrors: false,
removeAdditional: false,
verbose: false,
format: 'fast',
formats: {},
schemas: {},
meta: true,
validateSchema: true,
inlineRefs: true,
missingRefs: true,
loadSchema: function(uri, cb) { /* ... */ cb(err, schema); },
uniqueItems: true,
unicode: true,
beautify: false,
cache: new Cache,
jsonPointers: false,
i18n: false,
messages: true
}
```

- _allErrors_: check all rules collecting all errors. Default is to return after the first error.
- _removeAdditional_: remove additional properties. Default is not to remove. If the option is 'all', then all additional properties are removed, regardless of `additionalProperties` keyword in schema (and no validation is made for them). If the option is `true` (or truthy), only additional properties with `additionalProperties` keyword equal to `false` are removed. If the option is 'failing', then additional properties that fail schema validation will be removed too (where `additionalProperties` keyword is schema).
- _verbose_: include the reference to the part of the schema and validated data in errors (false by default).
Expand All @@ -271,6 +296,9 @@ Options can have properties `separator` (string used to separate errors, ", " by
- _beautify_: format the generated function with [js-beautify](https://github.com/beautify-web/js-beautify) (the validating function is generated without line-breaks). `npm install js-beautify` to use this option. `true` or js-beautify options can be passed.
- _cache_: an optional instance of cache to store compiled schemas using stable-stringified schema as a key. For example, set-associative cache [sacjs](https://github.com/epoberezkin/sacjs) can be used. If not passed then a simple hash is used which is good enough for the common use case (a limited number of statically defined schemas). Cache should have methods `put(key, value)`, `get(key)` and `del(key)`.
- _jsonPointers_: Output `dataPath` using JSON Pointers instead of JS path notation.
- _i18n_: Support internationalization of error messages using [ajv-i18n](https://github.com/epoberezkin/ajv-i18n). See its repo for details.
- _messages_: Include human-readable messages in errors. `true` by default. `messages: false` can be added when internationalization (options `i18n`) is used.


## Tests

Expand Down
4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ module.exports = function(config) {

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
singleRun: true,

browserNoActivityTimeout: 30000
});
};
37 changes: 30 additions & 7 deletions lib/dot/definitions.def
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,17 @@
{{## def._error:_rule:
{
keyword: '{{=_rule}}',
dataPath: (dataPath || '') + {{= it.errorPath }},
message: {{# def._errorMessages[_rule] }}
{{? it.opts.verbose }}, schema: {{# def._errorSchemas[_rule] }}, data: {{=$data}}{{?}}
dataPath: (dataPath || '') + {{= it.errorPath }}
{{? it.opts.messages !== false }}
, message: {{# def._errorMessages[_rule] }}
{{?}}
{{? it.opts.verbose || it.opts.i18n }}
, schema: {{# def._errorSchemas[_rule] }}
{{?}}
{{? it.opts.verbose }}
, data: {{=$data}}
{{?}}
{{# def._errorParams[_rule] || '' }}
}
#}}

Expand Down Expand Up @@ -125,9 +133,9 @@
{{## def._errorMessages = {
$ref: "'can\\\'t resolve reference {{=it.util.escapeQuotes($schema)}}'",
additionalItems: "'should NOT have more than {{=$schema.length}} items'",
additionalProperties: "'additional properties NOT allowed'",
additionalProperties: "'should NOT have additional properties'",
anyOf: "'should match some schema in anyOf'",
dependencies: "'{{? $deps.length == 1 }}property {{= it.util.escapeQuotes($deps[0]) }} is{{??}}properties {{= it.util.escapeQuotes($deps.join(\", \")) }} are{{?}} required when property {{= it.util.escapeQuotes($property) }} is present'",
dependencies: "'should have {{? $deps.length == 1 }}property {{= it.util.escapeQuotes($deps[0]) }}{{??}}properties {{= it.util.escapeQuotes($deps.join(\", \")) }}{{?}} when property {{= it.util.escapeQuotes($property) }} is present'",
enum: "'should be equal to one of values'",
format: "'should match format {{=it.util.escapeQuotes($schema)}}'",
maximum: "'should be {{=$op}} {{=$schema}}'",
Expand All @@ -142,9 +150,9 @@
not: "'should NOT be valid'",
oneOf: "'should match exactly one schema in oneOf'",
pattern: "'should match pattern \"{{=it.util.escapeQuotes($schema)}}\"'",
required: "'property {{=$missingProperty}} is required'",
required: "'should have required property {{=$missingProperty}}'",
type: "'should be {{? $isArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}'",
uniqueItems: "'items ## ' + j + ' and ' + i + ' are duplicate'"
uniqueItems: "'should NOT have duplicate items (items ## ' + j + ' and ' + i + ' are identical)'"
} #}}


Expand Down Expand Up @@ -172,3 +180,18 @@
type: "{{? $isArray }}['{{= $typeSchema.join(\"','\") }}']{{??}}'{{=$typeSchema}}'{{?}}",
uniqueItems: "{{=$schema}}"
} #}}


{{## def._params = "{{? it.opts.i18n }}, params: " #}}

{{## def._errorParams = {
$ref: "{{# def._params }}{ escaped: '{{=it.util.escapeQuotes($schema)}}' }{{?}}",
dependencies: "{{# def._params }}{ n: {{=$deps.length}}, deps: '{{? $deps.length==1 }}{{= it.util.escapeQuotes($deps[0]) }}{{??}}{{= it.util.escapeQuotes($deps.join(\", \")) }}{{?}}', property: '{{= it.util.escapeQuotes($property) }}' }{{?}}",
format: "{{# def._params }}{ escaped: '{{=it.util.escapeQuotes($schema)}}' }{{?}}",
maximum: "{{# def._params }}{ condition: '{{=$op}} {{=$schema}}' }{{?}}",
minimum: "{{# def._params }}{ condition: '{{=$op}} {{=$schema}}' }{{?}}",
pattern: "{{# def._params }}{ escaped: '{{=it.util.escapeQuotes($schema)}}' }{{?}}",
required: "{{# def._params }}{ missingProperty: '{{=$missingProperty}}' }{{?}}",
type: "{{# def._params }}{ type: '{{? $isArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}' }{{?}}",
uniqueItems: "{{# def._params }}{ i: i, j: j }{{?}}"
} #}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "1.3.2",
"version": "1.4.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"files": [
Expand Down
28 changes: 14 additions & 14 deletions spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ describe('Validation errors', function () {
var validate = ajv.compile(schema);
shouldBeValid(validate, data);
shouldBeInvalid(validate, invalidData1);
shouldBeError(validate.errors[0], 'required', '.bar', 'property .bar is required');
shouldBeError(validate.errors[0], 'required', '.bar', 'should have required property .bar');
shouldBeInvalid(validate, invalidData2);
shouldBeError(validate.errors[0], 'required', '.foo', 'property .foo is required');
shouldBeError(validate.errors[0], 'required', '.foo', 'should have required property .foo');

var validateJP = ajvJP.compile(schema);
shouldBeValid(validateJP, data);
shouldBeInvalid(validateJP, invalidData1);
shouldBeError(validateJP.errors[0], 'required', '/bar', 'property bar is required');
shouldBeError(validateJP.errors[0], 'required', '/bar', 'should have required property bar');
shouldBeInvalid(validateJP, invalidData2);
shouldBeError(validateJP.errors[0], 'required', '/foo', 'property foo is required');
shouldBeError(validateJP.errors[0], 'required', '/foo', 'should have required property foo');

var fullValidate = fullAjv.compile(schema);
shouldBeValid(fullValidate, data);
shouldBeInvalid(fullValidate, invalidData1);
shouldBeError(fullValidate.errors[0], 'required', '/bar', 'property .bar is required');
shouldBeError(fullValidate.errors[0], 'required', '/bar', 'should have required property .bar');
shouldBeInvalid(fullValidate, invalidData2, 2);
shouldBeError(fullValidate.errors[0], 'required', '/foo', 'property .foo is required');
shouldBeError(fullValidate.errors[1], 'required', '/baz', 'property .baz is required');
shouldBeError(fullValidate.errors[0], 'required', '/foo', 'should have required property .foo');
shouldBeError(fullValidate.errors[1], 'required', '/baz', 'should have required property .baz');
});


Expand All @@ -121,24 +121,24 @@ describe('Validation errors', function () {
var validate = ajv.compile(schema);
shouldBeValid(validate, data);
shouldBeInvalid(validate, invalidData1);
shouldBeError(validate.errors[0], 'required', "['1']", "property '1' is required");
shouldBeError(validate.errors[0], 'required', "['1']", "should have required property '1'");
shouldBeInvalid(validate, invalidData2);
shouldBeError(validate.errors[0], 'required', "['2']", "property '2' is required");
shouldBeError(validate.errors[0], 'required', "['2']", "should have required property '2'");

var validateJP = ajvJP.compile(schema);
shouldBeValid(validateJP, data);
shouldBeInvalid(validateJP, invalidData1);
shouldBeError(validateJP.errors[0], 'required', "/1", "property '1' is required");
shouldBeError(validateJP.errors[0], 'required', "/1", "should have required property '1'");
shouldBeInvalid(validateJP, invalidData2);
shouldBeError(validateJP.errors[0], 'required', "/2", "property '2' is required");
shouldBeError(validateJP.errors[0], 'required', "/2", "should have required property '2'");

var fullValidate = fullAjv.compile(schema);
shouldBeValid(fullValidate, data);
shouldBeInvalid(fullValidate, invalidData1);
shouldBeError(fullValidate.errors[0], 'required', '/1', "property '1' is required");
shouldBeError(fullValidate.errors[0], 'required', '/1', "should have required property '1'");
shouldBeInvalid(fullValidate, invalidData2, 2);
shouldBeError(fullValidate.errors[0], 'required', '/2', "property '2' is required");
shouldBeError(fullValidate.errors[1], 'required', '/98', "property '98' is required");
shouldBeError(fullValidate.errors[0], 'required', '/2', "should have required property '2'");
shouldBeError(fullValidate.errors[1], 'required', '/98', "should have required property '98'");
});


Expand Down
7 changes: 4 additions & 3 deletions spec/json-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var instances = getAjvInstances({
verbose: true,
format: 'full',
inlineRefs: false,
jsonPointers: true
jsonPointers: true,
i18n: true
});

var remoteRefs = {
Expand All @@ -25,7 +26,7 @@ var remoteRefs = {
'http://localhost:1234/name.json': require('./remotes/name.json')
};

var remoteRefsWithIds = [ // order is important
var remoteRefsWithIds = [
require('./remotes/bar.json'),
require('./remotes/foo.json'),
require('./remotes/buu.json'),
Expand Down Expand Up @@ -57,7 +58,7 @@ jsonSchemaTest(instances, {
],
cwd: __dirname,
hideFolder: 'draft4/',
timeout: 60000
timeout: 90000
});


Expand Down

0 comments on commit 34993de

Please sign in to comment.