jsonschema
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -415,10 +415,10 @@ 'use strict'; | ||
/** | ||
* Validates divisibleBy when the type of the instance value is a number. | ||
* Of course, this is susceptible to floating point error since it compares the floating points | ||
* and not the JSON byte sequences to arbitrary precision. | ||
* Perform validation for multipleOf and divisibleBy, which are essentially the same. | ||
* @param instance | ||
* @param schema | ||
* @return {String|null} | ||
* @param validationType | ||
* @param errorMessage | ||
* @returns {String|null} | ||
*/ | ||
validators.divisibleBy = function validateDivisibleBy (instance, schema, options, ctx) { | ||
var validateMultipleOfOrDivisbleBy = function validateMultipleOfOrDivisbleBy (instance, schema, options, ctx, validationType, errorMessage) { | ||
if (typeof instance !== 'number') { | ||
@@ -428,14 +428,23 @@ return null; | ||
if (schema.divisibleBy == 0) { | ||
throw new SchemaError("divisibleBy cannot be zero"); | ||
var validationArgument = schema[validationType]; | ||
if (validationArgument == 0) { | ||
throw new SchemaError(validationType + " cannot be zero"); | ||
} | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (instance / schema.divisibleBy % 1) { | ||
var instanceDecimals = helpers.getDecimalPlaces(instance); | ||
var divisorDecimals = helpers.getDecimalPlaces(validationArgument); | ||
var maxDecimals = Math.max(instanceDecimals , divisorDecimals); | ||
var multiplier = Math.pow(10, maxDecimals); | ||
if (Math.round(instance * multiplier) % Math.round(validationArgument * multiplier) !== 0) { | ||
result.addError({ | ||
name: 'divisibleBy', | ||
argument: schema.divisibleBy, | ||
message: "is not divisible by (multiple of) " + JSON.stringify(schema.divisibleBy), | ||
name: validationType, | ||
argument: validationArgument, | ||
message: errorMessage + JSON.stringify(validationArgument) | ||
}); | ||
} | ||
return result; | ||
@@ -446,4 +455,2 @@ }; | ||
* Validates divisibleBy when the type of the instance value is a number. | ||
* Of course, this is susceptible to floating point error since it compares the floating points | ||
* and not the JSON byte sequences to arbitrary precision. | ||
* @param instance | ||
@@ -454,19 +461,13 @@ * @param schema | ||
validators.multipleOf = function validateMultipleOf (instance, schema, options, ctx) { | ||
if (typeof instance !== 'number') { | ||
return null; | ||
} | ||
return validateMultipleOfOrDivisbleBy(instance, schema, options, ctx, "multipleOf", "is not a multiple of (divisible by) "); | ||
}; | ||
if (schema.multipleOf == 0) { | ||
throw new SchemaError("multipleOf cannot be zero"); | ||
} | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (instance / schema.multipleOf % 1) { | ||
result.addError({ | ||
name: 'multipleOf', | ||
argument: schema.multipleOf, | ||
message: "is not a multiple of (divisible by) " + JSON.stringify(schema.multipleOf), | ||
}); | ||
} | ||
return result; | ||
/** | ||
* Validates multipleOf when the type of the instance value is a number. | ||
* @param instance | ||
* @param schema | ||
* @return {String|null} | ||
*/ | ||
validators.divisibleBy = function validateDivisibleBy (instance, schema, options, ctx) { | ||
return validateMultipleOfOrDivisbleBy(instance, schema, options, ctx, "divisibleBy", "is not divisible by (multiple of) "); | ||
}; | ||
@@ -483,2 +484,3 @@ | ||
if (instance === undefined && schema.required === true) { | ||
// A boolean form is implemented for reverse-compatability with schemas written against older drafts | ||
result.addError({ | ||
@@ -781,2 +783,21 @@ name: 'required', | ||
/** | ||
* Validates whether the instance exactly matches a given value | ||
* | ||
* @param instance | ||
* @param schema | ||
* @return {ValidatorResult|null} | ||
*/ | ||
validators['const'] = function validateEnum (instance, schema, options, ctx) { | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
if (!helpers.deepCompareStrict(schema['const'], instance)) { | ||
result.addError({ | ||
name: 'const', | ||
argument: schema['const'], | ||
message: "does not exactly match expected constant: " + schema['const'], | ||
}); | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Validates whether the instance if of a prohibited type. | ||
@@ -783,0 +804,0 @@ * @param instance |
@@ -291,1 +291,36 @@ 'use strict'; | ||
}; | ||
/** | ||
* Calculate the number of decimal places a number uses | ||
* We need this to get correct results out of multipleOf and divisibleBy | ||
* when either figure is has decimal places, due to IEEE-754 float issues. | ||
* @param number | ||
* @returns {number} | ||
*/ | ||
exports.getDecimalPlaces = function getDecimalPlaces(number) { | ||
var decimalPlaces = 0; | ||
if (isNaN(number)) return decimalPlaces; | ||
if (typeof number !== 'number') { | ||
number = Number(number); | ||
} | ||
var parts = number.toString().split('e'); | ||
if (parts.length === 2) { | ||
if (parts[1][0] !== '-') { | ||
return decimalPlaces; | ||
} else { | ||
decimalPlaces = Number(parts[1].slice(1)); | ||
} | ||
} | ||
var decimalParts = parts[0].split('.'); | ||
if (decimalParts.length === 2) { | ||
decimalPlaces += decimalParts[1].length; | ||
} | ||
return decimalPlaces; | ||
}; | ||
@@ -52,2 +52,3 @@ /* | ||
$schema?: string | ||
$ref?: string | ||
title?: string | ||
@@ -98,2 +99,3 @@ description?: string | ||
base?: string; | ||
throwError?: boolean; | ||
} | ||
@@ -100,0 +102,0 @@ |
@@ -259,3 +259,3 @@ 'use strict'; | ||
* @return Object resolved schemas {subschema:String, switchSchema: String} | ||
* @thorws SchemaError | ||
* @throws SchemaError | ||
*/ | ||
@@ -319,3 +319,3 @@ Validator.prototype.resolve = function resolve (schema, switchSchema, ctx) { | ||
types.array = function testArray (instance) { | ||
return instance instanceof Array; | ||
return Array.isArray(instance); | ||
}; | ||
@@ -322,0 +322,0 @@ types['null'] = function testNull (instance) { |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"contributors": [ | ||
{ "name" : "Austin Wright" } | ||
{ | ||
"name": "Austin Wright" | ||
} | ||
], | ||
@@ -11,0 +12,0 @@ "main": "./lib", |
@@ -5,3 +5,3 @@ [![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.svg)](http://travis-ci.org/tdegrunt/jsonschema) | ||
[JSON schema](http://json-schema.org/) validator, which is designed to be fast and simple to use. | ||
The latest IETF published draft is v4, this library is mostly v4 compatible. | ||
The latest IETF published draft is v6, this library is mostly v4 compatible. | ||
@@ -8,0 +8,0 @@ ## Contributing & bugs |
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
73700
2003