Socket
Socket
Sign inDemoInstall

ajv

Package Overview
Dependencies
Maintainers
1
Versions
355
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv - npm Package Compare versions

Comparing version 0.4.3 to 0.4.4

5

lib/compile/rules/$ref.dot.js

@@ -19,5 +19,4 @@ {{# def.definitions }}

if (!refVal[{{=$id}}]({{=$data}}, (dataPath || '') + {{= it.errorPath }})) {
validate.errors = validate.errors === null
? refVal[{{=$id}}].errors
: validate.errors.concat(refVal[{{=$id}}].errors);
if (validate.errors === null) validate.errors = refVal[{{=$id}}].errors;
else validate.errors.push.apply(validate.errors, refVal[{{=$id}}].errors);
errors = validate.errors.length;

@@ -24,0 +23,0 @@ } {{? $breakOnError }} else { {{?}}

27

lib/compile/rules/definitions.def.js

@@ -54,3 +54,3 @@ {{## def.setup:_keyword:

{{## def.nonEmptySchema:_schema:
it.util.nonEmptySchema(_schema, it.RULES.hash)
it.util.schemaHasRules(_schema, it.RULES.all)
#}}

@@ -91,4 +91,7 @@

{{## def.error:_rule:
var err = {
{{## def.cleanUpVarErrors: {{ out = it.util.cleanUpVarErrors(out); }} #}}
{{## def._error:_rule:
{
keyword: '{{=_rule}}',

@@ -98,9 +101,19 @@ dataPath: {{= it.errorPath }},

{{? it.opts.verbose }}, schema: {{# def._errorSchemas[_rule] }}, data: {{=$data}}{{?}}
};
if (validate.errors === null) validate.errors = [err];
else validate.errors.push(err);
errors++;
}
#}}
{{## def.error:_rule:
{{? it.wasRoot && $breakOnError }}
validate.errors = [{{# def._error:_rule }}];
return false;
{{??}}
var err = {{# def._error:_rule }};
if (validate.errors === null) validate.errors = [err];
else validate.errors.push(err);
errors++;
{{?}}
#}}
{{## def.checkError:_rule:

@@ -107,0 +120,0 @@ if (!{{=$valid}}) {

'use strict';
var fs = require('fs')
, doT = require('dot');
, doT = require('dot')
, util = require('../util');

@@ -16,3 +17,3 @@ var defs = fs.readFileSync(__dirname + '/definitions.def.js');

{ type: 'object',
rules: [ 'maxProperties', 'minProperties', 'required', 'dependencies', 'properties' ] },
rules: [ 'maxProperties', 'minProperties', 'required', 'dependencies', 'properties' ] },
{ rules: [ '$ref', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] }

@@ -22,11 +23,7 @@ ];

RULES.defs = defs;
RULES.hash = {
type: true,
additionalProperties: true,
patternProperties: true
};
RULES.all = [ 'type', 'additionalProperties', 'patternProperties' ];
RULES.forEach(function (group) {
group.rules = group.rules.map(function (keyword) {
RULES.hash[keyword] = true;
RULES.all.push(keyword);
var template = fs.readFileSync(__dirname + '/' + keyword + '.dot.js');

@@ -40,1 +37,2 @@ return {

RULES.all = util.toHash(RULES.all);

@@ -28,3 +28,3 @@ {{# def.definitions }}

{{? $noAdditional && $pPropertyKeys.length === 0 }}
if (Object.keys({{=$data}}).length > Object.keys(propertiesSchema{{=$lvl}}).length) {
if (Object.keys({{=$data}}).length > {{= $schema ? Object.keys($schema).length : 0 }}) {
{{# def.error:'additionalProperties' }}

@@ -31,0 +31,0 @@ } {{# def.elseIfValid }}

@@ -24,4 +24,4 @@ {{# def.definitions }}

{{# def.checkError:'required' }}
{{? $breakOnError }} if ({{=$valid}}) { {{?}}
{{? $breakOnError }} else { {{?}}
{{?}}

@@ -16,3 +16,4 @@ 'use strict';

cleanUpCode: cleanUpCode,
nonEmptySchema: nonEmptySchema,
cleanUpVarErrors: cleanUpVarErrors,
schemaHasRules: schemaHasRules,
stableStringify: require('json-stable-stringify')

@@ -29,10 +30,16 @@ };

function checkDataType(dataType, data) {
// var data = 'data' + (lvl || '');
function checkDataType(dataType, data, negate) {
var EQUAL = negate ? ' !== ' : ' === '
, AND = negate ? ' || ' : ' && '
, OK = negate ? '!' : ''
, NOT = negate ? '' : '!';
switch (dataType) {
case 'null': return data + ' === null';
case 'array': return 'Array.isArray(' + data + ')';
case 'object': return '(' + data + ' && typeof ' + data + ' === "object" && !Array.isArray(' + data + '))';
case 'integer': return '(typeof ' + data + ' === "number" && !(' + data + ' % 1))'
default: return 'typeof ' + data + ' === "' + dataType + '"';
case 'null': return data + EQUAL + 'null';
case 'array': return OK + 'Array.isArray(' + data + ')';
case 'object': return '(' + OK + data + AND +
'typeof ' + data + EQUAL + '"object"' + AND +
NOT + 'Array.isArray(' + data + '))';
case 'integer': return '(typeof ' + data + EQUAL + '"number"' + AND +
NOT + '(' + data + ' % 1))'
default: return 'typeof ' + data + EQUAL + '"' + dataType + '"';
}

@@ -42,7 +49,10 @@ }

function checkDataTypes(dataTypes, data) {
// var data = 'data' + (lvl || '');
function checkDataTypes(dataTypes, data, negate) {
var EQUAL = negate ? ' !== ' : ' === '
, AND = negate ? ' || ' : ' && '
, OR = negate ? ' && ' : ' || '
, OK = negate ? '!' : '';
switch (dataTypes.length) {
case 0: return 'true';
case 1: return checkDataType(dataTypes[0], data);
case 0: return negate ? 'true' : 'false';
case 1: return checkDataType(dataTypes[0], data, negate);
default:

@@ -52,4 +62,4 @@ var code = ''

if (types.array && types.object) {
code = types.null ? '(': '(' + data + ' && '
code += 'typeof ' + data + ' === "object")';
code = types.null ? '(': '(' + OK + data + AND
code += 'typeof ' + data + EQUAL + '"object")';
delete types.null;

@@ -61,3 +71,3 @@ delete types.array;

for (var t in types)
code += (code ? '||' : '' ) + checkDataType(t, data);
code += (code ? OR : '' ) + checkDataType(t, data, negate);

@@ -134,4 +144,4 @@ return code;

, EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g;
function cleanUpCode(str) {
return str.replace(EMPTY_ELSE, '')
function cleanUpCode(out) {
return out.replace(EMPTY_ELSE, '')
.replace(EMPTY_IF_NO_ELSE, '')

@@ -142,4 +152,19 @@ .replace(EMPTY_IF_WITH_ELSE, 'if (!($1))');

function nonEmptySchema(schema, rules) {
var ERRORS_REGEXP = /[^\.]errors/g
, VAR_ERRORS = 'var errors = 0;'
, INITIALIZE_ERRORS = 'validate.errors = null;'
, RETURN_ERRORS = 'return errors === 0;'
function cleanUpVarErrors(out) {
var matches = out.match(ERRORS_REGEXP);
if (matches && matches.length === 2)
return out.replace(VAR_ERRORS, '')
.replace(INITIALIZE_ERRORS, '')
.replace(RETURN_ERRORS, INITIALIZE_ERRORS + ' return true;');
else
return out;
}
function schemaHasRules(schema, rules) {
for (var key in schema) if (rules[key]) return true;
}

@@ -22,2 +22,3 @@ {{# def.definitions }}

delete it.isRoot;
it.wasRoot = true;
}}

@@ -27,4 +28,4 @@

'use strict';
validate.errors = null;
var errors = 0;
validate.errors = null;{{ /* don't edit, used in replace */ }}
var errors = 0; {{ /* don't edit, used in replace */ }}
{{??}}

@@ -39,7 +40,8 @@ {{? it.opts._debug }} console.log('validate dataPath:', dataPath); {{?}}

if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
delete it.wasRoot;
}}
var errs_{{=$lvl}} = errors;
{{?}}
var errs_{{=$lvl}} = errors;
{{

@@ -82,3 +84,3 @@ var $valid = 'valid' + $lvl

{{? $breakOnError }}
if (errs_{{=$lvl}} === errors) {
if (errors === {{?$root}}0{{??}}errs_{{=$lvl}}{{?}}) {
{{ $closingBraces2 += '}'; }}

@@ -96,3 +98,3 @@ {{?}}

if (!({{= it.util[$method]($typeSchema, $data) }})) {
if ({{= it.util[$method]($typeSchema, $data, true) }}) {
{{# def.error:'type' }}

@@ -105,3 +107,3 @@ }

{{? $root }}
return errors === 0;
return errors === 0;{{ /* don't edit, used in replace */ }}
}

@@ -114,2 +116,6 @@ {{??}}

{{? $root && $breakOnError }}
{{# def.cleanUpVarErrors }}
{{?}}
{{

@@ -116,0 +122,0 @@ function $shouldUseGroup($rulesGroup) {

{
"name": "ajv",
"version": "0.4.3",
"version": "0.4.4",
"description": "Another JSON schema Validator",

@@ -5,0 +5,0 @@ "main": "lib/ajv.js",

@@ -74,2 +74,3 @@ 'use strict';

validate = ajv.compile(testSet.schema);
// console.log('validate', validate.toString());
fullValidate = fullAjv.compile(testSet.schema);

@@ -80,7 +81,5 @@ });

// if (test.description != 'valid object from z-schema benchmark') return;
// console.log(testSet.schema, '\n\n***\n\n', validate.toString());
it(test.description, function() {
var valid = validate(test.data);
// console.log('result', valid, test.valid, validate.errors);
// console.log('validate', validate.toString());
assert.equal(valid, test.valid);

@@ -87,0 +86,0 @@ if (valid) assert(validate.errors === null);

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