Comparing version 0.4.0 to 0.4.1
@@ -6,2 +6,4 @@ 'use strict'; | ||
, stableStringify = require('json-stable-stringify') | ||
, formats = require('./compile/formats') | ||
, util = require('./compile/util'); | ||
@@ -27,2 +29,3 @@ module.exports = Ajv; | ||
this._byJson = {}; | ||
this._formats = formats(this.opts.format); | ||
@@ -35,6 +38,8 @@ // this is done on purpose, so that methods are bound to the instance | ||
this.getSchema = getSchema; | ||
this.addFormat = addFormat; | ||
if (this.opts.meta !== false) | ||
addSchema(require('./refs/json-schema-draft-04.json'), META_SCHEMA_ID, true); | ||
addInitialSchemas(); | ||
addInitialFormats(); | ||
/** | ||
@@ -138,2 +143,29 @@ * Validate data using schema | ||
function addFormat(name, format) { | ||
if (typeof format == 'string') format = new RegExp(format); | ||
self._formats[name] = format; | ||
} | ||
function addInitialSchemas() { | ||
if (self.opts.meta !== false) | ||
addSchema(require('./refs/json-schema-draft-04.json'), META_SCHEMA_ID, true); | ||
var optsSchemas = self.opts.schemas; | ||
if (!optsSchemas) return; | ||
if (Array.isArray(optsSchemas)) addSchema(optsSchemas); | ||
else for (var key in optsSchemas) addSchema(optsSchemas[key], key); | ||
} | ||
function addInitialFormats() { | ||
var optsFormats = self.opts.formats; | ||
if (!optsFormats) return; | ||
for (var name in optsFormats) { | ||
var format = optsFormats[name]; | ||
addFormat(name, format); | ||
} | ||
} | ||
function checkUnique(id) { | ||
@@ -145,8 +177,1 @@ var schemaRef = self._refs[id]; | ||
} | ||
function copy(o, to) { | ||
to = to || {}; | ||
for (var key in o) to[key] = o[key]; | ||
return to; | ||
} |
'use strict'; | ||
var util = require('./util'); | ||
@@ -11,32 +12,40 @@ var DATE = /^\d\d\d\d-(\d\d)-(\d\d)$/; | ||
module.exports = { | ||
full: { | ||
date: date, | ||
'date-time': date_time, | ||
uri: uri, | ||
email: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[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]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i, | ||
regex: regex | ||
}, | ||
fast: { | ||
// date: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/, | ||
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
'date-time': /^\d\d\d\d-[0-1]\d-[0-3]\d[t ][0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)$/i, | ||
// uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js | ||
uri: /^([a-z][a-z0-9+-.]*:){0,1}\/\/[^\s]*$/i, | ||
// email (sources from jsen validator): | ||
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363 | ||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'willful violation') | ||
email: /^[a-z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i, | ||
hostname: HOSTNAME, | ||
ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/, | ||
ipv6: /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i, | ||
regex: regex | ||
} | ||
module.exports = formats; | ||
function formats(mode) { | ||
mode = mode == 'full' ? 'full' : 'fast'; | ||
return util.copy(formats[mode]); | ||
} | ||
formats.fast = { | ||
// date: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/, | ||
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
'date-time': /^\d\d\d\d-[0-1]\d-[0-3]\d[t ][0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)$/i, | ||
// uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js | ||
uri: /^([a-z][a-z0-9+-.]*:){0,1}\/\/[^\s]*$/i, | ||
// email (sources from jsen validator): | ||
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363 | ||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'willful violation') | ||
email: /^[a-z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i, | ||
hostname: HOSTNAME, | ||
ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/, | ||
ipv6: /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i, | ||
regex: regex | ||
}; | ||
formats.full = { | ||
date: date, | ||
'date-time': date_time, | ||
uri: uri, | ||
email: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/, | ||
hostname: hostname, | ||
ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/, | ||
ipv6: /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a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\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i, | ||
regex: regex | ||
}; | ||
function date(str) { | ||
@@ -43,0 +52,0 @@ // full-date from http://tools.ietf.org/html/rfc3339#section-5.6 |
@@ -5,3 +5,2 @@ 'use strict'; | ||
, fs = require('fs') | ||
, formats = require('./formats') | ||
, resolve = require('./resolve') | ||
@@ -23,2 +22,3 @@ , util = require('./util') | ||
_rootSchema = _rootSchema || schema; | ||
var formats = this._formats; | ||
@@ -25,0 +25,0 @@ var validateCode = validateGenerator({ |
{{# def.definitions }} | ||
{{# def.setup:'format' }} | ||
{{ | ||
var $mode = it.opts.format == 'full' ? 'full' : 'fast' | ||
, $format = it.formats[$mode][$schema]; | ||
}} | ||
{{ var $format = it.formats[$schema]; }} | ||
{{## def.format: formats.{{= $mode }}['{{=$schema}}'] #}} | ||
{{## def.format: formats{{= it.util.getProperty($schema) }} #}} | ||
@@ -11,0 +8,0 @@ {{## def.checkFormat: |
{ | ||
"name": "ajv", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -107,2 +107,13 @@ # ajv - Another JSON Schema Validator | ||
##### .addFormat(String name, String|RegExp|Function format) | ||
Add custom format to validate strings. It can also be used to replace pre-defined formats for ajv instance. | ||
Strins be converted to RegExp. | ||
Function should return validation result as `true` or `false`. | ||
Custom formats can be also added via `formats` option. | ||
## Options | ||
@@ -113,2 +124,3 @@ | ||
- _format_: formats validation mode ('fast' by default). Pass 'full' for more correct and slow validation or `false` not to validate formats at all. 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. | ||
- _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method. | ||
- _meta_: add [meta-schema](http://json-schema.org/documentation.html) so it can be used by other schemas (true by default). | ||
@@ -129,4 +141,9 @@ - _uniqueItems_: validate `uniqueItems` keyword (true by default). | ||
##### 0.4.1 | ||
Custom formats support. | ||
##### 0.4.0 | ||
To improve performance, errors are set to `null` if there are no errors (previously empty array). | ||
Errors are set to `null` if there are no errors (previously empty array). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
246173
7320
147