jsonschema
Advanced tools
Comparing version 1.2.10 to 1.2.11
@@ -50,2 +50,3 @@ 'use strict'; | ||
var list = types.map(function (v) { | ||
if(!v) return; | ||
return v.id && ('<' + v.id + '>') || (v+''); | ||
@@ -206,7 +207,13 @@ }); | ||
for (var property in properties) { | ||
var subschema = properties[property]; | ||
if(subschema===undefined){ | ||
continue; | ||
}else if(subschema===null){ | ||
throw new SchemaError('Unexpected null, expected schema in "properties"'); | ||
} | ||
if (typeof options.preValidateProperty == 'function') { | ||
options.preValidateProperty(instance, property, properties[property], options, ctx); | ||
options.preValidateProperty(instance, property, subschema, options, ctx); | ||
} | ||
var prop = getEnumerableProperty(instance, property); | ||
var res = this.validateSchema(prop, properties[property], options, ctx.makeChild(properties[property], property)); | ||
var res = this.validateSchema(prop, subschema, options, ctx.makeChild(subschema, property)); | ||
if(res.instance !== result.instance[property]) result.instance[property] = res.instance; | ||
@@ -265,2 +272,8 @@ result.importErrors(res); | ||
for (var pattern in patternProperties) { | ||
var subschema = patternProperties[pattern]; | ||
if(subschema===undefined){ | ||
continue; | ||
}else if(subschema===null){ | ||
throw new SchemaError('Unexpected null, expected schema in "patternProperties"'); | ||
} | ||
try { | ||
@@ -279,6 +292,6 @@ var regexp = new RegExp(pattern, 'u'); | ||
if (typeof options.preValidateProperty == 'function') { | ||
options.preValidateProperty(instance, property, patternProperties[pattern], options, ctx); | ||
options.preValidateProperty(instance, property, subschema, options, ctx); | ||
} | ||
var res = this.validateSchema(instance[property], patternProperties[pattern], options, ctx.makeChild(patternProperties[pattern], property)); | ||
var res = this.validateSchema(instance[property], subschema, options, ctx.makeChild(subschema, property)); | ||
if(res.instance !== result.instance[property]) result.instance[property] = res.instance; | ||
@@ -285,0 +298,0 @@ result.importErrors(res); |
@@ -85,3 +85,3 @@ /* | ||
} | ||
const?: any[] | ||
const?: any | ||
'enum'?: any[] | ||
@@ -88,0 +88,0 @@ type?: string | string[] |
@@ -278,2 +278,7 @@ 'use strict'; | ||
Validator.prototype.testType = function validateType (instance, schema, options, ctx, type) { | ||
if(type===undefined){ | ||
return; | ||
}else if(type===null){ | ||
throw new SchemaError('Unexpected null in "type" keyword'); | ||
} | ||
if (typeof this.types[type] == 'function') { | ||
@@ -280,0 +285,0 @@ return this.types[type].call(this, instance); |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "1.2.10", | ||
"version": "1.2.11", | ||
"license": "MIT", | ||
@@ -15,2 +15,4 @@ "dependencies": {}, | ||
"devDependencies": { | ||
"@stryker-mutator/core": "^4.0.0", | ||
"@stryker-mutator/mocha-runner": "^4.0.0", | ||
"chai": "~4.2.0", | ||
@@ -38,4 +40,5 @@ "eslint": "^7.7.0", | ||
"scripts": { | ||
"stryker": "stryker run", | ||
"test": "./node_modules/.bin/mocha -R spec" | ||
} | ||
} |
@@ -190,5 +190,5 @@ [![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.svg)](http://travis-ci.org/tdegrunt/jsonschema) | ||
### Custom properties | ||
### Custom keywords | ||
Specify your own JSON Schema properties with the validator.attributes property: | ||
Specify your own JSON Schema keywords with the validator.attributes property: | ||
@@ -304,33 +304,24 @@ ```javascript | ||
```javascript | ||
const coercionHook = function (instance, property, schema, options, ctx) { | ||
var value = instance[property]; | ||
// See examples/coercion.js | ||
function preValidateProperty(object, key, schema, options, ctx) { | ||
var value = object[key]; | ||
if (typeof value === 'undefined') return; | ||
// Skip null and undefined | ||
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 = Array.isArray(schema.type) ? 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); | ||
} | ||
// Test if the schema declares a type, but the type keyword fails validation | ||
if (schema.type && validator.attributes.type.call(validator, value, schema, options, ctx.makeChild(schema, key))) { | ||
// If the type is "number" but the instance is not a number, cast it | ||
if(schema.type==='number' && typeof value!=='number'){ | ||
object[key] = parseFloat(value); | ||
return; | ||
} | ||
// If we got a successful coercion we modify the property of the instance. | ||
if (typeof coerced != 'undefined') { | ||
instance[property] = coerced; | ||
// If the type is "string" but the instance is not a string, cast it | ||
if(schema.type==='string' && typeof value!=='string'){ | ||
object[key] = String(value).toString(); | ||
return; | ||
} | ||
} | ||
}.bind(validator) | ||
}; | ||
// And now, to actually perform validation with the coercion hook! | ||
v.validate(instance, schema, { preValidateProperty: coercionHook }); | ||
v.validate(instance, schema, { preValidateProperty }); | ||
``` | ||
@@ -337,0 +328,0 @@ |
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
69094
11
1593
6
389