|
3 | 3 | * @description |
4 | 4 | * Update the given environment |
5 | 5 | */ |
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 | +}; |
60 | 21 |
|
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\\-._~!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3}|\\*)?)*\\})*$/i.test(%s)', |
66 | | - }); |
| 22 | +const environmentConfig = {}; |
67 | 23 |
|
68 | | - Object.assign(keys, { |
69 | | - id: '$id', |
70 | | - }); |
| 24 | +function add(version, config) { |
| 25 | + environmentConfig[version] = config; |
| 26 | +} |
71 | 27 |
|
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) { |
80 | 29 | if (!version || !environmentConfig[version]) { |
81 | 30 | return; |
82 | 31 | } |
83 | 32 |
|
84 | 33 | const patchEnvironment = environmentConfig[version]; |
85 | | - patchEnvironment(); |
| 34 | + patchEnvironment(exposed); |
| 35 | +} |
| 36 | + |
| 37 | +module.exports = { |
| 38 | + add, |
| 39 | + use, |
86 | 40 | }; |
0 commit comments