jsonschema
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -61,4 +61,8 @@ 'use strict'; | ||
function testSchema(instance, options, ctx, callback, schema){ | ||
function testSchemaNoThrow(instance, options, ctx, callback, schema){ | ||
var throwError = options.throwError; | ||
options.throwError = false; | ||
var res = this.validateSchema(instance, schema, options, ctx); | ||
options.throwError = throwError; | ||
if (! res.valid && callback instanceof Function) { | ||
@@ -89,3 +93,3 @@ callback(res); | ||
if (!schema.anyOf.some( | ||
testSchema.bind( | ||
testSchemaNoThrow.bind( | ||
this, instance, options, ctx, function(res){inner.importErrors(res);} | ||
@@ -160,3 +164,3 @@ ))) { | ||
var count = schema.oneOf.filter( | ||
testSchema.bind( | ||
testSchemaNoThrow.bind( | ||
this, instance, options, ctx, function(res) {inner.importErrors(res);} | ||
@@ -189,3 +193,3 @@ ) ).length; | ||
validators.properties = function validateProperties (instance, schema, options, ctx) { | ||
if(instance === undefined || !(instance instanceof Object)) return; | ||
if(!this.types.object(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -198,3 +202,3 @@ var properties = schema.properties || {}; | ||
var prop = (instance || undefined) && instance[property]; | ||
var prop = Object.hasOwnProperty.call(instance, property) ? instance[property] : undefined; | ||
var res = this.validateSchema(prop, properties[property], options, ctx.makeChild(properties[property], property)); | ||
@@ -215,2 +219,3 @@ if(res.instance !== result.instance[property]) result.instance[property] = res.instance; | ||
function testAdditionalProperty (instance, schema, options, ctx, property, result) { | ||
if(!this.types.object(instance)) return; | ||
if (schema.properties && schema.properties[property] !== undefined) { | ||
@@ -247,3 +252,2 @@ return; | ||
validators.patternProperties = function validatePatternProperties (instance, schema, options, ctx) { | ||
if(instance === undefined) return; | ||
if(!this.types.object(instance)) return; | ||
@@ -287,3 +291,2 @@ var result = new ValidatorResult(instance, schema, options, ctx); | ||
validators.additionalProperties = function validateAdditionalProperties (instance, schema, options, ctx) { | ||
if(instance === undefined) return; | ||
if(!this.types.object(instance)) return; | ||
@@ -308,5 +311,3 @@ // if patternProperties is defined then we'll test when that one is called instead | ||
validators.minProperties = function validateMinProperties (instance, schema, options, ctx) { | ||
if (!instance || typeof instance !== 'object') { | ||
return null; | ||
} | ||
if (!this.types.object(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -331,5 +332,3 @@ var keys = Object.keys(instance); | ||
validators.maxProperties = function validateMaxProperties (instance, schema, options, ctx) { | ||
if (!instance || typeof instance !== 'object') { | ||
return null; | ||
} | ||
if (!this.types.object(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -356,10 +355,6 @@ var keys = Object.keys(instance); | ||
validators.items = function validateItems (instance, schema, options, ctx) { | ||
if (!Array.isArray(instance)) { | ||
return null; | ||
} | ||
var self = this; | ||
if (!this.types.array(instance)) return; | ||
if (!schema.items) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (instance === undefined || !schema.items) { | ||
return result; | ||
} | ||
instance.every(function (value, i) { | ||
@@ -392,5 +387,3 @@ var items = Array.isArray(schema.items) ? (schema.items[i] || schema.additionalItems) : schema.items; | ||
validators.minimum = function validateMinimum (instance, schema, options, ctx) { | ||
if (typeof instance !== 'number') { | ||
return null; | ||
} | ||
if (!this.types.number(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -420,5 +413,3 @@ var valid = true; | ||
validators.maximum = function validateMaximum (instance, schema, options, ctx) { | ||
if (typeof instance !== 'number') { | ||
return null; | ||
} | ||
if (!this.types.number(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -450,5 +441,3 @@ var valid; | ||
var validateMultipleOfOrDivisbleBy = function validateMultipleOfOrDivisbleBy (instance, schema, options, ctx, validationType, errorMessage) { | ||
if (typeof instance !== 'number') { | ||
return null; | ||
} | ||
if (!this.types.number(instance)) return; | ||
@@ -486,3 +475,3 @@ var validationArgument = schema[validationType]; | ||
validators.multipleOf = function validateMultipleOf (instance, schema, options, ctx) { | ||
return validateMultipleOfOrDivisbleBy(instance, schema, options, ctx, "multipleOf", "is not a multiple of (divisible by) "); | ||
return validateMultipleOfOrDivisbleBy.call(this, instance, schema, options, ctx, "multipleOf", "is not a multiple of (divisible by) "); | ||
}; | ||
@@ -497,3 +486,3 @@ | ||
validators.divisibleBy = function validateDivisibleBy (instance, schema, options, ctx) { | ||
return validateMultipleOfOrDivisbleBy(instance, schema, options, ctx, "divisibleBy", "is not divisible by (multiple of) "); | ||
return validateMultipleOfOrDivisbleBy.call(this, instance, schema, options, ctx, "divisibleBy", "is not divisible by (multiple of) "); | ||
}; | ||
@@ -515,5 +504,5 @@ | ||
}); | ||
} else if (instance && typeof instance==='object' && Array.isArray(schema.required)) { | ||
} else if (this.types.object(instance) && Array.isArray(schema.required)) { | ||
schema.required.forEach(function(n){ | ||
if(instance[n]===undefined){ | ||
if(!Object.hasOwnProperty.call(instance, n)){ | ||
result.addError({ | ||
@@ -537,5 +526,3 @@ name: 'required', | ||
validators.pattern = function validatePattern (instance, schema, options, ctx) { | ||
if (typeof instance !== 'string') { | ||
return null; | ||
} | ||
if (!this.types.string(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -574,2 +561,3 @@ if (!instance.match(schema.pattern)) { | ||
validators.format = function validateFormat (instance, schema, options, ctx) { | ||
if (instance===undefined) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -593,7 +581,7 @@ if (!result.disableFormat && !helpers.isFormat(instance, schema.format, this)) { | ||
validators.minLength = function validateMinLength (instance, schema, options, ctx) { | ||
if (!(typeof instance === 'string')) { | ||
return null; | ||
} | ||
if (!this.types.string(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (!(instance.length >= schema.minLength)) { | ||
var hsp = instance.match(/[\uDC00-\uDFFF]/g); | ||
var length = instance.length - (hsp ? hsp.length : 0); | ||
if (!(length >= schema.minLength)) { | ||
result.addError({ | ||
@@ -615,7 +603,8 @@ name: 'minLength', | ||
validators.maxLength = function validateMaxLength (instance, schema, options, ctx) { | ||
if (!(typeof instance === 'string')) { | ||
return null; | ||
} | ||
if (!this.types.string(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (!(instance.length <= schema.maxLength)) { | ||
// TODO if this was already computed in "minLength", use that value instead of re-computing | ||
var hsp = instance.match(/[\uDC00-\uDFFF]/g); | ||
var length = instance.length - (hsp ? hsp.length : 0); | ||
if (!(length <= schema.maxLength)) { | ||
result.addError({ | ||
@@ -637,5 +626,3 @@ name: 'maxLength', | ||
validators.minItems = function validateMinItems (instance, schema, options, ctx) { | ||
if (!Array.isArray(instance)) { | ||
return null; | ||
} | ||
if (!this.types.array(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -659,5 +646,3 @@ if (!(instance.length >= schema.minItems)) { | ||
validators.maxItems = function validateMaxItems (instance, schema, options, ctx) { | ||
if (!Array.isArray(instance)) { | ||
return null; | ||
} | ||
if (!this.types.array(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -683,6 +668,4 @@ if (!(instance.length <= schema.maxItems)) { | ||
validators.uniqueItems = function validateUniqueItems (instance, schema, options, ctx) { | ||
if (!this.types.array(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (!Array.isArray(instance)) { | ||
return result; | ||
} | ||
function testArrays (v, i, a) { | ||
@@ -727,5 +710,3 @@ for (var j = i + 1; j < a.length; j++) if (helpers.deepCompareStrict(v, a[j])) { | ||
validators.uniqueItems = function validateUniqueItems (instance, schema, options, ctx) { | ||
if (!Array.isArray(instance)) { | ||
return null; | ||
} | ||
if (!this.types.array(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -750,5 +731,3 @@ if (!instance.every(testArrays)) { | ||
validators.dependencies = function validateDependencies (instance, schema, options, ctx) { | ||
if (!instance || typeof instance != 'object') { | ||
return null; | ||
} | ||
if (!this.types.object(instance)) return; | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -800,8 +779,8 @@ for (var property in schema.dependencies) { | ||
validators['enum'] = function validateEnum (instance, schema, options, ctx) { | ||
if (instance === undefined) { | ||
return null; | ||
} | ||
if (!Array.isArray(schema['enum'])) { | ||
throw new SchemaError("enum expects an array", schema); | ||
} | ||
if (instance === undefined) { | ||
return null; | ||
} | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -812,3 +791,3 @@ if (!schema['enum'].some(helpers.deepCompareStrict.bind(null, instance))) { | ||
argument: schema['enum'], | ||
message: "is not one of enum values: " + schema['enum'].join(','), | ||
message: "is not one of enum values: " + schema['enum'].map(String).join(','), | ||
}); | ||
@@ -827,2 +806,5 @@ } | ||
validators['const'] = function validateEnum (instance, schema, options, ctx) { | ||
if (instance === undefined) { | ||
return null; | ||
} | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
@@ -829,0 +811,0 @@ if (!helpers.deepCompareStrict(schema['const'], instance)) { |
@@ -8,2 +8,4 @@ 'use strict'; | ||
module.exports.SchemaError = require('./helpers').SchemaError; | ||
module.exports.SchemaScanResult = require('./scan').SchemaScanResult; | ||
module.exports.scan = require('./scan').scan; | ||
@@ -10,0 +12,0 @@ module.exports.validate = function (instance, schema, options) { |
@@ -7,5 +7,8 @@ 'use strict'; | ||
var helpers = require('./helpers'); | ||
var scanSchema = require('./scan').scan; | ||
var ValidatorResult = helpers.ValidatorResult; | ||
var SchemaError = helpers.SchemaError; | ||
var SchemaContext = helpers.SchemaContext; | ||
//var anonymousBase = 'vnd.jsonschema:///'; | ||
var anonymousBase = '/'; | ||
@@ -44,52 +47,18 @@ /** | ||
*/ | ||
Validator.prototype.addSchema = function addSchema (schema, uri) { | ||
Validator.prototype.addSchema = function addSchema (schema, base) { | ||
var self = this; | ||
if (!schema) { | ||
return null; | ||
} | ||
var ourUri = uri || schema.id; | ||
this.addSubSchema(ourUri, schema); | ||
if (ourUri) { | ||
this.schemas[ourUri] = schema; | ||
var scan = scanSchema(base||anonymousBase, schema); | ||
var ourUri = base || schema.id; | ||
for(var uri in scan.id){ | ||
this.schemas[uri] = scan.id[uri]; | ||
} | ||
return this.schemas[ourUri]; | ||
}; | ||
Validator.prototype.addSubSchema = function addSubSchema(baseuri, schema) { | ||
if(!schema || typeof schema!='object') return; | ||
// Mark all referenced schemas so we can tell later which schemas are referred to, but never defined | ||
if(schema.$ref){ | ||
var resolvedUri = urilib.resolve(baseuri, schema.$ref); | ||
// Only mark unknown schemas as unresolved | ||
if (this.schemas[resolvedUri] === undefined) { | ||
this.schemas[resolvedUri] = null; | ||
this.unresolvedRefs.push(resolvedUri); | ||
} | ||
return; | ||
for(var uri in scan.ref){ | ||
this.unresolvedRefs.push(uri); | ||
} | ||
var ourUri = schema.id && urilib.resolve(baseuri, schema.id); | ||
var ourBase = ourUri || baseuri; | ||
if (ourUri) { | ||
if(this.schemas[ourUri]){ | ||
if(!helpers.deepCompareStrict(this.schemas[ourUri], schema)){ | ||
throw new Error('Schema <'+schema+'> already exists with different definition'); | ||
} | ||
return this.schemas[ourUri]; | ||
} | ||
this.schemas[ourUri] = schema; | ||
var documentUri = ourUri.replace(/^([^#]*)#$/, '$1'); | ||
this.schemas[documentUri] = schema; | ||
} | ||
this.addSubSchemaArray(ourBase, ((schema.items instanceof Array)?schema.items:[schema.items])); | ||
this.addSubSchemaArray(ourBase, ((schema.extends instanceof Array)?schema.extends:[schema.extends])); | ||
this.addSubSchema(ourBase, schema.additionalItems); | ||
this.addSubSchemaObject(ourBase, schema.properties); | ||
this.addSubSchema(ourBase, schema.additionalProperties); | ||
this.addSubSchemaObject(ourBase, schema.definitions); | ||
this.addSubSchemaObject(ourBase, schema.patternProperties); | ||
this.addSubSchemaObject(ourBase, schema.dependencies); | ||
this.addSubSchemaArray(ourBase, schema.disallow); | ||
this.addSubSchemaArray(ourBase, schema.allOf); | ||
this.addSubSchemaArray(ourBase, schema.anyOf); | ||
this.addSubSchemaArray(ourBase, schema.oneOf); | ||
this.addSubSchema(ourBase, schema.not); | ||
this.unresolvedRefs = this.unresolvedRefs.filter(function(uri){ | ||
return typeof self.schemas[uri]==='undefined'; | ||
}); | ||
return this.schemas[ourUri]; | ||
@@ -144,3 +113,3 @@ }; | ||
// This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI | ||
var base = urilib.resolve(options.base||'/', schema.id||''); | ||
var base = urilib.resolve(options.base||anonymousBase, schema.id||''); | ||
if(!ctx){ | ||
@@ -151,2 +120,7 @@ ctx = new SchemaContext(schema, options, propertyName, base, Object.create(this.schemas)); | ||
} | ||
var found = scanSchema(base, schema); | ||
for(var n in found.id){ | ||
var sch = found.id[n]; | ||
ctx.schemas[n] = sch; | ||
} | ||
} | ||
@@ -184,3 +158,14 @@ if (schema) { | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (!schema) { | ||
// Support for the true/false schemas | ||
if(typeof schema==='boolean') { | ||
if(schema===true){ | ||
// `true` is always valid | ||
schema = {}; | ||
}else if(schema===false){ | ||
// `false` is always invalid | ||
schema = {type: []}; | ||
} | ||
}else if(!schema){ | ||
// This might be a string | ||
throw new Error("schema is undefined"); | ||
@@ -202,2 +187,3 @@ } | ||
// If passed a string argument, load that schema URI | ||
var switchSchema; | ||
@@ -204,0 +190,0 @@ if (switchSchema = shouldResolve(schema)) { |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"license": "MIT", | ||
@@ -15,3 +15,4 @@ "dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "~1.8.2", | ||
"json-metaschema": "^1.2.0", | ||
"mocha": "~3", | ||
"chai": "~1.5.0" | ||
@@ -18,0 +19,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
63509
3
10
1548