Comparing version 0.0.10 to 0.0.11
@@ -29,2 +29,4 @@ 'use strict'; | ||
if (this.opts.meta !== false) | ||
addSchema(require('./refs/json-schema-draft-04.json')); | ||
@@ -39,4 +41,4 @@ /** | ||
function validate(schema, data) { | ||
var compiled = _addSchema(schema); | ||
return compiled.validate(data, self); | ||
var v = _addSchema(schema); | ||
return v(data); | ||
} | ||
@@ -51,6 +53,3 @@ | ||
function compile(schema) { | ||
var compiled = _addSchema(schema); | ||
return function validate(data) { | ||
return compiled.validate(data); | ||
}; | ||
return _addSchema(schema); | ||
} | ||
@@ -70,6 +69,6 @@ | ||
throw new Error('schema already exists'); | ||
var compiled = _addSchema(schema); | ||
self._schemas[id] = compiled; | ||
self._schemas[schema.id] = compiled; | ||
return compiled; | ||
var validate = _addSchema(schema); | ||
if (id) self._schemas[id] = validate; | ||
if (schema.id) self._schemas[schema.id] = validate; | ||
return validate; | ||
} | ||
@@ -76,0 +75,0 @@ |
@@ -10,4 +10,2 @@ 'use strict'; | ||
var RULES = require('./rules') | ||
, _validateTemplateStr = fs.readFileSync(__dirname + '/_validate.dot.js') | ||
, _validateTemplate = doT.compile(_validateTemplateStr) | ||
, validateTemplateStr = fs.readFileSync(__dirname + '/validate.dot.js') | ||
@@ -25,3 +23,3 @@ , validateTemplate = doT.compile(validateTemplateStr); | ||
RULES: RULES, | ||
_validate: _validateTemplate, | ||
validate: validateTemplate, | ||
copy: copy, | ||
@@ -36,10 +34,7 @@ resolveRef: resolveRef, | ||
var validate; | ||
eval(validateCode); | ||
eval('validate = ' + validateCode); | ||
var compiled = { | ||
validate: validate, | ||
schema: schema, | ||
}; | ||
validate.schema = schema; | ||
return compiled; | ||
return validate; | ||
@@ -52,4 +47,4 @@ function resolveRef(ref) { | ||
return ref == '#' | ||
? compiled.validate(data) | ||
: self._schemas[ref].validate(data); | ||
? validate(data) | ||
: self._schemas[ref](data); | ||
} | ||
@@ -56,0 +51,0 @@ } |
@@ -13,3 +13,3 @@ function (data, dataType, dataPath) { | ||
var result = ({{= it._validate($it) }})(data, dataType, dataPath); | ||
var result = ({{= it.validate($it) }})(data, dataType, dataPath); | ||
@@ -16,0 +16,0 @@ if (!result.valid) { |
@@ -13,3 +13,3 @@ function (data, dataType, dataPath) { | ||
var result = ({{= it._validate($it) }})(data, dataType, dataPath); | ||
var result = ({{= it.validate($it) }})(data, dataType, dataPath); | ||
@@ -16,0 +16,0 @@ if (result.valid) { |
@@ -24,3 +24,3 @@ function (data, dataType, dataPath) { | ||
keyword: 'dependencies', | ||
schema: self.schema{{= it.schemaPath }}, | ||
schema: validate.schema{{= it.schemaPath }}, | ||
dataPath: dataPath, | ||
@@ -49,3 +49,3 @@ message: '{{? $deps.length == 1 }}property {{= $deps[0] }} is{{??}}properties {{= $deps.join(",") }} are{{?}} required when property {{= $property }} is present' | ||
var result = ({{= it._validate($it) }})(data, dataType, dataPath); | ||
var result = ({{= it.validate($it) }})(data, dataType, dataPath); | ||
@@ -52,0 +52,0 @@ if (!result.valid) { |
@@ -20,3 +20,3 @@ function (data, dataType, dataPath) { | ||
keyword: 'enum', | ||
schema: self.schema{{= it.schemaPath }}, | ||
schema: validate.schema{{= it.schemaPath }}, | ||
dataPath: dataPath, | ||
@@ -23,0 +23,0 @@ message: 'should be equal to one of values' |
@@ -53,3 +53,3 @@ {{## def.validateItems:startFrom: | ||
, _dataPath = dataPath + '[{{= $index }}]' | ||
, result = ({{= it._validate($it) }})(_data, _dataType, _dataPath); | ||
, result = ({{= it.validate($it) }})(_data, _dataType, _dataPath); | ||
@@ -75,3 +75,3 @@ if (!result.valid) { | ||
if (data.length > {{= it.schema.length }}) { | ||
var validateItems = ({{= it._validate($it) }}); | ||
var validateItems = ({{= it.validate($it) }}); | ||
{{# def.validateItems: it.schema.length }} | ||
@@ -81,3 +81,3 @@ } | ||
{{?? Object.keys(it.schema).length }} | ||
var validateItems = ({{= it._validate(it) }}); | ||
var validateItems = ({{= it.validate(it) }}); | ||
{{# def.validateItems: 0 }} | ||
@@ -84,0 +84,0 @@ {{?}} |
{{ | ||
var $schema = it.schema.maximum | ||
, $exclusive = it.schema.exclusiveMaximum === true | ||
var $exclusive = it.schema.exclusiveMaximum === true | ||
, $op = $exclusive ? '<' : '<='; | ||
}} | ||
var valid = data {{= $op }} {{= $schema }}; | ||
var valid = data {{= $op }} {{= it.schema.maximum }}; | ||
var result = { | ||
@@ -12,7 +11,7 @@ valid: valid, | ||
keyword: 'maximum', | ||
schema: {{= $schema }}, | ||
schema: {{= it.schema.maximum }}, | ||
dataPath: dataPath, | ||
message: 'should be {{= $op }} {{= $schema }}' | ||
message: 'should be {{= $op }} {{= it.schema.maximum }}' | ||
{{? it.opts.verbose }}, data: data{{?}} | ||
}] | ||
}; |
function (data, dataType, dataPath) { | ||
'use strict'; | ||
var result = ({{= it._validate(it) }})(data, dataType, dataPath); | ||
var result = ({{= it.validate(it) }})(data, dataType, dataPath); | ||
result.valid = !result.valid; | ||
result.errors = result.valid ? [] : [{ | ||
keyword: 'not', | ||
schema: self.schema{{= it.schemaPath }}, | ||
schema: validate.schema{{= it.schemaPath }}, | ||
dataPath: dataPath, | ||
@@ -10,0 +10,0 @@ message: 'should NOT be valid' |
@@ -13,3 +13,3 @@ function (data, dataType, dataPath) { | ||
var result = ({{= it._validate($it) }})(data, dataType, dataPath); | ||
var result = ({{= it.validate($it) }})(data, dataType, dataPath); | ||
@@ -19,3 +19,3 @@ if (result.valid) { | ||
keyword: 'oneOf', | ||
schema: self.schema{{= it.schemaPath }}, | ||
schema: validate.schema{{= it.schemaPath }}, | ||
dataPath: dataPath, | ||
@@ -33,3 +33,3 @@ message: 'should match exactly one schema in oneOf' | ||
keyword: 'oneOf', | ||
schema: self.schema{{= it.schemaPath }}, | ||
schema: validate.schema{{= it.schemaPath }}, | ||
dataPath: dataPath, | ||
@@ -36,0 +36,0 @@ message: 'should match exactly one schema in oneOf' |
@@ -5,3 +5,3 @@ {{## def.validateProperty:useKey: | ||
, _dataPath = dataPath + '.' + {{= useKey }} | ||
, result = ({{= it._validate($it) }})(_data, _dataType, _dataPath); | ||
, result = ({{= it.validate($it) }})(_data, _dataType, _dataPath); | ||
@@ -37,3 +37,3 @@ if (!result.valid) { | ||
{{? $checkAdditional }} | ||
var propertiesSchema = self.schema{{= it.schemaPath }} || {}; | ||
var propertiesSchema = validate.schema{{= it.schemaPath }} || {}; | ||
{{?}} | ||
@@ -65,3 +65,3 @@ | ||
{{? $pPropertyKeys.length }} | ||
var pPropertiesSchema = self.schema{{= it.parentSchemaPath + '.patternProperties' }} | ||
var pPropertiesSchema = validate.schema{{= it.parentSchemaPath + '.patternProperties' }} | ||
, pPropertiesRegexps = {} | ||
@@ -68,0 +68,0 @@ , dataKeysPPs; /* map of arrays of applicable pattern properties */ |
var valid = true, missing = ''; | ||
var schema = self.schema{{= it.schemaPath + '.required' }}; | ||
var req_schema = validate.schema{{= it.schemaPath + '.required' }}; | ||
for (var i = 0; i < schema.length; i++) { | ||
var property = schema[i] | ||
, has = data.hasOwnProperty(schema[i]) | ||
for (var i = 0; i < req_schema.length; i++) { | ||
var property = req_schema[i] | ||
, has = data.hasOwnProperty(req_schema[i]) | ||
, valid = valid && has; | ||
@@ -27,3 +27,3 @@ | ||
keyword: 'required', | ||
schema: self.schema{{= it.schemaPath + '.required' }}, | ||
schema: req_schema, | ||
dataPath: dataPath, | ||
@@ -30,0 +30,0 @@ message: 'properties ' + missing + ' are missing' |
@@ -49,3 +49,3 @@ {{## def.isInteger: | ||
keyword: 'type', | ||
schema: {{? $isArray }}self.schema{{= it.schemaPath + '.type' }}{{??}}'{{= $schema }}'{{?}}, | ||
schema: {{? $isArray }}validate.schema{{= it.schemaPath + '.type' }}{{??}}'{{= $schema }}'{{?}}, | ||
dataPath: dataPath, | ||
@@ -52,0 +52,0 @@ message: 'data' + dataPath + ' is not valid. Expected {{? $isArray }}{{= $schema.join(",") }}{{??}}{{= $schema }}{{?}}' |
@@ -1,12 +0,81 @@ | ||
'use strict'; | ||
{{ /** | ||
* schema compilation (render) time: | ||
* it = { schema, RULES, _validate, opts } | ||
* getDataType is defined in the parent scope in index.js | ||
* it._validate - this template function | ||
* | ||
* runtime: | ||
* RULES, copy are defined in the parent scope in index.js | ||
*/ }} | ||
validate = function(data, dataType, dataPath) { | ||
var self = this; | ||
var dataType = dataType || getDataType(data); | ||
return ({{= it._validate(it) }})(data, dataType, dataPath ||''); | ||
{{ | ||
function compareRules(key1, key2) { | ||
var rule1 = it.RULES[key1] | ||
, rule2 = it.RULES[key2] | ||
, order1 = rule1 && rule1.order || 0 | ||
, order2 = rule2 && rule2.order || 0; | ||
if (order1 < order2) return -1; | ||
if (order1 > order2) return +1; | ||
if (order1 == order2) return 0; | ||
} | ||
}} | ||
function (data, dataType, dataPath) { | ||
'use strict'; | ||
dataType = dataType || getDataType(data); | ||
dataPath = dataPath || ''; | ||
{{? it.opts.allErrors }} | ||
var errors = []; | ||
{{?}} | ||
{{ | ||
var $schemaKeys = Object.keys(it.schema); | ||
$schemaKeys.sort(compareRules); | ||
}} | ||
{{ | ||
var $checkProperties = !it.schema.properties && | ||
( it.schema.patternProperties || | ||
it.schema.hasOwnProperty('additionalProperties') ); | ||
if ($checkProperties) $schemaKeys.push('properties'); | ||
}} | ||
{{~ $schemaKeys:$key }} | ||
{{ var $rule = it.RULES[$key]; }} | ||
{{? $rule }} | ||
var rule = RULES.{{=$key}}; | ||
/* check if rule applies to data type */ | ||
if ( !rule.type || rule.type == dataType ) { | ||
{{? $rule.inline}} | ||
{{= $rule.code(it) }} | ||
{{??}} | ||
{{ | ||
var $it = it.copy(it); | ||
$it.schema = it.schema[$key]; | ||
$it.schemaPath = it.schemaPath + '.' + $key; | ||
$it.parentSchema = it.schema; | ||
$it.parentSchemaPath = it.schemaPath; | ||
}} | ||
var result = ({{= $rule.code($it) }})(data, dataType, dataPath); | ||
{{?}} | ||
if (!result.valid) { | ||
{{? it.opts.allErrors }} | ||
errors.push.apply(errors, result.errors); | ||
{{??}} | ||
return result; | ||
{{?}} | ||
} | ||
} | ||
{{?}} | ||
{{~}} | ||
{{? it.opts.allErrors }} | ||
return { valid: !errors.length, errors: errors }; | ||
{{??}} | ||
return { valid: true, errors: [] }; | ||
{{?}} | ||
} |
{ | ||
"name": "ajv", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -7,5 +7,6 @@ 'use strict'; | ||
describe('Schema compilation', function() { | ||
it('works', function() { | ||
compileSchema.call(mockInstance, { type: 'string' }); | ||
it.skip('works', function() { | ||
var compiled = compileSchema.call(mockInstance, { type: 'string' }); | ||
console.log(compiled.validate.toString()); | ||
}); | ||
}); |
@@ -8,3 +8,3 @@ 'use strict'; | ||
var ONLY_RULES; | ||
var ONLY_RULES, SKIP_RULES; | ||
// ONLY_RULES = [ | ||
@@ -23,3 +23,8 @@ // 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum', | ||
SKIP_RULES = [ | ||
'refRemote', | ||
'optional/zeroTerminatedFloats' | ||
]; | ||
var Ajv = require('../lib/ajv') | ||
@@ -29,3 +34,15 @@ , ajv = Ajv() | ||
describe.only('JSON-Schema tests', function () { | ||
var remoteRefs = { | ||
'http://localhost:1234/integer.json': require('../JSON-Schema-Test-Suite/remotes/integer.json'), | ||
'http://localhost:1234/subSchemas.json': require('../JSON-Schema-Test-Suite/remotes/subSchemas.json'), | ||
'http://localhost:1234/folder/folderInteger.json': require('../JSON-Schema-Test-Suite/remotes/folder/folderInteger.json') | ||
}; | ||
for (var id in remoteRefs) { | ||
ajv.addSchema(remoteRefs[id], id); | ||
fullAjv.addSchema(remoteRefs[id], id); | ||
} | ||
describe('JSON-Schema tests', function () { | ||
var testsPath = path.join(__dirname, '..', TESTS_PATH); | ||
@@ -36,2 +53,4 @@ var files = getTestFilesRecursive(testsPath); | ||
if (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) return; | ||
if (SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0) return; | ||
describe(file.name, function() { | ||
@@ -38,0 +57,0 @@ var testSets = require(file.path); |
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
191459
5970
105