Socket
Socket
Sign inDemoInstall

is-my-json-valid

Package Overview
Dependencies
Maintainers
2
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-my-json-valid - npm Package Compare versions

Comparing version 2.6.0 to 2.7.0

39

index.js

@@ -90,2 +90,3 @@ var genobj = require('generate-object-property')

var scope = {unique:unique, formats:fmts}
var verbose = opts ? !!opts.verbose : false;

@@ -128,14 +129,15 @@ var syms = {}

var indent = 0
var error = function(msg) {
if (reporter === false) {
validate('errors++')
return
var error = function(msg, prop) {
validate('errors++')
if (reporter === true) {
validate('if (validate.errors === null) validate.errors = []')
if (verbose) {
validate('validate.errors.push({field:%s,message:%s,value:%s})', JSON.stringify(formatName(prop || name)), JSON.stringify(msg), name)
}
else {
var n = gensym('error')
scope[n] = {field:formatName(prop || name), message:msg}
validate('validate.errors.push(%s)', n)
}
}
var n = gensym('error')
scope[n] = {field:formatName(name), message:msg}
validate
('errors++')
('if (validate.errors === null) validate.errors = []')
('validate.errors.push(%s)', n)
}

@@ -194,5 +196,14 @@

validate('if ((%s) && (%s)) {', type !== 'object' ? types.object(name) : 'true', node.required.map(isUndefined).join(' || ') || 'false')
error('missing required properties')
validate('} else {')
var checkRequired = function (req) {
var prop = genobj(name, req);
validate('if (%s === undefined) {', prop)
error('is required', prop)
validate('missing++')
validate('}')
}
validate('if ((%s)) {', type !== 'object' ? types.object(name) : 'true')
validate('var missing = 0')
node.required.map(checkRequired)
validate('}');
validate('if (missing === 0) {')
indent++

@@ -199,0 +210,0 @@ }

{
"name": "is-my-json-valid",
"version": "2.6.0",
"version": "2.7.0",
"description": "A JSONSchema validator that uses code generation to be extremely fast",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -112,2 +112,24 @@ # is-my-json-valid

## Verbose mode outputs the value on errors
is-my-json-valid outputs the value causing an error when verbose is set to true
``` js
var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
}, {
verbose: true
})
validate({hello: 100});
console.log(validate.errors) // {field: 'data.hello', message: 'is the wrong type', value: 100}
```
## Performance

@@ -114,0 +136,0 @@

@@ -244,3 +244,24 @@ var tape = require('tape')

t.notOk(validate({}), 'should not be valid')
t.ok(validate.errors[0].field === 'data.x', 'should output the missing field')
t.end()
})
})
tape('verbose mode', function(t) {
var schema = {
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
};
var validate = validator(schema, {verbose: true})
t.ok(validate({hello: 'string'}), 'should be valid')
t.notOk(validate({hello: 100}), 'should not be valid')
t.ok(validate.errors[0].value === 100, 'error object should contain the invalid value')
t.end()
})
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