Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

normalize-json

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-json - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

14

normalize-json.js

@@ -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);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc