jsonschema
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -501,4 +501,5 @@ 'use strict'; | ||
/** | ||
* Validates whether the instance value is of a certain defined format, when the instance value is a string. | ||
* The following format are supported: | ||
* Validates whether the instance value is of a certain defined format or a custom | ||
* format. | ||
* The following formats are supported for string types: | ||
* - date-time | ||
@@ -522,7 +523,4 @@ * - date | ||
validators.format = function validateFormat (instance, schema, options, ctx) { | ||
if (!(typeof instance === 'string')) { | ||
return null; | ||
} | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (!helpers.isFormat(instance, schema.format)) { | ||
if (!result.disableFormat && !helpers.isFormat(instance, schema.format, this)) { | ||
result.addError({ | ||
@@ -529,0 +527,0 @@ name: 'format', |
@@ -37,2 +37,3 @@ 'use strict'; | ||
this.throwError = options && options.throwError; | ||
this.disableFormat = options && options.disableFormat === true; | ||
}; | ||
@@ -153,4 +154,4 @@ | ||
exports.isFormat = function isFormat (input, format) { | ||
if (FORMAT_REGEXPS[format] !== undefined) { | ||
exports.isFormat = function isFormat (input, format, validator) { | ||
if (typeof input === 'string' && FORMAT_REGEXPS[format] !== undefined) { | ||
if (FORMAT_REGEXPS[format] instanceof RegExp) { | ||
@@ -162,2 +163,5 @@ return FORMAT_REGEXPS[format].test(input); | ||
} | ||
} else if (validator && validator.customFormats && | ||
typeof validator.customFormats[format] === 'function') { | ||
return validator.customFormats[format](input); | ||
} | ||
@@ -164,0 +168,0 @@ return true; |
@@ -17,2 +17,5 @@ 'use strict'; | ||
var Validator = function Validator () { | ||
// Allow a validator instance to override global custom formats or to have their | ||
// own custom formats. | ||
this.customFormats = Object.create(Validator.prototype.customFormats); | ||
this.schemas = {}; | ||
@@ -26,2 +29,5 @@ this.unresolvedRefs = []; | ||
// Allow formats to be registered globally. | ||
Validator.prototype.customFormats = {}; | ||
// Hint at the presence of a property | ||
@@ -28,0 +34,0 @@ Validator.prototype.schemas = null; |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
@@ -91,5 +91,34 @@ [![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.svg)](http://travis-ci.org/tdegrunt/jsonschema) | ||
### String Formats | ||
### Formats | ||
#### Disabling the format keyword. | ||
You may disable format validation by providing `disableFormat: true` to the validator | ||
options. | ||
#### String Formats | ||
All formats are supported, phone numbers are expected to follow the [E.123](http://en.wikipedia.org/wiki/E.123) standard. | ||
#### Custom Formats | ||
You may add your own custom format functions. Format functions accept the input | ||
being validated and return a boolean value. If the returned value is `true`, then | ||
validation succeeds. If the returned value is `false`, then validation fails. | ||
* Formats added to `Validator.prototype.customFormats` do not affect previously instantiated | ||
Validators. This is to prevent validator instances from being altered once created. | ||
It is conceivable that multiple validators may be created to handle multiple schemas | ||
with different formats in a program. | ||
* Formats added to `validator.customFormats` affect only that Validator instance. | ||
Here is an example that uses custom formats: | ||
``` | ||
Validator.prototype.customFormats.myFormat = function(input) { | ||
return input === 'myFormat'; | ||
}; | ||
var validator = new Validator(); | ||
validator.validate('myFormat', {type: 'string', format: 'myFormat'}).valid; // true | ||
validator.validate('foo', {type: 'string', format: 'myFormat'}).valid; // false | ||
``` | ||
### Results | ||
@@ -96,0 +125,0 @@ The first error found will be thrown as an `Error` object if `options.throwError` is `true`. Otherwise all results will be appended to the `result.errors` array which also contains the success flag `result.valid`. |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
67621
1810
211
0