Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"compile-json-stringify": "^0.1.2",
"eslint": "^9.39.2",
"fast-json-stringify": ".",
"is-my-json-valid": "^2.20.6",
"json-accelerator": "^0.1.7",
"neostandard": "^0.13.0",
"simple-git": "^3.30.0",
Expand Down
2 changes: 1 addition & 1 deletion test/array.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')
const Ajv = require('ajv')

Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')

function buildTest (schema, toStringify) {
Expand Down
13 changes: 7 additions & 6 deletions test/const.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')

test('schema with const string', (t) => {
Expand Down Expand Up @@ -279,15 +279,17 @@ test('schema with const and null as type', (t) => {
})

t.assert.equal(output, '{"foo":null}')
t.assert.ok(validate(JSON.parse(output)), 'valid schema')
// `const` applies regardless of `type` in JSON Schema, so ajv rejects null
// here even though the serializer lets it through because the type allows it.
t.assert.equal(validate(JSON.parse(output)), false)

const output2 = stringify({ foo: 'baz' })
t.assert.equal(output2, '{"foo":"baz"}')
t.assert.ok(validate(JSON.parse(output2)), 'valid schema')
})

test('schema with const as nullable', (t) => {
t.plan(4)
t.plan(2)

const schema = {
type: 'object',
Expand All @@ -296,20 +298,19 @@ test('schema with const as nullable', (t) => {
}
}

const validate = validator(schema)
// ajv refuses to compile `nullable` without a `type`, so the output is not
// validated against the schema in this test.
const stringify = build(schema)
const output = stringify({
foo: null
})

t.assert.equal(output, '{"foo":null}')
t.assert.ok(validate(JSON.parse(output)), 'valid schema')

const output2 = stringify({
foo: 'baz'
})
t.assert.equal(output2, '{"foo":"baz"}')
t.assert.ok(validate(JSON.parse(output2)), 'valid schema')
})

test('schema with const and invalid object', (t) => {
Expand Down
2 changes: 1 addition & 1 deletion test/date.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')

process.env.TZ = 'UTC'
Expand Down
2 changes: 1 addition & 1 deletion test/inferType.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')

function buildTest (schema, toStringify) {
Expand Down
2 changes: 1 addition & 1 deletion test/integer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { test } = require('node:test')

const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')
const ROUNDING_TYPES = ['ceil', 'floor', 'round']

Expand Down
2 changes: 1 addition & 1 deletion test/regex.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')

test('object with RexExp', (t) => {
Expand Down
2 changes: 1 addition & 1 deletion test/surrogate.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('node:test')
const validator = require('is-my-json-valid')
const validator = require('./utils/validator')
const build = require('..')

test('render a string with surrogate pairs as JSON:test 1', (t) => {
Expand Down
20 changes: 20 additions & 0 deletions test/utils/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const Ajv = require('ajv')
const ajvFormats = require('ajv-formats')

// Compiles a JSON Schema into a `validate(data) => boolean` function with
// ajv, the validator the library itself uses, so tests check serialized
// output against the same rules. A fresh instance is created per schema so
// tests that reuse an `$id` do not collide.
module.exports = function validator (schema) {
const ajv = new Ajv({ strictSchema: false, allowUnionTypes: true })
ajvFormats(ajv)
// `unsafe` is a fast-json-stringify format that only disables escaping.
ajv.addFormat('unsafe', true)
// The serializer renders `format: time` as `HH:mm:ss` with no timezone
// (see `asTime` in lib/serializer.js), which ajv-formats rejects because
// RFC 3339 requires one. Validate the shape the library documents.
ajv.addFormat('time', /^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/)
return ajv.compile(schema)
}