normalize-json
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -97,4 +97,10 @@ module.exports = function nJFactory(requirements) { | ||
if (!Array.isArray(value)) throw new Error(`${propName} is not an array.`); | ||
let problemIndex = value.findIndex((v,i)=>validateProperty(i, v, value, requirement.slice(1)) !== true); | ||
if (problemIndex !== -1) throw new Error(`"${propName}[${problemIndex}]" is invalid.`); | ||
let element = null; | ||
value.forEach((e,i) => { | ||
element = e; | ||
let result = validateProperty(i, element, value, requirement.slice(1)); | ||
if (result !== true) { | ||
throw new Error(`"${propName}[${i}]" is invalid: ${result}`); | ||
} | ||
}) | ||
return true; | ||
@@ -105,4 +111,4 @@ } | ||
if ((typeof type) === 'object' && !Array.isArray(type)) { | ||
let validation = validateObject(value, type) | ||
return validation.valid || new Error(validation.error); | ||
validateObject(value, type) | ||
return true; | ||
} | ||
@@ -109,0 +115,0 @@ |
{ | ||
"name": "normalize-json", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simple JSON validation/normalization using terse object literals", | ||
@@ -5,0 +5,0 @@ "main": "normalize-json.js", |
@@ -402,2 +402,46 @@ /* global describe it expect */ | ||
}) | ||
it('should validate functions within inner schemas', () => { | ||
let numberSchema = nJ({ | ||
'type': ['positive', 'negative'], | ||
'number': (num) => ((num.type === 'negative') ? [Number, -Infinity, 0] : [Number, 0, Infinity]) | ||
}); | ||
expect({'type':'positive','number':5}).toFitSchema(numberSchema); | ||
expect({'type':'positive','number':-5}).not.toFitSchema(numberSchema); | ||
expect({'type':'negative','number':-5}).toFitSchema(numberSchema); | ||
expect({'type':'negative','number':5}).not.toFitSchema(numberSchema); | ||
let outerSchema = nJ({ | ||
'inner': numberSchema.requirements | ||
}); | ||
expect({inner:{'type':'positive','number':5}}).toFitSchema(outerSchema); | ||
expect({inner:{'type':'positive','number':-5}}).not.toFitSchema(outerSchema); | ||
expect({inner:{'type':'negative','number':-5}}).toFitSchema(outerSchema); | ||
expect({inner:{'type':'negative','number':5}}).not.toFitSchema(outerSchema); | ||
}); | ||
it('should validate functions within objects of an array', () => { | ||
let numberSchema = nJ({ | ||
'type': ['positive', 'negative'], | ||
'number': (num) => ((num.type === 'negative') ? [Number, -Infinity, 0] : [Number, 0, Infinity]) | ||
}); | ||
expect({'type':'positive','number':5}).toFitSchema(numberSchema); | ||
expect({'type':'positive','number':-5}).not.toFitSchema(numberSchema); | ||
expect({'type':'negative','number':-5}).toFitSchema(numberSchema); | ||
expect({'type':'negative','number':5}).not.toFitSchema(numberSchema); | ||
let numberArraySchema = nJ({ | ||
'list': [Array, numberSchema.requirements] | ||
}); | ||
expect({list:[ | ||
{'type':'positive','number':5}, {'type':'negative', 'number':-5} | ||
]}).toFitSchema(numberArraySchema); | ||
expect({list:[ | ||
{'type':'positive','number':5}, {'type':'positive', 'number':-5} | ||
]}).not.toFitSchema(numberArraySchema); | ||
}); | ||
}); |
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
25273
535