Skip to content

Commit bd1d4ca

Browse files
committed
feat(env): [BREAKING CHANGE]
Make schema v6 default #48
1 parent cb4db59 commit bd1d4ca

10 files changed

Lines changed: 89 additions & 99 deletions

File tree

lib/djv.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ const { head } = require('./utils/uri');
22
const { restore } = require('./utils/template');
33
const formats = require('./utils/formats');
44
const { generate, State } = require('./utils/state');
5-
const updateEnvironment = require('./utils/environment.js');
5+
const { add, use } = require('./utils/environment.js');
66

77
/**
88
* Configuration for template
99
* @typedef {object} DjvConfig
1010
* @property {string?} version - defines which version of json-schema draft to use,
1111
* draft-04 by default
12+
* @property {function?} versionConfigure - handler to apply for environment version
1213
* @property {boolean?} inner - a generating object should be considered as inner
1314
* Default value is false/undefined.
1415
* If true, then it avoid creating variables in a generated function body,
@@ -40,7 +41,7 @@ function Environment(options = {}) {
4041
this.resolved = {};
4142
this.state = new State(null, this);
4243

43-
this.useVersion(options.version);
44+
this.useVersion(options.version, options.versionConfigure);
4445
this.addFormat(options.formats);
4546
}
4647

@@ -273,17 +274,27 @@ Environment.prototype = {
273274
setErrorHandler(errorHandler) {
274275
Object.assign(this.options, { errorHandler });
275276
},
276-
277277
/**
278-
* @name useVersion
279-
* @type {function}
280-
* @description
281-
* Updates internals utilities and configurations to fix versions implementation conflicts
282-
* @param {string} version of json-schema specification to use
283-
* @returns void
284-
*/
285-
useVersion(version) {
286-
updateEnvironment(version);
278+
* @name useVersion
279+
* @type {function}
280+
* @description
281+
* Add a specification version for environment
282+
* A configure function is called with exposed environments, like keys, formats, etc.
283+
* Updates internals utilities and configurations to fix versions implementation conflicts
284+
* @param {string} version of json-schema specification to use
285+
* @param {function} configure
286+
* @returns void
287+
*/
288+
useVersion(version, configure) {
289+
if (typeof configure !== 'function' && version === 'draft-04') {
290+
/* eslint-disable no-param-reassign, global-require, import/no-extraneous-dependencies */
291+
configure = require('@korzio/djv-draft-04');
292+
/* eslint-enable no-param-reassign, global-require, import/no-extraneous-dependencies */
293+
}
294+
if (typeof configure === 'function') {
295+
add(version, configure);
296+
}
297+
use(version);
287298
},
288299
};
289300

lib/utils/environment.js

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,38 @@
33
* @description
44
* Update the given environment
55
*/
6-
const properties = require('./properties');
7-
const keywords = require('./keywords');
8-
const validators = require('../validators');
9-
const formats = require('./formats');
10-
const { keys } = require('./uri');
11-
const { transformation } = require('./schema');
12-
13-
const contains = require('../validators/contains');
14-
const constant = require('../validators/const');
15-
const propertyNames = require('../validators/propertyNames');
16-
17-
const environmentConfig = {
18-
'draft-06': () => {
19-
Object.assign(properties, {
20-
exclusiveMinimum(schema) {
21-
return `%s <= ${schema.exclusiveMinimum}`;
22-
},
23-
minimum(schema) {
24-
return `%s < ${schema.minimum}`;
25-
},
26-
exclusiveMaximum(schema) {
27-
return `%s >= ${schema.exclusiveMaximum}`;
28-
},
29-
maximum(schema) {
30-
return `%s > ${schema.maximum}`;
31-
},
32-
});
33-
34-
['exclusiveMaximum', 'exclusiveMininum', 'id'].forEach((key) => {
35-
const index = keywords.indexOf(key);
36-
if (index === -1) {
37-
return;
38-
}
39-
40-
keywords.splice(index, 1);
41-
});
42-
43-
if (keywords.indexOf('contains') === -1) {
44-
keywords.push('$id', 'contains', 'const', 'examples');
45-
}
46-
47-
if (validators.list.indexOf(contains) === -1) {
48-
validators.list.push(
49-
contains,
50-
constant,
51-
propertyNames
52-
);
53-
}
54-
55-
Object.assign(validators.name, {
56-
contains,
57-
constant,
58-
propertyNames
59-
});
6+
const exposedProperties = require('./properties');
7+
const exposedKeywords = require('./keywords');
8+
const exposedValidators = require('../validators');
9+
const exposedFormats = require('./formats');
10+
const { keys: exposedKeys } = require('./uri');
11+
const { transformation: exposedTransformation } = require('./schema');
12+
13+
const exposed = {
14+
properties: exposedProperties,
15+
keywords: exposedKeywords,
16+
validators: exposedValidators,
17+
formats: exposedFormats,
18+
keys: exposedKeys,
19+
transformation: exposedTransformation,
20+
};
6021

61-
// TODO optimize uri-reference regex... too long
62-
Object.assign(formats, {
63-
'json-pointer': '!/^$|^\\/(?:~(?=[01])|[^~])*$/i.test(%s)', // add empty valid string,
64-
'uri-reference': '!/^(?:[A-Za-z][A-Za-z0-9+\\-.]*:(?:\\/\\/(?:(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*@)?(?:\\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\\.[A-Za-z0-9\\-._~!$&\'()*+,;=:]+)\\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-Za-z0-9\\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})*)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1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\\.[A-Za-z0-9\\-._~!$&\'()*+,;=:]+)\\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-Za-z0-9\\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})*)(?::[0-9]*)?(?:\\/(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*|\\/(?:(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:\\/(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)?|(?:[A-Za-z0-9\\-._~!$&\'()*+,;=@]|%[0-9A-Fa-f]{2})+(?:\\/(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*|)(?:\\?(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*)?(?:\\#(?:[A-Za-z0-9\\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*)?)$/i.test(%s)',
65-
'uri-template': '!/^(?:(?:[^\\x00-\\x20"\'<>%\\\\^`{|}]|%[0-9a-f]{2})|\\{[+#.\\/;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?:\\:[1-9][0-9]{0,3}|\\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?:\\:[1-9][0-9]{0,3}|\\*)?)*\\})*$/i.test(%s)',
66-
});
22+
const environmentConfig = {};
6723

68-
Object.assign(keys, {
69-
id: '$id',
70-
});
24+
function add(version, config) {
25+
environmentConfig[version] = config;
26+
}
7127

72-
Object.assign(transformation, {
73-
ANY_SCHEMA: {},
74-
NOT_ANY_SCHEMA: { not: {} },
75-
});
76-
},
77-
};
78-
79-
module.exports = function environment(version) {
28+
function use(version) {
8029
if (!version || !environmentConfig[version]) {
8130
return;
8231
}
8332

8433
const patchEnvironment = environmentConfig[version];
85-
patchEnvironment();
34+
patchEnvironment(exposed);
35+
}
36+
37+
module.exports = {
38+
add,
39+
use,
8640
};

lib/utils/formats.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/utils/keywords.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ module.exports = [
1111
'anyOf',
1212
'allOf',
1313
'oneOf',
14-
'id',
15-
'exclusiveMaximum',
16-
'exclusiveMininum',
1714
'properties',
1815
'patternProperties',
1916
'additionalProperties',
@@ -24,5 +21,9 @@ module.exports = [
2421
'title',
2522
'description',
2623
'definitions',
27-
'dependencies'
24+
'dependencies',
25+
'$id',
26+
'contains',
27+
'const',
28+
'examples'
2829
];

lib/utils/properties.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
*/
99
module.exports = {
1010
readOnly: 'false',
11+
exclusiveMinimum(schema) {
12+
return `%s <= ${schema.exclusiveMinimum}`;
13+
},
1114
minimum(schema) {
12-
return `%s <${schema.exclusiveMinimum ? '=' : ''} ${schema.minimum}`;
15+
return `%s < ${schema.minimum}`;
16+
},
17+
exclusiveMaximum(schema) {
18+
return `%s >= ${schema.exclusiveMaximum}`;
1319
},
1420
maximum(schema) {
15-
return `%s >${schema.exclusiveMaximum ? '=' : ''} ${schema.maximum}`;
21+
return `%s > ${schema.maximum}`;
1622
},
1723
multipleOf: '($1/$2) % 1 !== 0 && typeof $1 === "number"',
1824
// When the instance value is a string

lib/utils/schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* Schema values transformation
1212
*/
1313
const transformation = {
14-
ANY_SCHEMA: true,
15-
NOT_ANY_SCHEMA: false,
14+
ANY_SCHEMA: {},
15+
NOT_ANY_SCHEMA: { not: {} },
1616
};
1717

1818
/**

lib/utils/uri.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const REGEXP_URI_PATH = /(^[^:]+:\/\/[^?#]*\/).*/;
1414
* Keys to apply schema attributes & values
1515
*/
1616
const keys = {
17-
id: 'id',
17+
id: '$id',
1818
};
1919

2020
/**

lib/validators/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const dependencies = require('./dependencies');
1919
const properties = require('./properties');
2020
const patternProperties = require('./patternProperties');
2121
const items = require('./items');
22+
const contains = require('../validators/contains');
23+
const constant = require('../validators/const');
24+
const propertyNames = require('../validators/propertyNames');
2225

2326
module.exports = {
2427
name: {
@@ -35,6 +38,9 @@ module.exports = {
3538
properties,
3639
patternProperties,
3740
items,
41+
contains,
42+
constant,
43+
propertyNames,
3844
},
3945
list: [
4046
$ref,
@@ -49,6 +55,9 @@ module.exports = {
4955
dependencies,
5056
properties,
5157
patternProperties,
52-
items
58+
items,
59+
contains,
60+
constant,
61+
propertyNames
5362
]
5463
};

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"build": "webpack",
88
"changelog": "generate-changelog",
9+
"docs": "jsdoc",
910
"lint": "eslint ./lib",
1011
"prepublishOnly": "npm run test && npm run build",
1112
"test": "npm run test:jasmine && npm run lint",
@@ -37,12 +38,16 @@
3738
"eslint-plugin-import": "^2.7.0",
3839
"generate-changelog": "^1.2.1",
3940
"jasmine": "^2.5.2",
41+
"jsdoc": "^3.5.5",
4042
"json-schema-benchmark": "https://github.com/ebdrup/json-schema-benchmark.git",
4143
"json-schema-test": "^1.3.0",
4244
"json-schema-test-suite": "https://github.com/json-schema/JSON-Schema-Test-Suite.git",
4345
"source-map-loader": "^0.2.1",
4446
"webpack": "^3.0.0"
4547
},
48+
"optionalDependencies": {
49+
"@korzio/djv-draft-04": "^2.0.0"
50+
},
4651
"spec_dir": "test",
4752
"spec_files": [
4853
"**/*.js"

test/json-schema-test-suite.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ const factory = function djvTestSuiteAdapter(version) {
3434
};
3535
};
3636

37-
function runTest(draft) {
38-
jsonSchemaTest(factory(`draft-0${draft}`), {
39-
description: `Test suite draft-0${draft}`,
40-
suites: { tests: `../node_modules/json-schema-test-suite/tests/draft${draft}/{**/,}*.json` },
37+
function runTest(version) {
38+
jsonSchemaTest(factory(`draft-0${version}`), {
39+
description: `Test suite draft-0${version}`,
40+
suites: { tests: `../node_modules/json-schema-test-suite/tests/draft${version}/{**/,}*.json` },
4141
cwd: __dirname,
4242
hideFolder: 'tests/',
4343
skip: ['optional/zeroTerminatedFloats']
4444
});
4545
}
4646

47-
runTest(4);
4847
runTest(6);
48+
runTest(4);

0 commit comments

Comments
 (0)