Skip to content

Commit abfea4b

Browse files
committed
test: add Validator tests for coverage
1 parent 87eccec commit abfea4b

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

lib/validator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Validator.prototype.addSchema = function addSchema (schema, base) {
5656
for(var uri in scan.ref){
5757
this.unresolvedRefs.push(uri);
5858
}
59+
// Remove newly defined schemas from unresolvedRefs
5960
this.unresolvedRefs = this.unresolvedRefs.filter(function(uri){
6061
return typeof self.schemas[uri]==='undefined';
6162
});

test/Validator.test.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,102 @@ describe('Validator', function () {
1212
beforeEach(function () {
1313
validator = new Validator();
1414
});
15+
describe('Validator#unresolvedRefs', function(){
16+
it('initial value', function(){
17+
assert.deepStrictEqual(validator.unresolvedRefs, []);
18+
});
19+
it('addSchema', function(){
20+
validator.addSchema({
21+
id: 'http://example.com/base.json',
22+
items: {
23+
$ref: 'item.json',
24+
},
25+
});
26+
assert.deepStrictEqual(validator.unresolvedRefs, ['http://example.com/item.json']);
27+
});
28+
});
29+
describe('Validator#addSchema', function () {
30+
it('argument schema must be a schema: object', function(){
31+
validator.addSchema({}, 'http://example.com/base.json');
32+
assert.deepStrictEqual(validator.schemas['http://example.com/base.json'], {});
33+
});
34+
// TODO: held for major version upgrade
35+
it.skip('argument schema must be a schema: true', function(){
36+
validator.addSchema(true, 'http://example.com/base.json');
37+
assert.deepStrictEqual(validator.schemas['http://example.com/base.json'], true);
38+
});
39+
// TODO: held for major version upgrade
40+
it.skip('argument schema must be a schema: false', function(){
41+
validator.addSchema(false, 'http://example.com/base.json');
42+
assert.deepStrictEqual(validator.schemas['http://example.com/base.json'], false);
43+
});
44+
it('argument schema must be a schema: null', function(){
45+
var res = validator.addSchema(undefined, 'http://example.com/base.json');
46+
assert.strictEqual(res, null);
47+
assert(!('http://example.com/base.json' in validator.schemas));
48+
});
49+
it('argument schema must be a schema: undefined', function(){
50+
var res = validator.addSchema(undefined, 'http://example.com/base.json');
51+
assert.strictEqual(res, null);
52+
assert(!('http://example.com/base.json' in validator.schemas));
53+
});
54+
// TODO: held for major version upgrade
55+
it.skip('argument schema must not be a number', function(){
56+
assert.throws(function(){
57+
validator.addSchema(1, 'http://example.com/base.json');
58+
}, function(err){
59+
assert(err instanceof Error);
60+
return true;
61+
});
62+
});
63+
// TODO: held for major version upgrade
64+
it.skip('argument schema must not be a string', function(){
65+
assert.throws(function(){
66+
validator.addSchema("schema", 'http://example.com/base.json');
67+
}, function(err){
68+
assert(err instanceof Error);
69+
return true;
70+
});
71+
});
72+
// TODO: held for major version upgrade
73+
it.skip('argument schema must not be null', function(){
74+
assert.throws(function(){
75+
validator.addSchema(null, 'http://example.com/base.json');
76+
}, function(err){
77+
assert(err instanceof Error);
78+
return true;
79+
});
80+
});
81+
// TODO: held for major version upgrade
82+
it.skip('argument schema must not be undefined', function(){
83+
assert.throws(function(){
84+
validator.addSchema(undefined, 'http://example.com/base.json');
85+
}, function(err){
86+
assert(err instanceof Error);
87+
return true;
88+
});
89+
});
90+
it('addSchema(schema) with absolute id', function(){
91+
validator.addSchema({
92+
id: 'http://example.com/base.json',
93+
});
94+
assert.deepStrictEqual(validator.unresolvedRefs, []);
95+
});
96+
it('addSchema(schema) with absolute $id', function(){
97+
validator.addSchema({
98+
$id: 'http://example.com/base.json',
99+
});
100+
assert.deepStrictEqual(validator.unresolvedRefs, []);
101+
});
102+
it('addSchema(schema, base) with relative id', function(){
103+
validator.addSchema({id: 'base.json'}, 'http://example.com/index.html');
104+
assert('http://example.com/base.json' in validator.schemas);
105+
});
106+
it('addSchema(schema, base) with relative $id', function(){
107+
validator.addSchema({$id: 'base.json'}, 'http://example.com/index.html');
108+
assert('http://example.com/base.json' in validator.schemas);
109+
});
110+
});
15111
describe('Validator#validate', function () {
16112
it('schema may be an object', function () {
17113
var res = validator.validate(true, {});
@@ -33,6 +129,14 @@ describe('Validator', function () {
33129
return true;
34130
});
35131
});
132+
it('schema cannot be undefined', function () {
133+
assert.throws(function () {
134+
validator.validate(true, undefined);
135+
}, function(err){
136+
assert(err instanceof SchemaError);
137+
return true;
138+
});
139+
});
36140
it('schema cannot be a string', function () {
37141
assert.throws(function () {
38142
validator.validate(true, "string");
@@ -41,5 +145,14 @@ describe('Validator', function () {
41145
return true;
42146
});
43147
});
148+
it('options may be undefined', function () {
149+
validator.validate(null, true, undefined);
150+
});
151+
it('options may be null', function () {
152+
validator.validate(null, true, null);
153+
});
154+
it('options.base must be a string', function () {
155+
validator.validate(null, true, null);
156+
});
44157
});
45158
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy