Skip to content

Commit b1535a3

Browse files
committed
fix: Replace ajv@6 with ajv-draft-04
Support JSON Schema draft 04 by a more modern package.
1 parent 2d69b4f commit b1535a3

11 files changed

+36
-42
lines changed

.ncurc.yml

-2
This file was deleted.

lib/validator.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
if (typeof exports === 'object' && typeof module !== 'undefined') {
33
const jsonlint = require('./jsonlint')
44
const ajv = {
5-
AjvOld: 'ajv6',
5+
Ajv04: 'ajv-draft-04',
66
Ajv07: 'ajv',
77
AjvJTD: 'ajv/dist/jtd',
88
Ajv2019: 'ajv/dist/2019',
99
Ajv2020: 'ajv/dist/2020',
10-
Schema04: 'ajv6/lib/refs/json-schema-draft-04.json',
1110
Schema06: 'ajv/dist/refs/json-schema-draft-06.json'
1211
}
1312
const requireAjv = name => {
@@ -16,20 +15,18 @@
1615
}
1716
factory(exports, jsonlint, requireAjv)
1817
} else if (typeof define === 'function' && define.amd) {
19-
define('jsonlint-validator', ['exports', 'jsonlint', 'ajv', 'ajv7'],
20-
function (exports, jsonlint, ajv, ajv7) {
18+
define('jsonlint-validator', ['exports', 'jsonlint', 'ajv'],
19+
function (exports, jsonlint, ajv) {
2120
const requireAjv = name => {
22-
if (name === 'AjvOld') return ajv
23-
const exported = ajv7[name]
21+
const exported = ajv[name]
2422
return !exported.$schema && exported.default || exported
2523
}
2624
factory(exports, jsonlint, requireAjv)
2725
})
2826
} else {
2927
global = global || self
3028
const requireAjv = name => {
31-
if (name === 'AjvOld') return global.Ajv
32-
const exported = global.ajv7[name]
29+
const exported = global.ajv[name]
3330
return !exported.$schema && exported.default || exported
3431
}
3532
factory(global.jsonlintValidator = {}, global.jsonlint, requireAjv)
@@ -114,9 +111,8 @@
114111
const Ajv = requireAjv('Ajv07')
115112
ajv = new Ajv()
116113
} 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'))
114+
const Ajv = requireAjv('Ajv04')
115+
ajv = new Ajv()
120116
} else if (environment === 'json-schema-draft-2019-09' || environment === 'draft-2019-09') {
121117
const Ajv = requireAjv('Ajv2019')
122118
ajv = new Ajv()

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"scripts": {
4242
"build": "npm run compile:jsonlint && rollup -c && npm run minify && npm run compile:tests",
4343
"compile:jsonlint": "cat.js src/prefix.js.txt src/unicode.js src/custom-parser.js src/pointer.js src/native-parser.js src/configurable-parser.js src/suffix.js.txt > lib/jsonlint.js",
44-
"minify": "esbuild --minify --sourcemap --outfile=web/jsonlint.min.js lib/jsonlint.js && esbuild --minify --sourcemap --outfile=web/validator.min.js lib/validator.js && esbuild --minify --sourcemap --outfile=web/formatter.min.js lib/formatter.js && esbuild --minify --sourcemap --outfile=web/sorter.min.js lib/sorter.js && esbuild --minify --sourcemap --outfile=web/printer.min.js lib/printer.js && esbuild --minify --sourcemap --outfile=web/ajv.min.js node_modules/ajv6/dist/ajv.bundle.js",
44+
"minify": "esbuild --minify --sourcemap --outfile=web/jsonlint.min.js lib/jsonlint.js && esbuild --minify --sourcemap --outfile=web/validator.min.js lib/validator.js && esbuild --minify --sourcemap --outfile=web/formatter.min.js lib/formatter.js && esbuild --minify --sourcemap --outfile=web/sorter.min.js lib/sorter.js && esbuild --minify --sourcemap --outfile=web/printer.min.js lib/printer.js",
4545
"compile:tests": "tsc --moduleResolution node --module es2022 test/types.test.ts && mv.js test/types.test.js test/types.test.mjs",
4646
"test": "denolint && c8 node test/types.test.mjs && c8 --no-clean node test/parse1 && c8 --no-clean node test/parse1 --native-parser && c8 --no-clean node test/parse2 && c8 --no-clean node test/parse3 && c8 --no-clean node test/parse4 && c8 --no-clean node test/parse5 && c8 --no-clean node test/portable && c8 --no-clean node test/tokenize && c8 --no-clean node test/print && c8 --no-clean node lib/cli package.json test/recursive && c8 --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && c8 --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/schema-04.json test/passes/data-04.json && c8 --no-clean node lib/cli -s -e json-schema-draft-07 -V test/passes/schema-07.json test/passes/data-07.json && c8 --no-clean node lib/cli -C test/passes/comments.txt && c8 --no-clean node lib/cli -pS test/passes/strings.txt && c8 --no-clean node lib/cli -M json5 test/passes/json5.text && c8 --no-clean node lib/cli -v && c8 --no-clean node lib/cli -h && c8 --no-clean node lib/cli -Pc test/fails/10.json || c8 --no-clean node lib/cli -f test/.jsonrc.yml 'test/**/*.json' '!**/fails' && c8 report",
4747
"start": "http-server -c 5",
@@ -74,7 +74,7 @@
7474
},
7575
"dependencies": {
7676
"ajv": "8.12.0",
77-
"ajv6": "npm:[email protected]",
77+
"ajv-draft-04": "1.0.0",
7878
"commander": "10.0.0",
7979
"cosmiconfig": "8.1.0",
8080
"diff": "5.1.0",

pnpm-lock.yaml

+10-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import json from '@rollup/plugin-json'
44
import { minify } from 'rollup-plugin-swc-minify'
55

66
export default {
7-
input: 'src/ajv7.js',
7+
input: 'src/ajv.js',
88
output: {
9-
file: 'web/ajv7.min.js',
9+
file: 'web/ajv.min.js',
1010
format: 'umd',
11-
name: 'ajv7',
11+
name: 'ajv',
1212
sourcemap: true,
1313
plugins: [minify()]
1414
},

src/ajv7.js src/ajv.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import Ajv04 from 'ajv-draft-04'
12
import Ajv07 from 'ajv'
23
import AjvJTD from 'ajv/dist/jtd'
34
import Ajv2019 from 'ajv/dist/2019'
45
import Ajv2020 from 'ajv/dist/2020'
56

6-
import Schema04 from 'ajv6/lib/refs/json-schema-draft-04.json'
77
import Schema06 from 'ajv/dist/refs/json-schema-draft-06.json'
88

9-
export { Ajv07, AjvJTD, Ajv2019, Ajv2020, Schema04, Schema06 }
9+
export { Ajv04, Ajv07, AjvJTD, Ajv2019, Ajv2020, Schema06 }

test/parse1.js

+7
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,10 @@ test('test schema validation failure', function () {
329329
validate(data, { ignoreComments: !nativeParser })
330330
}, 'should throw error')
331331
})
332+
333+
test('test JSON Type Definitions validation success', function () {
334+
const data = fs.readFileSync(path.join(__dirname, '/passes/data-jtd.json')).toString()
335+
const schema = fs.readFileSync(path.join(__dirname, '/passes/schema-jtd.json')).toString()
336+
const validate = validator.compile(schema, { environment: 'jtd' })
337+
assert.doesNotThrow(function () { validate(parse(data)) }, 'should pass')
338+
})

test/passes/schema-04.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/draft-04/schema#",
33
"type": "object",
4-
"title": "Schema for 3.json",
4+
"title": "Schema for data-04.json",
55
"properties": {
66
"JSON Test Pattern pass3": {
77
"type": "object",

test/passes/schema-07.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"type": "object",
3-
"title": "Schema for 4.json",
3+
"title": "Schema for data-07.json",
44
"properties": {
55
"test": {
66
"type": "string",

test/types.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import tehanu from 'tehanu'
22
import { strict as assert } from 'assert'
3-
import { parse } from '../lib/jsonlint'
4-
import { compile } from '../lib/validator'
5-
import { print } from '../lib/printer'
3+
import { parse } from '../lib/jsonlint.js'
4+
import { compile } from '../lib/validator.js'
5+
import { print } from '../lib/printer.js'
66

77
const test = tehanu(import.meta.url)
88

web/jsonlint.html

-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ <h2>Result</h2>
203203
<script src="sorter.min.js" defer></script>
204204
<script src="printer.min.js" defer></script>
205205
<script src="ajv.min.js" defer></script>
206-
<script src="ajv7.min.js" defer></script>
207206
<script src="validator.min.js" defer></script>
208207
<script defer>
209208
window.onload = function () {

0 commit comments

Comments
 (0)