jsonschema
Advanced tools
Comparing version 1.2.0 to 1.2.2
@@ -190,2 +190,6 @@ 'use strict'; | ||
for (var property in properties) { | ||
if (typeof options.preValidateProperty == 'function') { | ||
options.preValidateProperty(instance, property, properties[property], options, ctx); | ||
} | ||
var prop = (instance || undefined) && instance[property]; | ||
@@ -218,2 +222,7 @@ var res = this.validateSchema(prop, properties[property], options, ctx.makeChild(properties[property], property)); | ||
var additionalProperties = schema.additionalProperties || {}; | ||
if (typeof options.preValidateProperty == 'function') { | ||
options.preValidateProperty(instance, property, additionalProperties, options, ctx); | ||
} | ||
var res = this.validateSchema(instance[property], additionalProperties, options, ctx.makeChild(additionalProperties, property)); | ||
@@ -247,2 +256,7 @@ if(res.instance !== result.instance[property]) result.instance[property] = res.instance; | ||
test = false; | ||
if (typeof options.preValidateProperty == 'function') { | ||
options.preValidateProperty(instance, property, patternProperties[pattern], options, ctx); | ||
} | ||
var res = this.validateSchema(instance[property], patternProperties[pattern], options, ctx.makeChild(patternProperties[pattern], property)); | ||
@@ -249,0 +263,0 @@ if(res.instance !== result.instance[property]) result.instance[property] = res.instance; |
@@ -7,7 +7,7 @@ /* | ||
constructor(); | ||
customFormats: CustomFormat[]; | ||
schemas: {[id:string]: Schema}; | ||
customFormats: {[formatName: string]: CustomFormat}; | ||
schemas: {[id: string]: Schema}; | ||
unresolvedRefs: string[]; | ||
attributes: {[property:string]: CustomProperty}; | ||
attributes: {[property: string]: CustomProperty}; | ||
@@ -27,3 +27,3 @@ addSchema(schema?: Schema, uri?: string): Schema|void; | ||
valid: boolean; | ||
addError(detail:string|ErrorDetail): ValidationError; | ||
addError(detail: string|ErrorDetail): ValidationError; | ||
toString(): string; | ||
@@ -88,2 +88,3 @@ } | ||
type?: string | string[] | ||
format?: string | ||
allOf?: Schema[] | ||
@@ -113,3 +114,3 @@ anyOf?: Schema[] | ||
base: string; | ||
schemas: {[base:string]: Schema}; | ||
schemas: {[base: string]: Schema}; | ||
} | ||
@@ -116,0 +117,0 @@ |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "1.2.0", | ||
"version": "1.2.2", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "dependencies": {}, |
@@ -175,2 +175,38 @@ [![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.svg)](http://travis-ci.org/tdegrunt/jsonschema) | ||
### Pre-Property Validation Hook | ||
If some processing of properties is required prior to validation a function may be passed via the options parameter of the validate function. For example, say you needed to perform type coercion for some properties: | ||
```const coercionHook = function (instance, property, schema, options, ctx) { | ||
var value = instance[property]; | ||
// Skip nulls and undefineds | ||
if (value === null || typeof value == 'undefined') { | ||
return; | ||
} | ||
// If the schema declares a type and the property fails type validation. | ||
if (schema.type && this.attributes.type.call(this, instance, schema, options, ctx.makeChild(schema, property))) { | ||
var types = (schema.type instanceof Array) ? schema.type : [schema.type]; | ||
var coerced = undefined; | ||
// Go through the declared types until we find something that we can | ||
// coerce the value into. | ||
for (var i = 0; typeof coerced == 'undefined' && i < types.length; i++) { | ||
// If we support coercion to this type | ||
if (lib.coercions[types[i]]) { | ||
// ...attempt it. | ||
coerced = lib.coercions[types[i]](value); | ||
} | ||
} | ||
// If we got a successful coercion we modify the property of the instance. | ||
if (typeof coerced != 'undefined') { | ||
instance[property] = coerced; | ||
} | ||
} | ||
}.bind(validator) | ||
// And now, to actually perform validation with the coercion hook! | ||
v.validate(instance, schema, { preValidateProperty: coercionHook }); | ||
``` | ||
## Tests | ||
@@ -177,0 +213,0 @@ Uses [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) as well as our own tests. |
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
75609
2013
249
13