Skip to content

Commit b83cfbe

Browse files
committed
json-schema-consolidate in the test
1 parent 3337f7f commit b83cfbe

File tree

7 files changed

+68
-27
lines changed

7 files changed

+68
-27
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
},
2222
"homepage": "https://github.com/JSONScript/jsonscript",
2323
"devDependencies": {
24+
"json-schema-consolidate": "^0.1.1",
2425
"is-my-json-valid": "^2.12.0",
26+
"jjv": "^1.0.2",
27+
"jsonschema": "^1.0.1",
2528
"mocha": "^2.2.5",
26-
"request": "^2.55.0"
29+
"request": "^2.55.0",
30+
"skeemas": "^1.1.6",
31+
"tv4": "^1.1.9",
32+
"z-schema": "^3.10.0"
2733
}
2834
}

schema/_defs.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"id": "http://jsonscript.net/schema/_defs.json",
44
"title": "Definitions",
55
"defs": {
6+
"identifier": {
7+
"type": "string",
8+
"pattern": "^[A-Za-z][A-Za-z0-9_]*$"
9+
},
610
"lowerIdentifier": {
711
"type": "string",
812
"pattern": "^[a-z][A-Za-z0-9_]*$"

schema/_defs.spec.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
[
2+
{ "schema": { "$ref": "_defs.json#/defs/identifier" },
3+
"it": "identifier_1",
4+
"isValid": true },
5+
{ "schema": { "$ref": "_defs.json#/defs/identifier" },
6+
"it": "Identifier",
7+
"isValid": true },
8+
{ "schema": { "$ref": "_defs.json#/defs/identifier" },
9+
"it": "identifier-1",
10+
"isValid": false },
211
{ "schema": { "$ref": "_defs.json#/defs/lowerIdentifier" },
312
"it": "identifier_1",
413
"isValid": true },

schema/exec.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{ "$ref": "_defs.json#/defs/upperIdentifier" },
1010
{ "$ref": "jsonscript.json" } ] },
1111
"$method": { "anyOf": [
12-
{ "$ref": "_defs.json#/defs/lowerIdentifier" },
12+
{ "$ref": "_defs.json#/defs/identifier" },
1313
{ "$ref": "jsonscript.json" } ] },
1414
"$args": { "anyOf": [
1515
{
@@ -18,6 +18,7 @@
1818
{ "$ref": "data.json" },
1919
{ "$ref": "jsonscript.json" } ] }
2020
},
21+
{ "$ref": "data.json" },
2122
{ "$ref": "jsonscript.json" } ] }
2223
},
2324
"additionalProperties": false,

schema/exec.spec.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
[
2-
{ "it": { "$exec": "Router", "$method": "routes" }, "isValid": true },
2+
{ "it": { "$exec": "Router", "$method": "get" }, "isValid": true },
3+
{ "it": { "$exec": "Router", "$method": "GET" }, "isValid": true },
4+
{ "it": { "$exec": "router", "$method": "get" }, "isValid": false },
35
{ "it": { "$exec": "Router" }, "isValid": false },
4-
{ "it": { "$exec": "router", "$method": "routes" }, "isValid": false },
5-
{ "it": { "$exec": "Router", "$method": "Routes" }, "isValid": false }
6+
{ "it":
7+
{ "$exec": "Router",
8+
"$method": "get",
9+
"$args": "/resourse/1" },
10+
"isValid": true },
11+
{ "it":
12+
{ "$exec": "Router",
13+
"$method": "get",
14+
"$args": [ "/resourse/1" ] },
15+
"isValid": true },
16+
{ "it":
17+
{ "$exec": "Router",
18+
"$method": "get",
19+
"$args": { "url": "/resourse" } },
20+
"isValid": true }
621
]

spec/schema.spec.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
'use strict';
22

3-
var validator = require('is-my-json-valid')
3+
var consolidate = require('json-schema-consolidate')
4+
, validator = consolidate('jjv', { allErrors: true, verbose: true })
5+
6+
// , jjv = require('jjv')()
7+
// , Validator = require('jsonschema').Validator
8+
// , validator = new Validator
49
, assert = require('assert')
510
, metaSchema = require('./meta_schema.json')
611
, request = require('request')
712
, fs = require('fs')
813
, path = require('path');
914

1015

16+
var ONLY = '';
17+
18+
1119
describe('JSONScript schema', function() {
1220
describe('Meta-schema', function() {
1321
it('should be the same as the meta-schema at json-schema.org (needs internet connection)', function (done) {
@@ -27,17 +35,19 @@ describe('JSONScript schema', function() {
2735
loadSchemas();
2836

2937
before(function() {
30-
validateSchema = validator(metaSchema, {
31-
verbose: true,
32-
greedy: true
33-
});
38+
validateSchema = validator.compile(metaSchema);
39+
40+
for (var name in schemas)
41+
validator.addSchema(schemas[name], name);
3442
});
3543

3644

3745
for (var name in schemas) testSchema(name);
3846

3947

4048
function testSchema(name) {
49+
if (ONLY && ONLY != name) return;
50+
4151
describe(name + ' schema', function() {
4252
var schema;
4353
var specFile = name.replace('.', '.spec.');
@@ -50,8 +60,8 @@ describe('JSONScript schema', function() {
5060

5161
it('should be valid', function() {
5262
assert.equal(schema.$schema, metaSchema.id);
53-
validateSchema(schema);
54-
assert.equal(validateSchema.errors, null);
63+
var result = validateSchema(schema);
64+
assert.deepEqual(result.errors, []);
5565
});
5666

5767

@@ -60,7 +70,7 @@ describe('JSONScript schema', function() {
6070
var validate;
6171

6272
before(function() {
63-
validate = getValidator(schema);
73+
validate = validator.compile(schema);
6474
});
6575

6676
it('spec file should be array', function() {
@@ -70,33 +80,25 @@ describe('JSONScript schema', function() {
7080
if (!Array.isArray(specs[name])) return;
7181

7282
specs[name].forEach(function (s, index) {
73-
var schemaStr = s.schema ? objToString(s.schema, 24) + ' :' : '';
83+
var schemaStr = s.schema ? objToString(s.schema, 32) + ' :' : '';
7484
var itStr = objToString(s.it, 48);
7585
var validStr = 'should' + (s.isValid ? ' ' : ' NOT ') + 'be valid';
7686

7787
it([schemaStr, itStr, validStr].join(' '), function() {
7888
assert.notStrictEqual(s.it, undefined, 'item #' + index + ' should have "it" property');
7989
assert.equal(typeof s.isValid, 'boolean', 'item #' + index + ' should have "isValid" property');
80-
var _validate = s.schema ? getValidator(s.schema) : validate;
81-
_validate(s.it);
82-
var assertion = s.isValid ? 'equal' : 'notEqual';
83-
assert[assertion](_validate.errors, null);
90+
var _validate = s.schema ? validator.compile(s.schema) : validate;
91+
var result = _validate(s.it);
92+
93+
var assertion = s.isValid ? 'deepEqual' : 'notDeepEqual';
94+
assert[assertion](result.errors, []);
8495
});
8596
});
8697
});
8798
});
8899
}
89100

90101

91-
function getValidator(schema) {
92-
return validator(schema, {
93-
schemas: schemas,
94-
verbose: true,
95-
greedy: true
96-
});
97-
}
98-
99-
100102
function objToString(obj, maxLength) {
101103
var str = JSON.stringify(obj);
102104
if (str.length > maxLength + 3)

spec/validators.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
4+
module.exports = {}

0 commit comments

Comments
 (0)