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.9 to 0.4.10

46

lib/compile/index.js

@@ -18,10 +18,16 @@ 'use strict';

function compile(schema, _rootSchema) {
var self = this, refVal = [], refs = {};
_rootSchema = _rootSchema || schema;
function compile(schema, root) {
var self = this
, refVal = [ undefined ]
, refs = {};
var isRoot = !root || (root && root.schema == schema);
root = root || { schema: schema, refVal: refVal, refs: refs };
var formats = this._formats;
var validateCode = validateGenerator({
isRoot: true,
isTop: true,
schema: schema,
isRoot: isRoot,
root: root,
schemaPath: '',

@@ -48,2 +54,3 @@ errorPath: '""',

eval(validateCode);
refVal[0] = validate;
} catch(e) {

@@ -55,3 +62,6 @@ console.log('Error compiling schema, function code:', validateCode);

validate.schema = schema;
validate.errors = [];
validate.errors = null;
validate.refs = refs;
validate.refVal = refVal;
validate.root = isRoot ? validate : root;

@@ -63,13 +73,21 @@ return validate;

ref = resolve.url(baseId, ref);
if (refs[ref]) return refs[ref];
var v = resolve.call(self, compile, _rootSchema, ref);
if (v) {
var refId = refVal.length;
refVal.push(v);
refs[ref] = refId;
return refId;
} else {
// TODO register missing ref, create empty entry in refs and return id
if (refs[ref]) return 'refVal[' + refs[ref] + ']';
if (!isRoot) {
var rootRefId = root.refs[ref];
if (rootRefId !== undefined)
return addLocalRef(ref, root.refVal[rootRefId]);
else if (ref == '#' || ref =='#/')
return 'root.refVal[0]';
}
var v = resolve.call(self, compile, root, ref);
if (v)
return addLocalRef(ref, v);
}
function addLocalRef(ref, v) {
var refId = refVal.length;
refVal.push(v);
refs[ref] = refId;
return 'refVal[' + refId + ']';
}
}

@@ -76,0 +94,0 @@

@@ -15,7 +15,7 @@ 'use strict';

function resolve(compile, rootSchema, ref) {
function resolve(compile, root, ref) {
var refVal = this._refs[ref];
if (typeof refVal == 'string') {
if (this._refs[refVal]) refVal = this._refs[refVal];
else return resolve.call(this, compile, rootSchema, refVal);
else return resolve.call(this, compile, root, refVal);
}

@@ -25,11 +25,11 @@ if (typeof refVal == 'function') return refVal;

if (typeof refVal == 'function') return refVal;
var schema = _resolve.call(this, rootSchema, ref);
if (schema) return this._refs[ref] = compile.call(this, schema, rootSchema);
var schema = _resolve.call(this, root, ref);
if (schema) return this._refs[ref] = compile.call(this, schema, root);
};
function _resolve(rootSchema, ref) {
function _resolve(root, ref) {
var p = url.parse(ref, false, true)
, refPath = _getFullPath(p)
, baseId = getFullPath(rootSchema.id);
, baseId = getFullPath(root.schema.id);
if (refPath !== baseId) {

@@ -43,9 +43,9 @@ // rootSchema = undefined; TODO this breaks resolution in meta-schema

// }
if (typeof refVal == 'function') rootSchema = refVal.schema;
if (typeof refVal == 'function') replaceRoot(root, refVal);
else {
var refVal = this._schemas[normalizeId(refPath)];
if (typeof refVal == 'function') rootSchema = refVal.schema;
if (typeof refVal == 'function') replaceRoot(root, refVal);
}
if (!rootSchema) return;
baseId = getFullPath(rootSchema.id);
if (!root.schema) return;
baseId = getFullPath(root.schema.id);
}

@@ -55,3 +55,3 @@ p.hash = p.hash || '';

var parts = p.hash.split('/');
var schema = rootSchema;
var schema = root.schema;

@@ -67,10 +67,17 @@ for (var i = 1; i < parts.length; i++) {

var $ref = resolveUrl(baseId, schema.$ref);
schema = _resolve.call(this, rootSchema, $ref);
schema = _resolve.call(this, root, $ref);
}
}
}
if (schema != rootSchema) return schema;
if (schema != root.schema) return schema;
}
function replaceRoot(root, withRoot) {
root.schema = withRoot.schema;
root.refVal = withRoot.refVal;
root.refs = withRoot.refs;
}
function unescapeFragment(str) {

@@ -77,0 +84,0 @@ return decodeURIComponent(str)

@@ -5,12 +5,22 @@ {{# def.definitions }}

{{? $schema == '#' || $schema == '#/' }}
{{? $breakOnError && it.wasRoot }}
if (!validate({{=$data}}, (dataPath || '') + {{= it.errorPath }}))
return false;
else {
{{? it.isRoot }}
{{? $breakOnError && it.wasTop }}
if (!validate({{=$data}}, (dataPath || '') + {{= it.errorPath }}))
return false;
else {
{{??}}
var errors{{=$lvl}} = validate.errors;
if (!validate({{=$data}}, (dataPath || '') + {{= it.errorPath }})) {
if (errors{{=$lvl}} !== null) {
validate.errors = errors{{=$lvl}}.concat(validate.errors);
}
errors = validate.errors.length;
} {{? $breakOnError }} else { {{?}}
{{?}}
{{??}}
var errors{{=$lvl}} = validate.errors;
if (!validate({{=$data}}, (dataPath || '') + {{= it.errorPath }})) {
if (errors{{=$lvl}} !== null) {
validate.errors = errors{{=$lvl}}.concat(validate.errors);
}
{{ var $v = 'v' + $lvl; }}
var {{=$v}} = root.refVal[0];
if (!{{=$v}}({{=$data}}, (dataPath || '') + {{= it.errorPath }})) {
if (validate.errors === null) validate.errors = {{=$v}}.errors;
else validate.errors = validate.errors.concat({{=$v}}.errors);
errors = validate.errors.length;

@@ -20,10 +30,12 @@ } {{? $breakOnError }} else { {{?}}

{{??}}
{{ $id = it.resolveRef(it.baseId, $schema, it.rootId); }}
{{? $id === undefined }}
{{ var $refVal = it.resolveRef(it.baseId, $schema, it.rootId); }}
{{? $refVal === undefined }}
{{# def.error:'$ref' }}
{{? $breakOnError }} if (false) { {{?}}
{{??}}
if (!refVal[{{=$id}}]({{=$data}}, (dataPath || '') + {{= it.errorPath }})) {
if (validate.errors === null) validate.errors = refVal[{{=$id}}].errors;
else validate.errors.push.apply(validate.errors, refVal[{{=$id}}].errors);
{{ var $v = 'v' + $lvl; }}
var {{=$v}} = {{=$refVal}};
if (!{{=$v}}({{=$data}}, (dataPath || '') + {{= it.errorPath }})) {
if (validate.errors === null) validate.errors = {{=$v}}.errors;
else validate.errors = validate.errors.concat({{=$v}}.errors);
errors = validate.errors.length;

@@ -30,0 +42,0 @@ } {{? $breakOnError }} else { {{?}}

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

{{## def.error:_rule:
{{? it.wasRoot && $breakOnError }}
{{? it.wasTop && $breakOnError }}
validate.errors = [{{# def._error:_rule }}];

@@ -107,0 +107,0 @@ return false;

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

{{ for ($property in $propertyDeps) { }}
{{ for (var $property in $propertyDeps) { }}
if ({{=$data}}{{= it.util.getProperty($property) }} !== undefined) {

@@ -37,3 +37,3 @@ {{ $deps = $propertyDeps[$property]; }}

{{ for ($property in $schemaDeps) { }}
{{ for (var $property in $schemaDeps) { }}
{{ var $sch = $schemaDeps[$property]; }}

@@ -40,0 +40,0 @@ {{? {{# def.nonEmptySchema:$sch }} }}

@@ -14,11 +14,11 @@ {{# def.definitions }}

{{? it.isRoot}}
{{? it.isTop}}
{{
var $root = it.isRoot
var $top = it.isTop
, $lvl = it.level = 0
, $dataLvl = it.dataLevel = 0
, $data = 'data';
it.baseId = it.resolve.fullPath(it.schema.id);
delete it.isRoot;
it.wasRoot = true;
it.rootId = it.baseId = it.resolve.fullPath(it.schema.id);
delete it.isTop;
it.wasTop = true;
}}

@@ -39,3 +39,3 @@

if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
delete it.wasRoot;
delete it.wasTop;
}}

@@ -83,3 +83,3 @@

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

@@ -104,3 +104,3 @@ {{?}}

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

@@ -114,3 +114,3 @@ }

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

@@ -117,0 +117,0 @@ {{?}}

{
"name": "ajv",
"version": "0.4.9",
"version": "0.4.10",
"description": "Another JSON schema Validator",

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

@@ -9,5 +9,3 @@ 'use strict';

// ONLY_FILES = [
// 'type', 'not',
// 'allOf',
// 'anyOf', 'oneOf', 'enum',
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
// 'maximum', 'minimum', 'multipleOf', 'maxLength', 'minLength', 'pattern',

@@ -25,2 +23,3 @@ // 'properties', 'patternProperties', 'additionalProperties',

// 'schemas/advanced'
// 'issues/2_root_ref_in_ref'
// ];

@@ -52,3 +51,3 @@

describe('Schema validation tests', function() {
describe.only('Schema validation tests', function() {
addTests('JSON-Schema tests draft4', './json-schema-test-suite/tests/draft4/{**/,}*.json');

@@ -82,3 +81,3 @@ addTests('Advanced schema tests', './tests/{**/,}*.json');

testSet.tests.forEach(function (test) {
// if (test.description != 'valid object from z-schema benchmark') return;
// if (test.description != 'valid') return;
(test.skip ? it.skip : it)(test.description, function() {

@@ -85,0 +84,0 @@ var valid = validate(test.data);

[
{
"skip": true,
"description": "root ref in ref (#2)",

@@ -5,0 +4,0 @@ "schema": {

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc