Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit ce9c892

Browse files
committed
test: validate all remote test casesby json schema
1 parent a0db8a7 commit ce9c892

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tests/ajv4.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Ajv = require('ajv')
2+
const metaSchema = require('ajv/lib/refs/json-schema-draft-04.json')
3+
4+
// Needed to continue to use draft-04 schemas
5+
// @link https://github.com/epoberezkin/ajv/releases/tag/5.0.0
6+
const Ajv4 = (options = {}, ...args) => {
7+
const ajv = new Ajv({
8+
...options,
9+
meta: false, // optional, to prevent adding draft-06 meta-schema
10+
extendRefs: true, // optional, current default is to 'fail', spec behaviour is to 'ignore'
11+
unknownFormats: 'ignore', // optional, current default is true (fail)
12+
}, ...args)
13+
14+
ajv.addMetaSchema(metaSchema)
15+
ajv._opts.defaultMeta = metaSchema.id
16+
17+
// optional, using unversioned URI is out of spec, see https://github.com/json-schema-org/json-schema-spec/issues/216
18+
ajv._refs['http://json-schema.org/schema'] = 'http://json-schema.org/draft-04/schema'
19+
20+
// Optionally you can also disable keywords defined in draft-06
21+
ajv.removeKeyword('propertyNames')
22+
ajv.removeKeyword('contains')
23+
ajv.removeKeyword('const')
24+
25+
return ajv
26+
}
27+
28+
module.exports = Ajv4

tests/tests.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
/* global QUnit */
22

33
const http = require('http');
4+
const path = require('path');
45
const fs = require('fs');
6+
const glob = require('glob');
57
const jsonLogic = require('../dist/jsonLogic.js');
8+
const Ajv = require('./ajv4');
9+
const jsonLogicSchema = require('../schemas/json-logic.json');
10+
11+
const ajv = Ajv({
12+
// avoid no schema with key or ref "http://json-schema.org/draft-04/schema"
13+
validateSchema: false,
14+
});
15+
16+
const cwd = __dirname;
17+
glob.sync('../schemas/**/*.json', { cwd }).forEach(file => {
18+
// eslint-disable-next-line
19+
const schema = require(path.resolve(cwd, file));
20+
21+
ajv.addSchema(schema);
22+
});
23+
24+
const validate = ajv.compile(jsonLogicSchema)
625

726
const download = (url, dest, cb) => {
827
const file = fs.createWriteStream(dest);
@@ -80,6 +99,8 @@ remote_or_cache(
8099
data
81100
)}) === ${JSON.stringify(expected)}`
82101
);
102+
103+
assert.equal(validate(rule), true, JSON.stringify(rule) + JSON.stringify(validate.errors, null, 2));
83104
}
84105
);
85106

@@ -99,6 +120,8 @@ remote_or_cache(
99120
pattern
100121
)}) === ${JSON.stringify(expected)}`
101122
);
123+
124+
assert.equal(validate(rule), true, JSON.stringify(rule));
102125
}
103126
);
104127

0 commit comments

Comments
 (0)