|
1 | 1 | (function (global, factory) {
|
2 | 2 | if (typeof exports === 'object' && typeof module !== 'undefined') {
|
3 | 3 | const jsonlint = require('./jsonlint')
|
4 |
| - const Ajv = require('ajv') |
5 |
| - // eslint-disable-next-line no-inner-declarations |
6 |
| - function requireSchemaDraft (environment) { |
7 |
| - return require('ajv/lib/refs/' + environment + '.json') |
| 4 | + const ajv = { |
| 5 | + AjvOld: 'ajv6', |
| 6 | + Ajv07: 'ajv', |
| 7 | + AjvJTD: 'ajv/dist/jtd', |
| 8 | + Ajv2019: 'ajv/dist/2019', |
| 9 | + Ajv2020: 'ajv/dist/2020', |
| 10 | + Schema04: 'ajv6/lib/refs/json-schema-draft-04.json', |
| 11 | + Schema06: 'ajv/dist/refs/json-schema-draft-06.json' |
8 | 12 | }
|
9 |
| - factory(exports, Ajv, jsonlint, requireSchemaDraft) |
10 |
| - // eslint-disable-next-line no-undef |
| 13 | + const requireAjv = name => { |
| 14 | + const exported = require(ajv[name]) |
| 15 | + return !exported.$schema && exported.default || exported |
| 16 | + } |
| 17 | + factory(exports, jsonlint, requireAjv) |
11 | 18 | } else if (typeof define === 'function' && define.amd) {
|
12 |
| - // eslint-disable-next-line no-undef |
13 |
| - define('jsonlint-validator', ['exports', 'ajv', 'jsonlint', 'jsonlint-schema-drafts'], |
14 |
| - function (exports, jsonlint, Ajv, schemaDrafts) { |
15 |
| - function requireSchemaDraft (environment) { |
16 |
| - return schemaDrafts[environment] |
| 19 | + define('jsonlint-validator', ['exports', 'jsonlint', 'ajv', 'ajv7'], |
| 20 | + function (exports, jsonlint, ajv, ajv7) { |
| 21 | + const requireAjv = name => { |
| 22 | + if (name === 'AjvOld') return ajv |
| 23 | + const exported = ajv7[name] |
| 24 | + return !exported.$schema && exported.default || exported |
17 | 25 | }
|
18 |
| - factory(exports, Ajv, jsonlint, requireSchemaDraft) |
| 26 | + factory(exports, jsonlint, requireAjv) |
19 | 27 | })
|
20 | 28 | } else {
|
21 |
| - // eslint-disable-next-line no-undef |
22 | 29 | global = global || self
|
23 |
| - const requireSchemaDraft = function (environment) { |
24 |
| - return global.jsonlintSchemaDrafts[environment] |
| 30 | + const requireAjv = name => { |
| 31 | + if (name === 'AjvOld') return global.Ajv |
| 32 | + const exported = global.ajv7[name] |
| 33 | + return !exported.$schema && exported.default || exported |
25 | 34 | }
|
26 |
| - factory(global.jsonlintValidator = {}, global.Ajv, global.jsonlint, requireSchemaDraft) |
| 35 | + factory(global.jsonlintValidator = {}, global.jsonlint, requireAjv) |
27 | 36 | }
|
28 |
| -}(this, function (exports, Ajv, jsonlint, requireSchemaDraft) { |
| 37 | +}(this, function (exports, jsonlint, requireAjv) { |
29 | 38 | 'use strict'
|
30 | 39 |
|
31 | 40 | function addErrorLocation (problem, input, tokens, dataPath) {
|
|
96 | 105 | }
|
97 | 106 |
|
98 | 107 | function createAjv (environment) {
|
99 |
| - const ajvOptions = { jsonPointers: true } |
100 | 108 | let ajv
|
101 |
| - if (!environment) { |
102 |
| - ajvOptions.schemaId = 'auto' |
103 |
| - ajv = new Ajv(ajvOptions) |
104 |
| - ajv.addMetaSchema(requireSchemaDraft('json-schema-draft-04')) |
105 |
| - ajv.addMetaSchema(requireSchemaDraft('json-schema-draft-06')) |
106 |
| - } else if (environment === 'json-schema-draft-07') { |
107 |
| - ajv = new Ajv(ajvOptions) |
108 |
| - } else if (environment === 'json-schema-draft-06') { |
109 |
| - ajv = new Ajv(ajvOptions) |
110 |
| - ajv.addMetaSchema(requireSchemaDraft('json-schema-draft-06')) |
111 |
| - } else if (environment === 'json-schema-draft-04') { |
112 |
| - ajvOptions.schemaId = 'id' |
113 |
| - ajv = new Ajv(ajvOptions) |
114 |
| - ajv.addMetaSchema(requireSchemaDraft('json-schema-draft-04')) |
| 109 | + if (!environment || environment === 'json-schema-draft-06' || environment === 'draft-06') { |
| 110 | + const Ajv = requireAjv('Ajv07') |
| 111 | + ajv = new Ajv() |
| 112 | + ajv.addMetaSchema(requireAjv('Schema06')) |
| 113 | + } else if (environment === 'json-schema-draft-07' || environment === 'draft-07') { |
| 114 | + const Ajv = requireAjv('Ajv07') |
| 115 | + ajv = new Ajv() |
| 116 | + } else if (environment === 'json-schema-draft-04' || environment === 'draft-04') { |
| 117 | + const Ajv = requireAjv('AjvOld') |
| 118 | + ajv = new Ajv({ schemaId: 'id' }) |
| 119 | + ajv.addMetaSchema(requireAjv('Schema04')) |
| 120 | + } else if (environment === 'json-schema-draft-2019-09' || environment === 'draft-2019-09') { |
| 121 | + const Ajv = requireAjv('Ajv2019') |
| 122 | + ajv = new Ajv() |
| 123 | + } else if (environment === 'json-schema-draft-2020-12' || environment === 'draft-2020-12') { |
| 124 | + const Ajv = requireAjv('Ajv2020') |
| 125 | + ajv = new Ajv() |
| 126 | + } else if (environment === 'json-type-definition' || environment === 'jtd' || environment === 'rfc8927') { |
| 127 | + const Ajv = requireAjv('AjvJTD') |
| 128 | + ajv = new Ajv() |
115 | 129 | } else {
|
116 |
| - throw new RangeError('Unsupported environment for the JSON schema validation: "' + |
| 130 | + throw new RangeError('Unsupported environment for the JSON Schema validation: "' + |
117 | 131 | environment + '".')
|
118 | 132 | }
|
119 | 133 | return ajv
|
|
124 | 138 | try {
|
125 | 139 | parsed = jsonlint.parse(schema, parseOptions)
|
126 | 140 | } catch (error) {
|
127 |
| - error.message = 'Parsing the JSON schema failed.\n' + error.message |
| 141 | + error.message = 'Parsing the JSON Schema failed.\n' + error.message |
128 | 142 | throw error
|
129 | 143 | }
|
130 | 144 | try {
|
|
134 | 148 | const betterError = errors
|
135 | 149 | ? createError(errors, parsed, schema, parseOptions)
|
136 | 150 | : originalError
|
137 |
| - betterError.message = 'Compiling the JSON schema failed.\n' + betterError.message |
| 151 | + betterError.message = 'Compiling the JSON Schema failed.\n' + betterError.message |
138 | 152 | throw betterError
|
139 | 153 | }
|
140 | 154 | }
|
|
0 commit comments