removed dataType parameter
parent
46cf5f9930
commit
0021032091
|
@ -22,8 +22,10 @@ function compile(schema) {
|
||||||
RULES: RULES,
|
RULES: RULES,
|
||||||
validate: validateTemplate,
|
validate: validateTemplate,
|
||||||
copy: copy,
|
copy: copy,
|
||||||
|
toHash: toHash,
|
||||||
resolveRef: resolveRef,
|
resolveRef: resolveRef,
|
||||||
getDataType: getDataType,
|
checkDataType: checkDataType,
|
||||||
|
checkDataTypes: checkDataTypes,
|
||||||
escapeQuotes: escapeQuotes,
|
escapeQuotes: escapeQuotes,
|
||||||
stableStringify: stableStringify,
|
stableStringify: stableStringify,
|
||||||
opts: this.opts
|
opts: this.opts
|
||||||
|
@ -54,13 +56,6 @@ function compile(schema) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function getDataType(data) {
|
|
||||||
if (data === null) return 'null';
|
|
||||||
if (Array.isArray(data)) return 'array';
|
|
||||||
return typeof data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function copy(o, to) {
|
function copy(o, to) {
|
||||||
to = to || {};
|
to = to || {};
|
||||||
for (var key in o) to[key] = o[key];
|
for (var key in o) to[key] = o[key];
|
||||||
|
@ -68,6 +63,58 @@ function copy(o, to) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getDataType(data) {
|
||||||
|
if (data === null) return 'null';
|
||||||
|
if (Array.isArray(data)) return 'array';
|
||||||
|
return typeof data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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) {
|
function escapeQuotes(str) {
|
||||||
return str.replace(/"/g, '\\"');
|
return str.replace(/"/g, '\\"');
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
{{? it.resolveRef(it.schema) }}
|
{{? 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);
|
if (!result.valid) validate.errors.push.apply(validate.errors, result.errors);
|
||||||
return result.valid;
|
return result.valid;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||||
|
@ -11,7 +11,7 @@ function (data, dataType, dataPath) {
|
||||||
}}
|
}}
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} var valid = {{?}}
|
{{? !it.opts.allErrors }} var valid = {{?}}
|
||||||
({{= it.validate($it) }})(data, dataType, dataPath);
|
({{= it.validate($it) }})(data, dataPath);
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
||||||
{{~}}
|
{{~}}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var errs = validate.errors.length;
|
var errs = validate.errors.length;
|
||||||
|
@ -10,7 +10,7 @@ function (data, dataType, dataPath) {
|
||||||
$it.schemaPath = it.schemaPath + '[' + $i + ']';
|
$it.schemaPath = it.schemaPath + '[' + $i + ']';
|
||||||
}}
|
}}
|
||||||
|
|
||||||
var valid = ({{= it.validate($it) }})(data, dataType, dataPath);
|
var valid = ({{= it.validate($it) }})(data, dataPath);
|
||||||
|
|
||||||
if (valid) {
|
if (valid) {
|
||||||
validate.errors.length = errs;
|
validate.errors.length = errs;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||||
|
@ -41,7 +41,7 @@ function (data, dataType, dataPath) {
|
||||||
}}
|
}}
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} var valid = {{?}}
|
{{? !it.opts.allErrors }} var valid = {{?}}
|
||||||
({{= it.validate($it) }})(data, dataType, dataPath);
|
({{= it.validate($it) }})(data, dataPath);
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,8 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
/* TODO change to inline */
|
/* TODO change to inline */
|
||||||
|
|
||||||
{{
|
{{ var $itemsHash = it.toHash(it.schema, it.stableStringify); }}
|
||||||
var $itemsHash = {};
|
|
||||||
it.schema.forEach(function ($item) {
|
|
||||||
$itemsHash[it.stableStringify($item)] = true;
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
|
|
||||||
var itemsHash = {{= JSON.stringify($itemsHash) }};
|
var itemsHash = {{= JSON.stringify($itemsHash) }};
|
||||||
var valid = itemsHash[stableStringify(data)];
|
var valid = itemsHash[stableStringify(data)];
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
{{## def.validateItems:startFrom:
|
{{## def.validateItems:startFrom:
|
||||||
for (var i = {{= startFrom }}; i < data.length; i++) {
|
for (var i = {{= startFrom }}; i < data.length; i++) {
|
||||||
var _data = data[i]
|
var _data = data[i]
|
||||||
, _dataType = getDataType(_data)
|
|
||||||
, _dataPath = dataPath + '[' + i + ']';
|
, _dataPath = dataPath + '[' + i + ']';
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} var valid = {{?}}
|
{{? !it.opts.allErrors }} var valid = {{?}}
|
||||||
validateItems(_data, _dataType, _dataPath);
|
validateItems(_data, _dataPath);
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
||||||
}
|
}
|
||||||
#}}
|
#}}
|
||||||
|
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
|
||||||
|
@ -41,11 +40,10 @@ function (data, dataType, dataPath) {
|
||||||
}}
|
}}
|
||||||
|
|
||||||
var _data = data[{{= $index }}]
|
var _data = data[{{= $index }}]
|
||||||
, _dataType = getDataType(_data)
|
|
||||||
, _dataPath = dataPath + '[{{= $index }}]';
|
, _dataPath = dataPath + '[{{= $index }}]';
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} var valid = {{?}}
|
{{? !it.opts.allErrors }} var valid = {{?}}
|
||||||
({{= it.validate($it) }})(_data, _dataType, _dataPath);
|
({{= it.validate($it) }})(_data, _dataPath);
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var errs = validate.errors.length;
|
var errs = validate.errors.length;
|
||||||
|
|
||||||
var valid = ({{= it.validate(it) }})(data, dataType, dataPath);
|
var valid = ({{= it.validate(it) }})(data, dataPath);
|
||||||
valid = !valid;
|
valid = !valid;
|
||||||
|
|
||||||
if (valid) validate.errors.length = errs;
|
if (valid) validate.errors.length = errs;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var foundValid = false
|
var foundValid = false
|
||||||
|
@ -11,7 +11,7 @@ function (data, dataType, dataPath) {
|
||||||
$it.schemaPath = it.schemaPath + '[' + $i + ']';
|
$it.schemaPath = it.schemaPath + '[' + $i + ']';
|
||||||
}}
|
}}
|
||||||
|
|
||||||
var valid = ({{= it.validate($it) }})(data, dataType, dataPath);
|
var valid = ({{= it.validate($it) }})(data, dataPath);
|
||||||
|
|
||||||
if (valid) {
|
if (valid) {
|
||||||
if (foundValid) {
|
if (foundValid) {
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
{{## def.validateProperty:useKey:
|
{{## def.validateProperty:useKey:
|
||||||
var _data = data[{{= useKey }}]
|
var _data = data[{{= useKey }}]
|
||||||
, _dataType = getDataType(_data)
|
|
||||||
, _dataPath = dataPath + '.' + {{= useKey }};
|
, _dataPath = dataPath + '.' + {{= useKey }};
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} var valid = {{?}}
|
{{? !it.opts.allErrors }} var valid = {{?}}
|
||||||
({{= it.validate($it) }})(_data, _dataType, _dataPath);
|
({{= it.validate($it) }})(_data, _dataPath);
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
||||||
#}}
|
#}}
|
||||||
|
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var errs = validate.errors.length;
|
var errs = validate.errors.length;
|
||||||
|
|
|
@ -1,48 +1,15 @@
|
||||||
{{## def.isInteger:
|
{{ var $isArray = Array.isArray(it.schema.type); }}
|
||||||
dataType == 'number' && !(data % 1)
|
|
||||||
#}}
|
|
||||||
|
|
||||||
|
|
||||||
{{
|
|
||||||
var $schema = it.schema.type;
|
|
||||||
var $isArray = Array.isArray($schema);
|
|
||||||
if ($isArray && $schema.length == 1) {
|
|
||||||
$schema = $schema[0];
|
|
||||||
$isArray = false;
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
|
|
||||||
var valid;
|
|
||||||
{{? $isArray }}
|
{{? $isArray }}
|
||||||
{{? $schema.indexOf('integer') >= 0 }}
|
var valid = {{= it.checkDataTypes(it.schema.type) }};
|
||||||
valid = {{~ $schema:$t }}
|
|
||||||
{{? $t != 'integer' }}
|
|
||||||
{{? $notFirst }} || {{?}}
|
|
||||||
{{ var $notFirst = true; }}
|
|
||||||
dataType == '{{=$t}}'
|
|
||||||
{{?}}
|
|
||||||
{{~}};
|
|
||||||
if (!valid) {
|
|
||||||
valid = {{# def.isInteger }};
|
|
||||||
}
|
|
||||||
{{??}}
|
|
||||||
valid = {{~ $schema:$t:$i }}
|
|
||||||
{{? $i }} || {{?}}
|
|
||||||
dataType == '{{=$t}}'
|
|
||||||
{{~}};
|
|
||||||
{{?}}
|
|
||||||
{{??}}
|
{{??}}
|
||||||
valid = {{? $schema == 'integer' }}
|
var valid = {{= it.checkDataType(it.schema.type) }};
|
||||||
{{# def.isInteger }};
|
|
||||||
{{??}}
|
|
||||||
dataType == '{{= $schema }}';
|
|
||||||
{{?}}
|
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
|
|
||||||
if (!valid) validate.errors.push({
|
if (!valid) validate.errors.push({
|
||||||
keyword: 'type',
|
keyword: 'type',
|
||||||
dataPath: dataPath,
|
dataPath: dataPath,
|
||||||
message: 'should be {{? $isArray }}{{= $schema.join(",") }}{{??}}{{= $schema }}{{?}}'
|
message: 'should be {{? $isArray }}{{= it.schema.type.join(",") }}{{??}}{{= it.schema.type }}{{?}}'
|
||||||
{{? it.opts.verbose }}, schema: {{? $isArray }}validate.schema{{= it.schemaPath + '.type' }}{{??}}'{{= $schema }}'{{?}}, data: data{{?}}
|
{{? it.opts.verbose }}, schema: {{? $isArray }}validate.schema{{= it.schemaPath + '.type' }}{{??}}'{{= it.schema.type }}'{{?}}, data: data{{?}}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
function (data, dataType, dataPath) {
|
function (data, dataPath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/* TODO change to inline ??? with break in the loop */
|
/* TODO change to inline ??? with break in the loop */
|
||||||
|
|
|
@ -9,12 +9,11 @@
|
||||||
* copy, getDataType etc. are defined in the parent scope in index.js
|
* copy, getDataType etc. are defined in the parent scope in index.js
|
||||||
*/ }}
|
*/ }}
|
||||||
|
|
||||||
function (data{{? !it.isRoot }}, dataType, dataPath{{?}}) {
|
function (data{{? !it.isRoot }}, dataPath{{?}}) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
{{? it.isRoot }}
|
{{? it.isRoot }}
|
||||||
{{ it.isRoot = false; }}
|
{{ it.isRoot = false; }}
|
||||||
var dataType = getDataType(data);
|
|
||||||
var dataPath = '';
|
var dataPath = '';
|
||||||
var errs = validate.errors.length = 0;
|
var errs = validate.errors.length = 0;
|
||||||
{{??}}
|
{{??}}
|
||||||
|
@ -23,7 +22,7 @@ function (data{{? !it.isRoot }}, dataType, dataPath{{?}}) {
|
||||||
|
|
||||||
{{~ it.RULES:$rulesGroup }}
|
{{~ it.RULES:$rulesGroup }}
|
||||||
{{? $shouldUseGroup($rulesGroup) }}
|
{{? $shouldUseGroup($rulesGroup) }}
|
||||||
{{? $rulesGroup.type }} if (dataType == '{{= $rulesGroup.type }}') { {{?}}
|
{{? $rulesGroup.type }} if ({{= it.checkDataType($rulesGroup.type) }}) { {{?}}
|
||||||
{{~ $rulesGroup.rules:$rule }}
|
{{~ $rulesGroup.rules:$rule }}
|
||||||
{{? $shouldUseRule($rule) }}
|
{{? $shouldUseRule($rule) }}
|
||||||
{{? $rule.inline }}
|
{{? $rule.inline }}
|
||||||
|
@ -37,7 +36,7 @@ function (data{{? !it.isRoot }}, dataType, dataPath{{?}}) {
|
||||||
$it.parentSchemaPath = it.schemaPath;
|
$it.parentSchemaPath = it.schemaPath;
|
||||||
}}
|
}}
|
||||||
{{? !it.opts.allErrors }} var valid = {{?}}
|
{{? !it.opts.allErrors }} var valid = {{?}}
|
||||||
({{= $rule.code($it) }})(data, dataType, dataPath);
|
({{= $rule.code($it) }})(data, dataPath);
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
{{? !it.opts.allErrors }} if (!valid) return false; {{?}}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "ajv",
|
"name": "ajv",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"description": "Another JSON schema Validator",
|
"description": "Another JSON schema Validator",
|
||||||
"main": "lib/ajv.js",
|
"main": "lib/ajv.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -7,7 +7,8 @@ var fs = require('fs')
|
||||||
|
|
||||||
var ONLY_RULES, SKIP_RULES;
|
var ONLY_RULES, SKIP_RULES;
|
||||||
// ONLY_RULES = [
|
// ONLY_RULES = [
|
||||||
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
|
// 'type',
|
||||||
|
// 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
|
||||||
// 'maximum', 'minimum', 'multipleOf',
|
// 'maximum', 'minimum', 'multipleOf',
|
||||||
// 'maxLength', 'minLength', 'pattern',
|
// 'maxLength', 'minLength', 'pattern',
|
||||||
// 'properties', 'patternProperties', 'additionalProperties',
|
// 'properties', 'patternProperties', 'additionalProperties',
|
||||||
|
@ -51,14 +52,14 @@ describe('JSON-Schema tests', function () {
|
||||||
describe(file.name, function() {
|
describe(file.name, function() {
|
||||||
var testSets = require(file.path);
|
var testSets = require(file.path);
|
||||||
testSets.forEach(function (testSet) {
|
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() {
|
describe(testSet.description, function() {
|
||||||
// it(testSet.description, function() {
|
// it(testSet.description, function() {
|
||||||
var validate = ajv.compile(testSet.schema);
|
var validate = ajv.compile(testSet.schema);
|
||||||
var fullValidate = fullAjv.compile(testSet.schema);
|
var fullValidate = fullAjv.compile(testSet.schema);
|
||||||
|
|
||||||
testSet.tests.forEach(function (test) {
|
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() {
|
it(test.description, function() {
|
||||||
var valid = validate(test.data);
|
var valid = validate(test.data);
|
||||||
// console.log('result', result);
|
// console.log('result', result);
|
||||||
|
|
Loading…
Reference in New Issue