-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathofficial.test.mjs
More file actions
147 lines (131 loc) · 5.4 KB
/
Copy pathofficial.test.mjs
File metadata and controls
147 lines (131 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { join, resolve, dirname, basename } from 'node:path';
import { fileURLToPath } from 'node:url';
import { compileSchema } from './compile.mjs';
import { Blaze } from './index.mjs';
const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
const PROJECT_ROOT = resolve(MODULE_DIR, '../..');
const SUITE_PATH = join(PROJECT_ROOT, 'vendor/jsonschema-test-suite');
const REMOTES_PATH = join(SUITE_PATH, 'remotes');
const RESOLVE_DIRECTORY = `http://localhost:1234,${REMOTES_PATH}`;
const BLACKLISTS = {
'draft2020-12': new Set(),
'draft2020-12/optional': new Set(['format-assertion', 'refOfUnknownKeyword']),
'draft2020-12/optional/format': new Set([
'date-time', 'date', 'duration', 'email', 'hostname', 'idn-email',
'idn-hostname', 'ipv4', 'ipv6', 'iri-reference', 'iri', 'json-pointer',
'regex', 'relative-json-pointer', 'time', 'uri-reference', 'uri-template',
'uri', 'uuid', 'ecmascript-regex'
]),
'draft2019-09': new Set(),
'draft2019-09/optional': new Set(['refOfUnknownKeyword']),
'draft2019-09/optional/format': new Set([
'date-time', 'date', 'duration', 'email', 'hostname', 'idn-email',
'idn-hostname', 'ipv4', 'ipv6', 'iri-reference', 'iri', 'json-pointer',
'regex', 'relative-json-pointer', 'time', 'uri-reference', 'uri-template',
'uri', 'uuid'
]),
'draft7': new Set(),
'draft7/optional': new Set(['content']),
'draft7/optional/format': new Set([
'date-time', 'date', 'email', 'hostname', 'idn-email', 'idn-hostname',
'ipv4', 'ipv6', 'iri-reference', 'iri', 'json-pointer', 'regex',
'relative-json-pointer', 'time', 'unknown', 'uri-reference', 'uri-template',
'uri'
]),
'draft6': new Set(),
'draft6/optional': new Set(),
'draft6/optional/format': new Set([
'date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'json-pointer',
'unknown', 'uri-reference', 'uri-template', 'uri'
]),
'draft4': new Set(),
'draft4/optional': new Set(['zeroTerminatedFloats']),
'draft4/optional/format': new Set([
'date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'uri'
])
};
const DIALECTS = {
'draft2020-12': 'https://json-schema.org/draft/2020-12/schema',
'draft2019-09': 'https://json-schema.org/draft/2019-09/schema',
'draft7': 'http://json-schema.org/draft-07/schema#',
'draft6': 'http://json-schema.org/draft-06/schema#',
'draft4': 'http://json-schema.org/draft-04/schema#'
};
function registerTests(subdirectory, defaultDialect, blacklist) {
const testsDir = join(SUITE_PATH, 'tests', subdirectory);
if (!existsSync(testsDir)) return;
const files = readdirSync(testsDir)
.filter(file => file.endsWith('.json'))
.sort();
describe(subdirectory, () => {
for (const file of files) {
const name = basename(file, '.json');
if (blacklist.has(name)) continue;
const filePath = join(testsDir, file);
console.error(`Loading ${subdirectory}/${name}`);
const suite = JSON.parse(readFileSync(filePath, 'utf-8'), Blaze.reviver);
for (let groupIndex = 0; groupIndex < suite.length; groupIndex++) {
const testGroup = suite[groupIndex];
for (const mode of ['fast', 'exhaustive']) {
let evaluator;
try {
evaluator = new Blaze(compileSchema(filePath, {
mode,
defaultDialect,
resolveDirectory: RESOLVE_DIRECTORY,
path: `/${groupIndex}/schema`
}));
} catch (error) {
it(`[${mode}] ${testGroup.description} (compile error: ${error.message})`, () => {
assert.fail(`Compilation failed: ${error.message}`);
});
continue;
}
for (const testCase of testGroup.tests) {
const testName = `[${mode}] ${testGroup.description} - ${testCase.description}`;
it(testName, () => {
const result = evaluator.validate(testCase.data);
assert.equal(result, testCase.valid,
`Expected ${testCase.valid} but got ${result}`);
});
}
}
}
}
});
}
for (const [subdirectory, blacklist] of Object.entries(BLACKLISTS)) {
const draftKey = subdirectory.split('/')[0];
const defaultDialect = DIALECTS[draftKey];
registerTests(subdirectory, defaultDialect, blacklist);
}
describe('reviver', () => {
it('does not crash when the third argument is undefined', () => {
assert.doesNotThrow(() => {
Blaze.reviver('key', 42, undefined);
});
});
it('returns the value unchanged when context is missing', () => {
assert.equal(Blaze.reviver('key', 42, undefined), 42);
});
it('handles uppercase E in exponential notation', () => {
const result = Blaze.reviver('key', 1e+35, { source: '9.9999999999999999999999999999999999E+34' });
assert.equal(result, 99999999999999999999999999999999999n);
});
});
describe('version', () => {
it('rejects a template with an unsupported version', () => {
const template = [6, false, false, [[]], []];
assert.throws(() => new Blaze(template), {
message: 'Only version 5 of the compiled template is supported by this version of the evaluator'
});
});
it('rejects a template that is not an array', () => {
assert.throws(() => new Blaze({}), {
message: 'Only version 5 of the compiled template is supported by this version of the evaluator'
});
});
});