Skip to content

Commit

Permalink
Merge branch 'franciscomorais-feature/keywords-options'
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 22, 2020
2 parents c1c0ba7 + e5bed30 commit 8fd1e44
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ Defaults:
- `"full"` - more restrictive and slow validation. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode.
- `false` - ignore all format keywords.
- _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method.
- _keywords_: an object with custom keywords. Keys and values will be passed to `addKeyword` method.
- _unknownFormats_: handling of unknown formats. Option values:
- `true` (default) - if an unknown format is encountered the exception is thrown during schema compilation. If `format` keyword value is [$data reference](#data-reference) and it is unknown the validation will fail.
- `[String]` - an array of unknown format names that will be ignored. This option can be used to allow usage of third party schemas with format(s) for which you don't have definitions, but still fail if another unknown format is used. If `format` keyword value is [$data reference](#data-reference) and it is not in this array the validation will fail.
Expand Down
4 changes: 4 additions & 0 deletions lib/ajv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ declare namespace ajv {
unicode?: boolean;
format?: false | string;
formats?: object;
keywords?: object;
unknownFormats?: true | string[] | 'ignore';
schemas?: Array<object> | object;
schemaId?: '$id' | 'id' | 'auto';
Expand Down Expand Up @@ -252,6 +253,9 @@ declare namespace ajv {
formats: {
[index: string]: FormatDefinition | undefined;
};
keywords: {
[index: string]: KeywordDefinition | undefined;
};
compositeRule: boolean;
validate: (schema: object) => boolean;
util: {
Expand Down
9 changes: 9 additions & 0 deletions lib/ajv.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function Ajv(opts) {
this._metaOpts = getMetaSchemaOptions(this);

if (opts.formats) addInitialFormats(this);
if (opts.keywords) addInitialKeywords(this);
addDefaultMetaSchema(this);
if (typeof opts.meta == 'object') this.addMetaSchema(opts.meta);
if (opts.nullable) this.addKeyword('nullable', {metaSchema: {type: 'boolean'}});
Expand Down Expand Up @@ -467,6 +468,14 @@ function addInitialFormats(self) {
}


function addInitialKeywords(self) {
for (var name in self._opts.keywords) {
var keyword = self._opts.keywords[name];
self.addKeyword(name, keyword);
}
}


function checkUnique(self, id) {
if (self._schemas[id] || self._refs[id])
throw new Error('schema with key or id "' + id + '" already exists');
Expand Down
22 changes: 22 additions & 0 deletions spec/options/options_validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,29 @@ describe('validation options', function() {
}});

var validate = ajv.compile({ format: 'identifier' });

validate('Abc1') .should.equal(true);
validate('foo bar') .should.equal(false);
validate('123') .should.equal(false);
validate(123) .should.equal(true);
});
});

describe('keywords', function() {
it('should add keywords from options', function() {
var ajv = new Ajv({ keywords: {
identifier: {
type: 'string',
validate: function (schema, data ) {
return /^[a-z_$][a-z0-9_$]*$/i.test(data);
}
}
}});

var validate = ajv.compile({ identifier: true });

validate('Abc1') .should.equal(true);
validate('foo bar') .should.equal(false);
validate('123') .should.equal(false);
validate(123) .should.equal(true);
});
Expand Down

0 comments on commit 8fd1e44

Please sign in to comment.