Comparing version 0.1.3 to 0.1.4
@@ -25,4 +25,6 @@ 'use strict'; | ||
copy: copy, | ||
toHash: toHash, | ||
resolveRef: resolveRef, | ||
getDataType: getDataType, | ||
checkDataType: checkDataType, | ||
checkDataTypes: checkDataTypes, | ||
escapeQuotes: escapeQuotes, | ||
@@ -58,2 +60,9 @@ stableStringify: stableStringify, | ||
function copy(o, to) { | ||
to = to || {}; | ||
for (var key in o) to[key] = o[key]; | ||
return to; | ||
} | ||
function getDataType(data) { | ||
@@ -66,11 +75,49 @@ if (data === null) return 'null'; | ||
function copy(o, to) { | ||
to = to || {}; | ||
for (var key in o) to[key] = o[key]; | ||
return to; | ||
function checkDataType(dataType) { | ||
switch (dataType) { | ||
case 'null': return 'data === null'; | ||
case 'array': return 'Array.isArray(data)'; | ||
case 'object': return '(data && typeof data == "object" && !Array.isArray(data))'; | ||
// case 'object': return 'Object.prototype.toString.call(data).slice(8,-1) == "Object"'; | ||
case 'integer': return '(typeof data == "number" && !(data % 1))' | ||
default: return 'typeof data == "' + dataType + '"'; | ||
} | ||
} | ||
function checkDataTypes(dataTypes) { | ||
switch (dataTypes.length) { | ||
case 0: return 'true'; | ||
case 1: return checkDataType(dataTypes[0]); | ||
default: | ||
var code = '' | ||
var types = toHash(dataTypes); | ||
if (types.array && types.object) { | ||
code = types.null ? '(': '(data && ' | ||
code += 'typeof data == "object")'; | ||
delete types.null; | ||
delete types.array; | ||
delete types.object; | ||
} | ||
if (types.number) delete types.integer; | ||
for (var t in types) | ||
code += (code ? '||' : '' ) + checkDataType(t); | ||
return code; | ||
} | ||
} | ||
function toHash(arr, func) { | ||
var hash = {}; | ||
arr.forEach(function (item) { | ||
if (func) item = func(item); | ||
hash[item] = true; | ||
}); | ||
return hash; | ||
} | ||
function escapeQuotes(str) { | ||
return str.replace(/"/g, '\\"'); | ||
} |
@@ -1,6 +0,6 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
{{? it.resolveRef(it.schema) }} | ||
var result = validateRef('{{= it.schema }}', data, dataType, dataPath); | ||
var result = validateRef('{{= it.schema }}', data, dataPath); | ||
if (!result.valid) validate.errors.push.apply(validate.errors, result.errors); | ||
@@ -7,0 +7,0 @@ return result.valid; |
@@ -1,2 +0,2 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -14,3 +14,3 @@ | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= it.validate($it) }})(data, dataType, dataPath); | ||
({{= it.validate($it) }})(data, dataPath); | ||
@@ -17,0 +17,0 @@ {{? !it.opts.allErrors }} if (!valid) return false; {{?}} |
@@ -1,2 +0,2 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -13,3 +13,3 @@ | ||
var valid = ({{= it.validate($it) }})(data, dataType, dataPath); | ||
var valid = ({{= it.validate($it) }})(data, dataPath); | ||
@@ -16,0 +16,0 @@ if (valid) { |
@@ -1,2 +0,2 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -44,3 +44,3 @@ | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= it.validate($it) }})(data, dataType, dataPath); | ||
({{= it.validate($it) }})(data, dataPath); | ||
@@ -47,0 +47,0 @@ {{? !it.opts.allErrors }} if (!valid) return false; {{?}} |
@@ -1,11 +0,6 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
/* TODO change to inline */ | ||
{{ | ||
var $itemsHash = {}; | ||
it.schema.forEach(function ($item) { | ||
$itemsHash[it.stableStringify($item)] = true; | ||
}); | ||
}} | ||
{{ var $itemsHash = it.toHash(it.schema, it.stableStringify); }} | ||
@@ -12,0 +7,0 @@ var itemsHash = {{= JSON.stringify($itemsHash) }}; |
{{## def.validateItems:startFrom: | ||
for (var i = {{= startFrom }}; i < data.length; i++) { | ||
var _data = data[i] | ||
, _dataType = getDataType(_data) | ||
, _dataPath = dataPath + '[' + i + ']'; | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
validateItems(_data, _dataType, _dataPath); | ||
validateItems(_data, _dataPath); | ||
@@ -14,3 +13,3 @@ {{? !it.opts.allErrors }} if (!valid) return false; {{?}} | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -45,7 +44,6 @@ | ||
var _data = data[{{= $index }}] | ||
, _dataType = getDataType(_data) | ||
, _dataPath = dataPath + '[{{= $index }}]'; | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= it.validate($it) }})(_data, _dataType, _dataPath); | ||
({{= it.validate($it) }})(_data, _dataPath); | ||
@@ -52,0 +50,0 @@ {{? !it.opts.allErrors }} if (!valid) return false; {{?}} |
@@ -1,2 +0,2 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -6,3 +6,3 @@ | ||
var valid = ({{= it.validate(it) }})(data, dataType, dataPath); | ||
var valid = ({{= it.validate(it) }})(data, dataPath); | ||
valid = !valid; | ||
@@ -9,0 +9,0 @@ |
@@ -1,2 +0,2 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -14,3 +14,3 @@ | ||
var valid = ({{= it.validate($it) }})(data, dataType, dataPath); | ||
var valid = ({{= it.validate($it) }})(data, dataPath); | ||
@@ -17,0 +17,0 @@ if (valid) { |
{{## def.validateProperty:useKey: | ||
var _data = data[{{= useKey }}] | ||
, _dataType = getDataType(_data) | ||
, _dataPath = dataPath + '.' + {{= useKey }}; | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= it.validate($it) }})(_data, _dataType, _dataPath); | ||
({{= it.validate($it) }})(_data, _dataPath); | ||
@@ -12,3 +11,3 @@ {{? !it.opts.allErrors }} if (!valid) return false; {{?}} | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -15,0 +14,0 @@ |
@@ -1,40 +0,7 @@ | ||
{{## def.isInteger: | ||
dataType == 'number' && !(data % 1) | ||
#}} | ||
{{ var $isArray = Array.isArray(it.schema.type); }} | ||
{{ | ||
var $schema = it.schema.type; | ||
var $isArray = Array.isArray($schema); | ||
if ($isArray && $schema.length == 1) { | ||
$schema = $schema[0]; | ||
$isArray = false; | ||
} | ||
}} | ||
var valid; | ||
{{? $isArray }} | ||
{{? $schema.indexOf('integer') >= 0 }} | ||
valid = {{~ $schema:$t }} | ||
{{? $t != 'integer' }} | ||
{{? $notFirst }} || {{?}} | ||
{{ var $notFirst = true; }} | ||
dataType == '{{=$t}}' | ||
{{?}} | ||
{{~}}; | ||
if (!valid) { | ||
valid = {{# def.isInteger }}; | ||
} | ||
{{??}} | ||
valid = {{~ $schema:$t:$i }} | ||
{{? $i }} || {{?}} | ||
dataType == '{{=$t}}' | ||
{{~}}; | ||
{{?}} | ||
var valid = {{= it.checkDataTypes(it.schema.type) }}; | ||
{{??}} | ||
valid = {{? $schema == 'integer' }} | ||
{{# def.isInteger }}; | ||
{{??}} | ||
dataType == '{{= $schema }}'; | ||
{{?}} | ||
var valid = {{= it.checkDataType(it.schema.type) }}; | ||
{{?}} | ||
@@ -46,4 +13,4 @@ | ||
dataPath: dataPath, | ||
message: 'should be {{? $isArray }}{{= $schema.join(",") }}{{??}}{{= $schema }}{{?}}' | ||
{{? it.opts.verbose }}, schema: {{? $isArray }}validate.schema{{= it.schemaPath + '.type' }}{{??}}'{{= $schema }}'{{?}}, data: data{{?}} | ||
message: 'should be {{? $isArray }}{{= it.schema.type.join(",") }}{{??}}{{= it.schema.type }}{{?}}' | ||
{{? it.opts.verbose }}, schema: {{? $isArray }}validate.schema{{= it.schemaPath + '.type' }}{{??}}'{{= it.schema.type }}'{{?}}, data: data{{?}} | ||
}); |
@@ -1,2 +0,2 @@ | ||
function (data, dataType, dataPath) { | ||
function (data, dataPath) { | ||
'use strict'; | ||
@@ -3,0 +3,0 @@ |
@@ -12,3 +12,3 @@ {{ /** | ||
function (data{{? !it.isRoot }}, dataType, dataPath{{?}}) { | ||
function (data{{? !it.isRoot }}, dataPath{{?}}) { | ||
'use strict'; | ||
@@ -18,3 +18,2 @@ | ||
{{ it.isRoot = false; }} | ||
var dataType = getDataType(data); | ||
var dataPath = ''; | ||
@@ -28,3 +27,3 @@ var errs = validate.errors.length = 0; | ||
{{? $shouldUseGroup($rulesGroup) }} | ||
{{? $rulesGroup.type }} if (dataType == '{{= $rulesGroup.type }}') { {{?}} | ||
{{? $rulesGroup.type }} if ({{= it.checkDataType($rulesGroup.type) }}) { {{?}} | ||
{{~ $rulesGroup.rules:$rule }} | ||
@@ -43,3 +42,3 @@ {{? $shouldUseRule($rule) }} | ||
{{? !it.opts.allErrors }} var valid = {{?}} | ||
({{= $rule.code($it) }})(data, dataType, dataPath); | ||
({{= $rule.code($it) }})(data, dataPath); | ||
{{?}} | ||
@@ -46,0 +45,0 @@ |
{ | ||
"name": "ajv", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -10,3 +10,4 @@ 'use strict'; | ||
// ONLY_RULES = [ | ||
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum', | ||
// 'type', | ||
// 'not', 'allOf', 'anyOf', 'oneOf', 'enum', | ||
// 'maximum', 'minimum', 'multipleOf', | ||
@@ -55,3 +56,3 @@ // 'maxLength', 'minLength', 'pattern', | ||
testSets.forEach(function (testSet) { | ||
// if (testSet.description != 'not') return; | ||
// if (testSet.description != 'multiple types can be specified in an array') return; | ||
describe(testSet.description, function() { | ||
@@ -63,3 +64,3 @@ // it(testSet.description, function() { | ||
testSet.tests.forEach(function (test) { | ||
// if (test.description != 'a float is not an integer') return; | ||
// if (test.description != 'an integer is valid') return; | ||
it(test.description, function() { | ||
@@ -66,0 +67,0 @@ var valid = validate(test.data); |
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
188867
5763