Project rename

This commit is contained in:
2019-03-24 08:28:57 +01:00
parent 1728e0e6e1
commit 7926230faf
7262 changed files with 0 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="qunit.css" type="text/css"/>
<script type="text/javascript" src="qunit.js"></script>
<script type="text/javascript" src="../lib/uri/uri.js"></script>
<script type="text/javascript" src="../lib/uri/schemes/urn.js"></script>
<script type="text/javascript" src="../lib/jsv.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-01.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-02.js"></script>
<script type="text/javascript" src="tests.js"></script>
</head>
<body>
<h1 id="qunit-header">JSON Schema Validator Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="qunit.css" type="text/css"/>
<script type="text/javascript" src="qunit.js"></script>
<script type="text/javascript" src="../lib/uri/uri.js"></script>
<script type="text/javascript" src="../lib/uri/schemes/urn.js"></script>
<script type="text/javascript" src="../lib/jsv.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-03.js"></script>
<script type="text/javascript" src="tests3.js"></script>
</head>
<body>
<h1 id="qunit-header">JSON Schema Validator Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="qunit.css" type="text/css"/>
<script type="text/javascript" src="qunit.js"></script>
<script type="text/javascript" src="../lib/uri/uri.js"></script>
<script type="text/javascript" src="../lib/uri/schemes/urn.js"></script>
<script type="text/javascript" src="../lib/jsv.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-03.js"></script>
<script type="text/javascript" src="tests3b.js"></script>
</head>
<body>
<h1 id="qunit-header">JSON Schema Validator Tests by dougtreder</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
+118
View File
@@ -0,0 +1,118 @@
ol#qunit-tests {
font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
margin:0;
padding:0;
list-style-position:inside;
font-size: smaller;
}
ol#qunit-tests li{
padding:0.4em 0.5em 0.4em 2.5em;
border-bottom:1px solid #fff;
font-size:small;
list-style-position:inside;
}
ol#qunit-tests li ol{
box-shadow: inset 0px 2px 13px #999;
-moz-box-shadow: inset 0px 2px 13px #999;
-webkit-box-shadow: inset 0px 2px 13px #999;
margin-top:0.5em;
margin-left:0;
padding:0.5em;
background-color:#fff;
border-radius:15px;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
}
ol#qunit-tests li li{
border-bottom:none;
margin:0.5em;
background-color:#fff;
list-style-position: inside;
padding:0.4em 0.5em 0.4em 0.5em;
}
ol#qunit-tests li li.pass{
border-left:26px solid #C6E746;
background-color:#fff;
color:#5E740B;
}
ol#qunit-tests li li.fail{
border-left:26px solid #EE5757;
background-color:#fff;
color:#710909;
}
ol#qunit-tests li.pass{
background-color:#D2E0E6;
color:#528CE0;
}
ol#qunit-tests li.fail{
background-color:#EE5757;
color:#000;
}
ol#qunit-tests li strong {
cursor:pointer;
}
h1#qunit-header{
background-color:#0d3349;
margin:0;
padding:0.5em 0 0.5em 1em;
color:#fff;
font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
border-top-right-radius:15px;
border-top-left-radius:15px;
-moz-border-radius-topright:15px;
-moz-border-radius-topleft:15px;
-webkit-border-top-right-radius:15px;
-webkit-border-top-left-radius:15px;
text-shadow: rgba(0, 0, 0, 0.5) 4px 4px 1px;
}
h2#qunit-banner{
font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
height:5px;
margin:0;
padding:0;
}
h2#qunit-banner.qunit-pass{
background-color:#C6E746;
}
h2#qunit-banner.qunit-fail, #qunit-testrunner-toolbar {
background-color:#EE5757;
}
#qunit-testrunner-toolbar {
font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
padding:0;
/*width:80%;*/
padding:0em 0 0.5em 2em;
font-size: small;
}
h2#qunit-userAgent {
font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
background-color:#2b81af;
margin:0;
padding:0;
color:#fff;
font-size: small;
padding:0.5em 0 0.5em 2.5em;
text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
}
p#qunit-testresult{
font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
margin:0;
font-size: small;
color:#2b81af;
border-bottom-right-radius:15px;
border-bottom-left-radius:15px;
-moz-border-radius-bottomright:15px;
-moz-border-radius-bottomleft:15px;
-webkit-border-bottom-right-radius:15px;
-webkit-border-bottom-left-radius:15px;
background-color:#D2E0E6;
padding:0.5em 0.5em 0.5em 2.5em;
}
strong b.fail{
color:#710909;
}
strong b.pass{
color:#5E740B;
}
File diff suppressed because it is too large Load Diff
+452
View File
@@ -0,0 +1,452 @@
var env,
ENVIRONMENTS = [
"json-schema-draft-01",
"json-schema-draft-02"
],
curEnvId;
//calls ok(true) if no error is thrown
function okNoError(func, msg) {
try {
func();
ok(true, msg);
} catch (e) {
ok(false, msg + ': ' + e);
}
}
//calls ok(true) if an error is thrown
function okError(func, msg) {
try {
func();
ok(false, msg);
} catch (e) {
ok(true, msg + ': ' + e);
}
}
//
//
// Tests
//
//
for (curEnvId = 0; curEnvId < ENVIRONMENTS.length; ++curEnvId) {
module(ENVIRONMENTS[curEnvId]);
(function (id) {
test("Acquire Validator", function () {
JSV = require('../lib/jsv').JSV;
env = null;
ok(JSV, "JSV is loaded");
env = JSV.createEnvironment(id);
ok(env, id + " environment created");
});
}(ENVIRONMENTS[curEnvId]));
test("Primitive Validation", function () {
equal(env.validate({}).errors.length, 0, "Object");
equal(env.validate([]).errors.length, 0, "Array");
equal(env.validate('').errors.length, 0, "String");
equal(env.validate(00).errors.length, 0, "Number");
equal(env.validate(false).errors.length, 0, "Boolean");
equal(env.validate(null).errors.length, 0, "Null");
});
test("Type Validation", function () {
//simple type
equal(env.validate({}, { type : 'object' }).errors.length, 0, "Object");
equal(env.validate([], { type : 'array' }).errors.length, 0, "Array");
equal(env.validate('', { type : 'string' }).errors.length, 0, "String");
equal(env.validate(00, { type : 'number' }).errors.length, 0, "Number");
equal(env.validate(00, { type : 'integer' }).errors.length, 0, "Integer");
equal(env.validate(false, { type : 'boolean' }).errors.length, 0, "Boolean");
equal(env.validate(null, { type : 'null' }).errors.length, 0, "Null");
equal(env.validate(true, { type : 'any' }).errors.length, 0, "Any");
notEqual(env.validate(null, { type : 'object' }).errors.length, 0, "Object");
notEqual(env.validate(null, { type : 'array' }).errors.length, 0, "Array");
notEqual(env.validate(null, { type : 'string' }).errors.length, 0, "String");
notEqual(env.validate(null, { type : 'number' }).errors.length, 0, "Number");
notEqual(env.validate(0.1, { type : 'integer' }).errors.length, 0, "Integer");
notEqual(env.validate(null, { type : 'boolean' }).errors.length, 0, "Boolean");
notEqual(env.validate(false, { type : 'null' }).errors.length, 0, "Null");
//union type
equal(env.validate({}, { type : ['null', 'boolean', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Object");
notEqual(env.validate({}, { type : ['null', 'boolean', 'number', 'integer', 'string', 'array'] }).errors.length, 0, "Object");
//schema union type
equal(env.validate({}, { type : [{ type : 'string' }, { type : 'object' }] }).errors.length, 0, "Object");
equal(env.validate(55, { type : [{ type : 'string' }, { type : 'object' }, 'number'] }).errors.length, 0, "Object");
notEqual(env.validate([], { type : ['string', { type : 'object' }] }).errors.length, 0, "Array");
});
test("Properties Validation", function () {
equal(env.validate({}, { type : 'object', properties : {} }).errors.length, 0);
equal(env.validate({ a : 1 }, { type : 'object', properties : { a : {}} }).errors.length, 0);
equal(env.validate({ a : 1 }, { type : 'object', properties : { a : { type : 'number' }} }).errors.length, 0);
equal(env.validate({ a : { b : 'two' } }, { type : 'object', properties : { a : { type : 'object', properties : { b : { type : 'string' } } }} }).errors.length, 0);
});
test("Items Validation", function () {
equal(env.validate([], { type : 'array', items : { type : 'string' } }).errors.length, 0);
equal(env.validate(['foo'], { type : 'array', items : { type : 'string' } }).errors.length, 0);
equal(env.validate(['foo', 2], { type : 'array', items : [{ type : 'string' }, { type : 'number' }] }).errors.length, 0);
notEqual(env.validate([1], { type : 'array', items : { type : 'string' } }).errors.length, 0);
notEqual(env.validate(['foo', 'two'], { type : 'array', items : [{ type : 'string' }, { type : 'number' }] }).errors.length, 0);
});
test("Optional Validation", function () {
equal(env.validate({}, { properties : { a : { optional : true } } }).errors.length, 0);
equal(env.validate({ a : false }, { properties : { a : { optional : true } } }).errors.length, 0);
equal(env.validate({ a : false }, { properties : { a : { optional : false } } }).errors.length, 0);
notEqual(env.validate({}, { properties : { a : { optional : false } } }).errors.length, 0);
notEqual(env.validate({ b : true }, { properties : { a : { optional : false } } }).errors.length, 0);
notEqual(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
});
test("AdditionalProperties Validation", function () {
//object tests
equal(env.validate({ a : 1, b : 2, c : 3 }, {}).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { additionalProperties : true }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : true }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {}, c : {} }, additionalProperties : false }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {}, c : {} }, additionalProperties : { type : 'string' } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : false }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : { type : 'string' } }).errors.length, 0);
//array tests
equal(env.validate([1, 2, 3], {}).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : true }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : false }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : { type : 'string' } }).errors.length, 0);
equal(env.validate(['1', '2'], { items : { type : 'string' }, additionalProperties : false }).errors.length, 0);
equal(env.validate(['1', '2'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : false }).errors.length, 0);
equal(env.validate(['1', '2', 3], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : false }).errors.length, 0);
notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
});
test("Requires Validation", function () {
equal(env.validate({ a : 1 }, { properties : { a : { requires : 'a' } } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { a : {}, b : { requires : 'a' } } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { a : { requires : 'b' }, b : { requires : 'a' } } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { b : { requires : { properties : { a : { type : 'number' } } } } } }).errors.length, 0);
notEqual(env.validate({ b : 2 }, { properties : { b : { requires : 'a' } } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2 }, { properties : { a : { requires : 'b' }, b : { requires : 'c' } } }).errors.length, 0);
notEqual(env.validate({ b : 2 }, { properties : { b : { requires : { properties : { b : { type : 'string' } } } } } }).errors.length, 0);
});
test("Minimum/Maximum Validation", function () {
equal(env.validate(0, {}).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 10 }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10 }).errors.length, 0);
equal(env.validate(10, { minimum : 1, maximum : 10 }).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 1 }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10 }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10 }).errors.length, 0);
});
test("MinimumCanEqual/MaximumCanEqual Validation", function () {
//true
notEqual(env.validate(0, { minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0); //illegal
equal(env.validate(1, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
equal(env.validate(10, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 1, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
//false
notEqual(env.validate(0, { minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0); //illegal
equal(env.validate(1.0001, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
equal(env.validate(9.9999, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(1, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(10, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(1, { minimum : 1, maximum : 1, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
});
test("MinItems/MaxItems Validation", function () {
equal(env.validate([], {}).errors.length, 0);
equal(env.validate([1], { minItems : 1, maxItems : 1 }).errors.length, 0);
equal(env.validate([1], { minItems : 1, maxItems : 3 }).errors.length, 0);
equal(env.validate([1, 2], { minItems : 1, maxItems : 3 }).errors.length, 0);
equal(env.validate([1, 2, 3], { minItems : 1, maxItems : 3 }).errors.length, 0);
notEqual(env.validate([], { minItems : 1, maxItems : 0 }).errors.length, 0);
notEqual(env.validate([], { minItems : 1, maxItems : 3 }).errors.length, 0);
notEqual(env.validate([1, 2, 3, 4], { minItems : 1, maxItems : 3 }).errors.length, 0);
});
if (curEnvId >= 1) {
test("UniqueItems Validation", function () {
equal(env.validate([], {}).errors.length, 0);
equal(env.validate([], { uniqueItems : true }).errors.length, 0);
equal(env.validate([null], { uniqueItems : true }).errors.length, 0);
equal(env.validate([true, false], { uniqueItems : true }).errors.length, 0);
equal(env.validate([1, 2, 3], { uniqueItems : true }).errors.length, 0);
equal(env.validate(['a', 'b'], { uniqueItems : true }).errors.length, 0);
equal(env.validate([[], []], { uniqueItems : true }).errors.length, 0);
equal(env.validate([{}, {}], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate([null, null], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate([false, false], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate([1, 2, 1], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate(['a', 'b', 'b'], { uniqueItems : true }).errors.length, 0);
});
}
test("Pattern Validation", function () {
equal(env.validate('', {}).errors.length, 0);
equal(env.validate('', { pattern : '^$' }).errors.length, 0);
equal(env.validate('today', { pattern : 'day' }).errors.length, 0);
notEqual(env.validate('', { pattern : '^ $' }).errors.length, 0);
notEqual(env.validate('today', { pattern : 'dam' }).errors.length, 0);
notEqual(env.validate('aaaaa', { pattern : 'aa(a' }).errors.length, 0);
});
test("MinLength/MaxLength Validation", function () {
equal(env.validate('', {}).errors.length, 0);
equal(env.validate('1', { minLength : 1, maxLength : 1 }).errors.length, 0);
equal(env.validate('1', { minLength : 1, maxLength : 3 }).errors.length, 0);
equal(env.validate('12', { minLength : 1, maxLength : 3 }).errors.length, 0);
equal(env.validate('123', { minLength : 1, maxLength : 3 }).errors.length, 0);
notEqual(env.validate('', { minLength : 1, maxLength : 0 }).errors.length, 0);
notEqual(env.validate('', { minLength : 1, maxLength : 3 }).errors.length, 0);
notEqual(env.validate('1234', { minLength : 1, maxLength : 3 }).errors.length, 0);
});
test("Enum Validation", function () {
equal(env.validate(null, {}).errors.length, 0);
equal(env.validate(true, { 'enum' : [false, true] }).errors.length, 0);
equal(env.validate(2, { 'enum' : [1, 2, 3] }).errors.length, 0);
equal(env.validate('a', { 'enum' : ['a'] }).errors.length, 0);
equal(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : true } } }).errors.length, 0);
notEqual(env.validate(true, { 'enum' : ['false', 'true'] }).errors.length, 0);
notEqual(env.validate(4, { 'enum' : [1, 2, 3, '4'] }).errors.length, 0);
notEqual(env.validate('', { 'enum' : [] }).errors.length, 0);
notEqual(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'] } } }).errors.length, 0);
});
test("Format Validation", function () {
//TODO
});
if (curEnvId === 0) {
test("MaxDecimal Validation", function () {
equal(env.validate(0, {}).errors.length, 0);
equal(env.validate(0, { maxDecimal : 0 }).errors.length, 0);
equal(env.validate(0, { maxDecimal : 1 }).errors.length, 0);
equal(env.validate(0.22, { maxDecimal : 2 }).errors.length, 0);
equal(env.validate(0.33, { maxDecimal : 3 }).errors.length, 0);
notEqual(env.validate(0.1, { maxDecimal : 0 }).errors.length, 0);
notEqual(env.validate(0.111, { maxDecimal : 1 }).errors.length, 0);
});
}
if (curEnvId >= 1) {
test("DivisibleBy Validation", function () {
equal(env.validate(0, {}).errors.length, 0);
equal(env.validate(0, { divisibleBy : 1 }).errors.length, 0);
equal(env.validate(10, { divisibleBy : 5 }).errors.length, 0);
equal(env.validate(10, { divisibleBy : 10 }).errors.length, 0);
equal(env.validate(0, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(5, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(7.5, { divisibleBy : 2.5 }).errors.length, 0);
notEqual(env.validate(0, { divisibleBy : 0 }).errors.length, 0);
notEqual(env.validate(7, { divisibleBy : 5 }).errors.length, 0);
notEqual(env.validate(4.5, { divisibleBy : 2 }).errors.length, 0);
notEqual(env.validate(7.5, { divisibleBy : 1.8 }).errors.length, 0);
});
}
test("Disallow Validation", function () {
equal(env.validate({}, { disallow : ['null', 'boolean', 'number', 'integer', 'string', 'array'] }).errors.length, 0, "Object");
equal(env.validate([], { disallow : ['null', 'boolean', 'number', 'integer', 'string', 'object'] }).errors.length, 0, "Array");
equal(env.validate('', { disallow : ['null', 'boolean', 'number', 'integer', 'array', 'object'] }).errors.length, 0, "String");
equal(env.validate(0.1, { disallow : ['null', 'boolean', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Number");
equal(env.validate(00, { disallow : ['null', 'boolean', 'string', 'array', 'object'] }).errors.length, 0, "Integer");
equal(env.validate(false, { disallow : ['null', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Boolean");
equal(env.validate(null, { disallow : ['boolean', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Null");
notEqual(env.validate({}, { disallow : 'object' }).errors.length, 0, "Object");
notEqual(env.validate([], { disallow : 'array' }).errors.length, 0, "Array");
notEqual(env.validate('', { disallow : 'string' }).errors.length, 0, "String");
notEqual(env.validate(00, { disallow : 'integer' }).errors.length, 0, "Number");
notEqual(env.validate(0.1, { disallow : 'number' }).errors.length, 0, "Integer");
notEqual(env.validate(false, { disallow : 'boolean' }).errors.length, 0, "Boolean");
notEqual(env.validate(null, { disallow : 'null' }).errors.length, 0, "Null");
notEqual(env.validate(null, { disallow : 'any' }).errors.length, 0, "Any");
});
test("Extends Validation", function () {
equal(env.validate({}, { 'extends' : {} }).errors.length, 0);
equal(env.validate({}, { 'extends' : { type : 'object' } }).errors.length, 0);
equal(env.validate(1, { type : 'integer', 'extends' : { type : 'number' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { a : { type : 'number' } }, additionalProperties : false, 'extends' : { properties : { b : { type : 'number' } } } }).errors.length, 0);
notEqual(env.validate(1, { type : 'number', 'extends' : { type : 'string' } }).errors.length, 0);
//TODO: More tests
});
test("JSON Schema Validation", function () {
var schema = env.findSchema("http://json-schema.org/schema");
var hyperSchema = env.findSchema("http://json-schema.org/hyper-schema");
var links = env.findSchema("http://json-schema.org/links");
equal(schema.validate(schema).errors.length, 0, "schema.validate(schema)");
equal(hyperSchema.validate(schema).errors.length, 0, "hyperSchema.validate(schema)");
equal(hyperSchema.validate(hyperSchema).errors.length, 0, "hyperSchema.validate(hyperSchema)");
equal(hyperSchema.validate(links).errors.length, 0, "hyperSchema.validate(links)");
});
test("Links Validation", function () {
var schema;
//full
equal(env.validate({ 'a' : {} }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
notEqual(env.validate({ 'a' : 1 }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
//describedby
schema = env.createSchema({ "id" : "http://test.example.com/3", "properties" : { "test" : { "type" : "object" } }, "extends" : { "$ref" : "http://json-schema.org/hyper-schema" } }, null, "http://test.example.com/3");
equal(env.validate({}, { "$schema" : "http://test.example.com/3", "test" : {} }).errors.length, 0);
notEqual(env.validate({}, { "$schema" : "http://test.example.com/3", "test" : 0 }).errors.length, 0);
//self
schema = env.createSchema({ "properties" : { "two" : { "id" : "http://test.example.com/2", "type" : "object" } } }, null, "http://not.example.com/2");
equal(env.validate({}, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
notEqual(env.validate(null, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
//links api
schema = env.createSchema({ "links" : [ { "rel" : "bar", "href" : "http:{-this}#" } ] });
instance = env.createInstance("foo");
equal(schema.getLink("bar", instance), "http:foo#", "'bar' link and self reference");
});
test("PathStart Validation", function () {
var instance = env.createInstance({}, "http://test.example.com/4"),
schema = env.createSchema({"pathStart" : "http://test.example.com"});
equal(env.validate(instance, schema).errors.length, 0);
instance = env.createInstance({}); //random URI
notEqual(env.validate(instance, schema).errors.length, 0);
});
test("Register Schemas", function () {
var schema = env.createSchema({'type' : 'string'}, null, 'http://test.example.com/1');
equal(env.findSchema('http://json-schema.org/hyper-schema').validate(schema).errors.length, 0);
equal(env.validate('', { '$ref' : 'http://test.example.com/1' }).errors.length, 0);
notEqual(env.validate({}, { '$ref' : 'http://test.example.com/1' }).errors.length, 0);
});
test("Complex Examples", function () {
//example 1
var schema = env.createSchema({
"id":"Common#",
"type":"object",
"properties":{
"!":{"type":"string","enum":["Common"]}
},
"additionalProperties":false
}, undefined, "Common#");
var report = env.validate({
"!" : "List",
"list" : [
{
"!" : "Text",
"common" : {"!":"NotCommon"}
}
],
"common" : {"!":"Common"}
},
{
"properties":{
"!":{"type":"string","enum":["List"]},
"list":{
"type":"array",
"items":{
"type":[
{
"type":"object",
"properties":{
"!":{"type":"string","enum":["Music"]},
"common":{"$ref":"Common#"}
}
},
{
"type":"object",
"properties":{
"!":{"type":"string","enum":["Text"]},
"common":{"$ref":"Common#"}
}
}
]
}
},
"common":{"$ref":"Common#"}
}
}
);
notEqual(report.errors.length, 0, "example 1");
//example 2
schema = env.createSchema({
"extends": {
"type": "object",
"properties": {
"id": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
}
}
},
"properties": {
"role": {
"extends": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
},
"description": "some description"
}
}
});
report = env.validate({ "id" : "some id", "role" : "yunowork?"}, schema);
equal(report.errors.length, 0, "example 2");
});
}
+567
View File
@@ -0,0 +1,567 @@
var env,
DRAFTS = [
"draft-00",
"draft-01",
"draft-02",
"draft-03"
],
curDraftId;
//calls ok(true) if no error is thrown
function okNoError(func, msg) {
try {
func();
ok(true, msg);
} catch (e) {
ok(false, msg + ': ' + JSON.stringify(e));
}
}
//calls ok(true) if an error is thrown
function okError(func, msg) {
try {
func();
ok(false, msg);
} catch (e) {
ok(true, msg + ': ' + JSON.stringify(e));
}
}
//
//
// Tests
//
//
for (curDraftId = 0; curDraftId < DRAFTS.length; ++curDraftId) {
module(DRAFTS[curDraftId]);
(function (id) {
test("Acquire Validator", function () {
JSV = require('../lib/jsv').JSV;
env = null;
ok(JSV, "JSV is loaded");
env = JSV.createEnvironment("json-schema-draft-03");
env.setOption("defaultSchemaURI", "http://json-schema.org/" + id + "/hyper-schema#");
env.setOption("latestJSONSchemaSchemaURI", "http://json-schema.org/" + id + "/schema#");
env.setOption("latestJSONSchemaHyperSchemaURI", "http://json-schema.org/" + id + "/hyper-schema#");
env.setOption("latestJSONSchemaLinksURI", "http://json-schema.org/" + id + "/links#");
ok(env, "Environment created");
});
}(DRAFTS[curDraftId]));
test("Primitive Validation", function () {
equal(env.validate({}).errors.length, 0, "Object");
equal(env.validate([]).errors.length, 0, "Array");
equal(env.validate('').errors.length, 0, "String");
equal(env.validate(00).errors.length, 0, "Number");
equal(env.validate(false).errors.length, 0, "Boolean");
equal(env.validate(null).errors.length, 0, "Null");
});
test("Type Validation", function () {
//simple type
equal(env.validate({}, { type : 'object' }).errors.length, 0, "Object");
equal(env.validate([], { type : 'array' }).errors.length, 0, "Array");
equal(env.validate('', { type : 'string' }).errors.length, 0, "String");
equal(env.validate(00, { type : 'number' }).errors.length, 0, "Number");
equal(env.validate(00, { type : 'integer' }).errors.length, 0, "Integer");
equal(env.validate(false, { type : 'boolean' }).errors.length, 0, "Boolean");
equal(env.validate(null, { type : 'null' }).errors.length, 0, "Null");
equal(env.validate(true, { type : 'any' }).errors.length, 0, "Any");
notEqual(env.validate(null, { type : 'object' }).errors.length, 0, "Object");
notEqual(env.validate(null, { type : 'array' }).errors.length, 0, "Array");
notEqual(env.validate(null, { type : 'string' }).errors.length, 0, "String");
notEqual(env.validate(null, { type : 'number' }).errors.length, 0, "Number");
notEqual(env.validate(0.1, { type : 'integer' }).errors.length, 0, "Integer");
notEqual(env.validate(null, { type : 'boolean' }).errors.length, 0, "Boolean");
notEqual(env.validate(false, { type : 'null' }).errors.length, 0, "Null");
//union type
equal(env.validate({}, { type : ['null', 'boolean', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Object");
notEqual(env.validate({}, { type : ['null', 'boolean', 'number', 'integer', 'string', 'array'] }).errors.length, 0, "Object");
//schema union type
equal(env.validate({}, { type : [{ type : 'string' }, { type : 'object' }] }).errors.length, 0, "Object");
equal(env.validate(55, { type : [{ type : 'string' }, { type : 'object' }, 'number'] }).errors.length, 0, "Object");
notEqual(env.validate([], { type : ['string', { type : 'object' }] }).errors.length, 0, "Array");
});
test("Properties Validation", function () {
equal(env.validate({}, { type : 'object', properties : {} }).errors.length, 0);
equal(env.validate({ a : 1 }, { type : 'object', properties : { a : {}} }).errors.length, 0);
equal(env.validate({ a : 1 }, { type : 'object', properties : { a : { type : 'number' }} }).errors.length, 0);
equal(env.validate({ a : { b : 'two' } }, { type : 'object', properties : { a : { type : 'object', properties : { b : { type : 'string' } } }} }).errors.length, 0);
notEqual(env.validate({ a : 1 }, { type : 'object', properties : { a : { type : 'string' }} }).errors.length, 0);
notEqual(env.validate({ a : { b : 'two' } }, { type : 'object', properties : { a : { type : 'object', properties : { b : { type : 'number' } } }} }).errors.length, 0);
});
if (curDraftId > 2) {
test("PatternProperties Validation", function () {
equal(env.validate(null, { patternProperties : {} }).errors.length, 0);
equal(env.validate({}, { patternProperties : {} }).errors.length, 0);
equal(env.validate({ a : 1 }, { patternProperties : { '[a-z]' : {}} }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, cc : '3' }, { patternProperties : { '^[a-z]$' : { type : 'number' }} }).errors.length, 0);
equal(env.validate({ a : { b : 'two' } }, { patternProperties : { '[a-z]' : { patternProperties : { '[a-z]' : { type : 'string' } } }} }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2, c : '3' }, { patternProperties : { '^[a-z]$' : { type : 'number' }} }).errors.length, 0);
notEqual(env.validate({ a : { b : 'two' } }, { patternProperties : { '[a-z]' : { patternProperties : { '[a-z]' : { type : 'number' } } }} }).errors.length, 0);
});
}
test("AdditionalProperties Validation", function () {
//object tests
equal(env.validate({ a : 1, b : 2, c : 3 }, {}).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { additionalProperties : true }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : true }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {}, c : {} }, additionalProperties : false }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {}, c : {} }, additionalProperties : { type : 'string' } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : false }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2, c : 3 }, { properties : { a : {}, b : {} }, additionalProperties : { type : 'string' } }).errors.length, 0);
//array tests
equal(env.validate([1, 2, 3], {}).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : true }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : false }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalProperties : { type : 'string' } }).errors.length, 0);
equal(env.validate(['1', '2'], { items : { type : 'string' }, additionalProperties : false }).errors.length, 0);
equal(env.validate(['1', '2'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : false }).errors.length, 0);
equal(env.validate(['1', '2', 3], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
equal(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
if (curDraftId < 3) {
notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : false }).errors.length, 0);
notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalProperties : { type : 'number' } }).errors.length, 0);
}
});
test("Items Validation", function () {
equal(env.validate([], { type : 'array', items : { type : 'string' } }).errors.length, 0);
equal(env.validate(['foo'], { type : 'array', items : { type : 'string' } }).errors.length, 0);
equal(env.validate(['foo', 2], { type : 'array', items : [{ type : 'string' }, { type : 'number' }] }).errors.length, 0);
notEqual(env.validate([1], { type : 'array', items : { type : 'string' } }).errors.length, 0);
notEqual(env.validate(['foo', 'two'], { type : 'array', items : [{ type : 'string' }, { type : 'number' }] }).errors.length, 0);
});
if (curDraftId > 2) {
test("AdditionalItems Validation", function () {
//array tests
equal(env.validate([1, 2, 3], {}).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalItems : true }).errors.length, 0);
equal(env.validate([1, 2, 3], { additionalItems : { type : 'number' } }).errors.length, 0);
equal(env.validate(['1', '2'], { items : { type : 'string' }, additionalItems : false }).errors.length, 0);
equal(env.validate(['1', '2'], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : false }).errors.length, 0);
equal(env.validate(['1', '2', 3], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : { type : 'number' } }).errors.length, 0);
equal(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' }, { type : 'string' } ], additionalItems : { type : 'number' } }).errors.length, 0);
notEqual(env.validate([1, 2, 3], { additionalItems : false }).errors.length, 0);
notEqual(env.validate([1, 2, 3], { additionalItems : { type : 'string' } }).errors.length, 0);
notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : false }).errors.length, 0);
notEqual(env.validate(['1', '2', '3'], { items : [ { type : 'string' }, { type : 'string' } ], additionalItems : { type : 'number' } }).errors.length, 0);
});
}
(function (curDraftId) {
test("Optional Validation", function () {
equal(env.validate({}, { properties : { a : { optional : true } } }).errors.length, 0);
equal(env.validate({ a : false }, { properties : { a : { optional : true } } }).errors.length, 0);
equal(env.validate({ a : false }, { properties : { a : { optional : false } } }).errors.length, 0);
notEqual(env.validate({}, { properties : { a : { optional : false } } }).errors.length, 0);
notEqual(env.validate({ b : true }, { properties : { a : { optional : false } } }).errors.length, 0);
if (curDraftId < 3) {
notEqual(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
} else {
equal(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
}
});
}(curDraftId));
if (curDraftId > 2) {
test("Required Validation", function () {
equal(env.validate({ b : true }, { properties : { a : {} } }).errors.length, 0);
equal(env.validate({}, { properties : { a : { required : false } } }).errors.length, 0);
equal(env.validate({ a : false }, { properties : { a : { required : false } } }).errors.length, 0);
equal(env.validate({ a : false }, { properties : { a : { required : true } } }).errors.length, 0);
notEqual(env.validate({}, { properties : { a : { required : true } } }).errors.length, 0);
notEqual(env.validate({ b : true }, { properties : { a : { required : true } } }).errors.length, 0);
});
}
test("Requires Validation", function () {
equal(env.validate({ a : 1 }, { properties : { a : { requires : 'a' } } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { a : {}, b : { requires : 'a' } } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { a : { requires : 'b' }, b : { requires : 'a' } } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { b : { requires : { properties : { a : { type : 'number' } } } } } }).errors.length, 0);
notEqual(env.validate({ b : 2 }, { properties : { b : { requires : 'a' } } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2 }, { properties : { a : { requires : 'b' }, b : { requires : 'c' } } }).errors.length, 0);
notEqual(env.validate({ b : 2 }, { properties : { b : { requires : { properties : { b : { type : 'string' } } } } } }).errors.length, 0);
});
if (curDraftId > 2) {
test("Dependencies Validation", function () {
equal(env.validate(null, { dependencies : {} }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { dependencies : {} }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { dependencies : { a : 'b' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { dependencies : { c : 'd' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2, c : 3 }, { dependencies : { a : ['b'], b : ['a', 'c'] } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { dependencies : { a : { properties : { b : { type : 'number', required : true } } } } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2 }, { dependencies : { a : 'c' } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2, c : 3 }, { dependencies : { a : ['b'], b : ['a', 'd'] } }).errors.length, 0);
notEqual(env.validate({ a : 1, b : 2 }, { dependencies : { a : { properties : { b : { type : 'string', required : true } } } } }).errors.length, 0);
});
}
test("Minimum/Maximum Validation", function () {
equal(env.validate(0, {}).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 10 }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10 }).errors.length, 0);
equal(env.validate(10, { minimum : 1, maximum : 10 }).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 1 }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10 }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10 }).errors.length, 0);
});
test("MinimumCanEqual/MaximumCanEqual Validation", function () {
//true
notEqual(env.validate(0, { minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0); //illegal
equal(env.validate(1, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
equal(env.validate(10, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 1, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10, minimumCanEqual : true, maximumCanEqual : true }).errors.length, 0);
//false
notEqual(env.validate(0, { minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0); //illegal
equal(env.validate(1.0001, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
equal(env.validate(9.9999, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(1, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(10, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(1, { minimum : 1, maximum : 1, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10, minimumCanEqual : false, maximumCanEqual : false }).errors.length, 0);
});
if (curDraftId > 2) {
test("ExclusiveMinimum/ExclusiveMaximum Validation", function () {
//true
notEqual(env.validate(0, { exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0); //illegal
equal(env.validate(1, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
equal(env.validate(10, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
equal(env.validate(1, { minimum : 1, maximum : 1, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
//false
notEqual(env.validate(0, { exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0); //illegal
equal(env.validate(1.0001, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
equal(env.validate(5, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
equal(env.validate(9.9999, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
notEqual(env.validate(1, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
notEqual(env.validate(10, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
notEqual(env.validate(1, { minimum : 1, maximum : 1, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
notEqual(env.validate(0, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
notEqual(env.validate(11, { minimum : 1, maximum : 10, exclusiveMinimum : true, exclusiveMaximum : true }).errors.length, 0);
});
}
test("MinItems/MaxItems Validation", function () {
equal(env.validate([], {}).errors.length, 0);
equal(env.validate([1], { minItems : 1, maxItems : 1 }).errors.length, 0);
equal(env.validate([1], { minItems : 1, maxItems : 3 }).errors.length, 0);
equal(env.validate([1, 2], { minItems : 1, maxItems : 3 }).errors.length, 0);
equal(env.validate([1, 2, 3], { minItems : 1, maxItems : 3 }).errors.length, 0);
notEqual(env.validate([], { minItems : 1, maxItems : 0 }).errors.length, 0);
notEqual(env.validate([], { minItems : 1, maxItems : 3 }).errors.length, 0);
notEqual(env.validate([1, 2, 3, 4], { minItems : 1, maxItems : 3 }).errors.length, 0);
});
if (curDraftId > 1) {
test("UniqueItems Validation", function () {
equal(env.validate([], {}).errors.length, 0);
equal(env.validate([], { uniqueItems : true }).errors.length, 0);
equal(env.validate([null], { uniqueItems : true }).errors.length, 0);
equal(env.validate([true, false], { uniqueItems : true }).errors.length, 0);
equal(env.validate([1, 2, 3], { uniqueItems : true }).errors.length, 0);
equal(env.validate(['a', 'b'], { uniqueItems : true }).errors.length, 0);
equal(env.validate([[], []], { uniqueItems : true }).errors.length, 0);
equal(env.validate([{}, {}], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate([null, null], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate([false, false], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate([1, 2, 1], { uniqueItems : true }).errors.length, 0);
notEqual(env.validate(['a', 'b', 'b'], { uniqueItems : true }).errors.length, 0);
});
}
test("Pattern Validation", function () {
equal(env.validate('', {}).errors.length, 0);
equal(env.validate('', { pattern : '^$' }).errors.length, 0);
equal(env.validate('today', { pattern : 'day' }).errors.length, 0);
notEqual(env.validate('', { pattern : '^ $' }).errors.length, 0);
notEqual(env.validate('today', { pattern : 'dam' }).errors.length, 0);
notEqual(env.validate('aaaaa', { pattern : 'aa(a' }).errors.length, 0);
});
test("MinLength/MaxLength Validation", function () {
equal(env.validate('', {}).errors.length, 0);
equal(env.validate('1', { minLength : 1, maxLength : 1 }).errors.length, 0);
equal(env.validate('1', { minLength : 1, maxLength : 3 }).errors.length, 0);
equal(env.validate('12', { minLength : 1, maxLength : 3 }).errors.length, 0);
equal(env.validate('123', { minLength : 1, maxLength : 3 }).errors.length, 0);
notEqual(env.validate('', { minLength : 1, maxLength : 0 }).errors.length, 0);
notEqual(env.validate('', { minLength : 1, maxLength : 3 }).errors.length, 0);
notEqual(env.validate('1234', { minLength : 1, maxLength : 3 }).errors.length, 0);
});
test("Enum Validation", function () {
equal(env.validate(null, {}).errors.length, 0);
equal(env.validate(true, { 'enum' : [false, true] }).errors.length, 0);
equal(env.validate(2, { 'enum' : [1, 2, 3] }).errors.length, 0);
equal(env.validate('a', { 'enum' : ['a'] }).errors.length, 0);
equal(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : true, 'required' : false } } }).errors.length, 0);
notEqual(env.validate(true, { 'enum' : ['false', 'true'] }).errors.length, 0);
notEqual(env.validate(4, { 'enum' : [1, 2, 3, '4'] }).errors.length, 0);
notEqual(env.validate('', { 'enum' : [] }).errors.length, 0);
notEqual(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : false, 'required' : true } } }).errors.length, 0);
});
test("Format Validation", function () {
//TODO
});
test("MaxDecimal Validation", function () {
equal(env.validate(0, {}).errors.length, 0);
equal(env.validate(0, { maxDecimal : 0 }).errors.length, 0);
equal(env.validate(0, { maxDecimal : 1 }).errors.length, 0);
equal(env.validate(0.22, { maxDecimal : 2 }).errors.length, 0);
equal(env.validate(0.33, { maxDecimal : 3 }).errors.length, 0);
notEqual(env.validate(0.1, { maxDecimal : 0 }).errors.length, 0);
notEqual(env.validate(0.111, { maxDecimal : 1 }).errors.length, 0);
});
if (curDraftId > 1) {
test("DivisibleBy Validation", function () {
equal(env.validate(0, {}).errors.length, 0);
equal(env.validate(0, { divisibleBy : 1 }).errors.length, 0);
equal(env.validate(10, { divisibleBy : 5 }).errors.length, 0);
equal(env.validate(10, { divisibleBy : 10 }).errors.length, 0);
equal(env.validate(0, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(5, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(7.5, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(9.1, { divisibleBy : 1.3 }).errors.length, 0);
notEqual(env.validate(0, { divisibleBy : 0 }).errors.length, 0);
notEqual(env.validate(7, { divisibleBy : 5 }).errors.length, 0);
notEqual(env.validate(4.5, { divisibleBy : 2 }).errors.length, 0);
notEqual(env.validate(7.5, { divisibleBy : 1.8 }).errors.length, 0);
});
}
test("Disallow Validation", function () {
equal(env.validate({}, { disallow : ['null', 'boolean', 'number', 'integer', 'string', 'array'] }).errors.length, 0, "Object");
equal(env.validate([], { disallow : ['null', 'boolean', 'number', 'integer', 'string', 'object'] }).errors.length, 0, "Array");
equal(env.validate('', { disallow : ['null', 'boolean', 'number', 'integer', 'array', 'object'] }).errors.length, 0, "String");
equal(env.validate(0.1, { disallow : ['null', 'boolean', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Number");
equal(env.validate(00, { disallow : ['null', 'boolean', 'string', 'array', 'object'] }).errors.length, 0, "Integer");
equal(env.validate(false, { disallow : ['null', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Boolean");
equal(env.validate(null, { disallow : ['boolean', 'number', 'integer', 'string', 'array', 'object'] }).errors.length, 0, "Null");
notEqual(env.validate({}, { disallow : 'object' }).errors.length, 0, "Object");
notEqual(env.validate([], { disallow : 'array' }).errors.length, 0, "Array");
notEqual(env.validate('', { disallow : 'string' }).errors.length, 0, "String");
notEqual(env.validate(00, { disallow : 'integer' }).errors.length, 0, "Number");
notEqual(env.validate(0.1, { disallow : 'number' }).errors.length, 0, "Integer");
notEqual(env.validate(false, { disallow : 'boolean' }).errors.length, 0, "Boolean");
notEqual(env.validate(null, { disallow : 'null' }).errors.length, 0, "Null");
notEqual(env.validate(null, { disallow : 'any' }).errors.length, 0, "Any");
});
test("Extends Validation", function () {
equal(env.validate({}, { 'extends' : {} }).errors.length, 0);
equal(env.validate({}, { 'extends' : { type : 'object' } }).errors.length, 0);
equal(env.validate(1, { type : 'integer', 'extends' : { type : 'number' } }).errors.length, 0);
equal(env.validate({ a : 1, b : 2 }, { properties : { a : { type : 'number' } }, additionalProperties : false, 'extends' : { properties : { b : { type : 'number' } } } }).errors.length, 0);
notEqual(env.validate(1, { type : 'number', 'extends' : { type : 'string' } }).errors.length, 0);
//TODO: More tests
});
test("JSON Schema Validation", function () {
var schema = env.findSchema(env.getOption("latestJSONSchemaSchemaURI"));
var hyperSchema = env.findSchema(env.getOption("latestJSONSchemaHyperSchemaURI"));
var links = env.findSchema(env.getOption("latestJSONSchemaLinksURI"));
equal(schema.validate(schema).errors.length, 0, "schema.validate(schema)");
equal(hyperSchema.validate(schema).errors.length, 0, "hyperSchema.validate(schema)");
equal(hyperSchema.validate(hyperSchema).errors.length, 0, "hyperSchema.validate(hyperSchema)");
equal(hyperSchema.validate(links).errors.length, 0, "hyperSchema.validate(links)");
});
(function(curDraftId){
test("Links Validation", function () {
var schema, instance;
//full
equal(env.validate({ 'a' : {} }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
notEqual(env.validate({ 'a' : 1 }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
//describedby
okNoError(function () {
schema = env.createSchema({ "id" : "http://test.example.com/3", "properties" : { "test" : { "type" : "object" } }, "extends" : { "$ref" : "http://json-schema.org/draft-03/schema#" } }, null, "http://test.example.com/3");
equal(env.validate({}, { "$schema" : "http://test.example.com/3", "test" : {} }).errors.length, 0);
notEqual(env.validate({}, { "$schema" : "http://test.example.com/3", "test" : 0 }).errors.length, 0);
}, "describedby schema");
//self
okNoError(function () {
schema = env.createSchema({ "properties" : { "two" : { "id" : "http://test.example.com/2", "type" : "object" } } }, null, "http://not.example.com/2");
equal(env.validate({}, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
notEqual(env.validate(null, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
}, "self schema");
//links api
okNoError(function () {
schema = env.createSchema({ "links" : [ { "rel" : "bar", "href" : "http:" + (curDraftId < 3 ? "{-this}" : "{@}") + "#" } ] });
instance = env.createInstance("foo");
equal(schema.getLink("bar", instance), "http:foo#", "'bar' link and self reference");
}, "links api schema");
//invalid reference
(env.getOption("enforceReferences") ? okError : okNoError)(function () {
schema = env.createSchema({ "$ref" : "asdf:qwerty" }); //should throw error
}, "invalid reference");
});
}(curDraftId));
test("PathStart Validation", function () {
var instance = env.createInstance({}, "http://test.example.com/4"),
schema = env.createSchema({"pathStart" : "http://test.example.com"});
equal(env.validate(instance, schema).errors.length, 0);
instance = env.createInstance({}); //random URI
notEqual(env.validate(instance, schema).errors.length, 0);
});
test("Register Schemas", function () {
var schema = env.createSchema({'type' : 'string'}, null, 'http://test.example.com/1');
equal(env.findSchema(env.getOption("latestJSONSchemaHyperSchemaURI")).validate(schema).errors.length, 0);
equal(env.validate('', { '$ref' : 'http://test.example.com/1' }).errors.length, 0);
notEqual(env.validate({}, { '$ref' : 'http://test.example.com/1' }).errors.length, 0);
});
test("Complex Examples", function () {
//example 1
var schema = env.createSchema({
"id":"Common#",
"type":"object",
"properties":{
"!":{"type":"string","enum":["Common"]}
},
"additionalProperties":false
}, undefined, "Common#");
var report = env.validate({
"!" : "List",
"list" : [
{
"!" : "Text",
"common" : {"!":"NotCommon"}
}
],
"common" : {"!":"Common"}
},
{
"properties":{
"!":{"type":"string","enum":["List"]},
"list":{
"type":"array",
"items":{
"type":[
{
"type":"object",
"properties":{
"!":{"type":"string","enum":["Music"]},
"common":{"$ref":"Common#"}
}
},
{
"type":"object",
"properties":{
"!":{"type":"string","enum":["Text"]},
"common":{"$ref":"Common#"}
}
}
]
}
},
"common":{"$ref":"Common#"}
}
}
);
notEqual(report.errors.length, 0, "example 1");
//example 2
schema = env.createSchema({
"extends": {
"type": "object",
"properties": {
"id": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
}
}
},
"properties": {
"role": {
"extends": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
},
"description": "some description"
}
}
});
report = env.validate({ "id" : "some id", "role" : "yunowork?"}, schema);
equal(report.errors.length, 0, "example 2");
});
}
+211
View File
@@ -0,0 +1,211 @@
var env;
//calls ok(true) if no error is thrown
function okNoError(func, msg) {
try {
func();
ok(true, msg);
} catch (e) {
ok(false, msg + ': ' + e);
}
}
function setupEnv () {
env = env || require('../lib/jsv').JSV.createEnvironment("json-schema-draft-03");
// AddressBook example from http://relaxng.org/compact-tutorial-20030326.html
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook.json",
"description": "AddressBook example from http://relaxng.org/compact-tutorial-20030326.html",
"properties": {
"cards": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2,
"maxItems": 2,
"$schema":"http://json-schema.org/draft-03/schema#"
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/addressbook.json");
// The referral target schema, with a canonical id.
env.createSchema({
"type": "array",
"id": "http://example.com/subdir/card.json",
"description": "Referral target",
"items": {
"type": "string"
},
"minItems": 2,
"maxItems": 2,
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/subdir/card.json");
// Similar example, using $ref to factor part of the schema.
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook_ref.json",
"description": "Similar example, using $ref to factor part of the schema.",
"properties": {
"cards": {
"type": "array",
"items": {
"$ref": "./subdir/card.json"
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/addressbook_ref.json");
// Similar example, using extends to factor part of the schema.
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook_extends.json",
"description": "Similar example, using extends to factor part of the schema.",
"properties": {
"cards": {
"type": "array",
"items": {
"extends": {
"$ref": "./subdir/card.json"
}
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/addressbook_extends.json");
}
//
//
// Tests
//
//
module("Reference Tests");
test("Self Identifying Schemas", function () {
setupEnv();
// While createSchema takes a URI argument, for V3 schemas the validator should be able
// to use the canonical id field to find it, if it has been registered.
// http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.27
env.createSchema({ "id": "http://example.com/foo.json",
"$schema":"http://json-schema.org/draft-03/schema#"
});
ok(env.findSchema("http://example.com/foo.json"), "found schema by id");
});
test("Forward Referral", function () {
setupEnv();
// A schema with a reference in it should be accepted, even if the destination schema
// hasn't been registered yet.
okNoError(function () {
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook.json",
"properties": {
"cards": {
"type": "array",
"items": {
"$ref": "http://example.com/subdir/card.json"
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
});
}, "Can make schema with forward reference");
});
test("Validate Schema", function () {
setupEnv();
var jsonschema = env.findSchema(env.getOption("latestJSONSchemaSchemaURI"));
var explicit_schema = env.findSchema("http://example.com/addressbook.json");
var referring_schema = env.findSchema("http://example.com/addressbook_ref.json");
var card_schema = env.findSchema("http://example.com/subdir/card.json");
ok(explicit_schema, "explicit addressbook schema");
ok(referring_schema, "referring addressbook schema");
ok(card_schema, "card schema");
equal(jsonschema.validate(explicit_schema).errors.length, 0, 'valid explicit schema');
equal(jsonschema.validate(referring_schema).errors.length, 0, 'valid referring schema');
equal(jsonschema.validate(card_schema).errors.length, 0, 'valid referral target schema');
});
test("Simple Referral", function () {
setupEnv();
var schema = { "$ref": "http://example.com/subdir/card.json" };
notEqual(env.validate({}, schema).errors.length, 0, "card must be array");
notEqual(env.validate([], schema).errors.length, 0, "card must have fields");
notEqual(env.validate(["foo"], schema).errors.length, 0, "card must have two fields");
notEqual(env.validate(["foo", {}], schema).errors.length, 0, "both fields must be string");
notEqual(env.validate(["foo", "bar", "baz"], schema).errors.length, 0, "card maxItems 2");
equal(env.validate(["foo", "bar"], schema).errors.length, 0, "card maxItems 2");
});
function validateAddressbook (test_name, schema_uri) {
var schema = { "$ref": schema_uri };
test(test_name, function () {
setupEnv();
notEqual(env.validate('', schema).errors.length, 0, "addressbook is object");
notEqual(env.validate({}, schema).errors.length, 0, "cards required");
notEqual(env.validate({ "cards": {}}, schema).errors.length, 0, "cards must be array");
equal(env.validate({ "cards": []}, schema).errors.length, 0, "empty array ok");
notEqual(env.validate({ "cards": [ {} ] }, schema).errors.length, 0,
"cards schema is enforced on items");
notEqual(env.validate({ "cards": [['foo']]}, schema).errors.length, 0,
"each card must have length 2");
notEqual(env.validate({ "cards": [ ['foo', 'bar' ], ["foo" ] ] }, schema).errors.length, 0,
"second card is bad");
equal(env.validate({ "cards": [ ["foo", "bar" ] ] }, schema).errors.length, 0,
"good addressbook with one card");
equal(env.validate({ "cards": [ ["foo", "bar" ], ["bar", "foo" ] ] }, schema).errors.length, 0,
"good addressbook with two cards");
});
}
validateAddressbook("Explicit Schema", "http://example.com/addressbook.json");
validateAddressbook("Referring Schema", "http://example.com/addressbook_ref.json");
validateAddressbook("Extends Schema", "http://example.com/addressbook_extends.json");