Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,3 @@ yarn.lock
# editor files
.vscode
.idea

#tap files
.tap/
7 changes: 0 additions & 7 deletions .taprc

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint": "standard",
"lint:fix": "standard --fix",
"test:typescript": "tsd",
"test:unit": "tap",
"test:unit": "node --test",
"test": "npm run test:unit && npm run test:typescript"
},
"precommit": [
Expand Down Expand Up @@ -46,7 +46,6 @@
"is-my-json-valid": "^2.20.6",
"simple-git": "^3.23.0",
"standard": "^17.1.0",
"tap": "^19.2.5",
"tsd": "^0.31.0",
"webpack": "^5.90.3"
},
Expand Down
95 changes: 39 additions & 56 deletions test/additionalProperties.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const test = require('tap').test
const { describe } = require('node:test')
const { deepStrictEqual, throws } = require('node:assert')
const build = require('..')

test('additionalProperties', (t) => {
t.plan(1)
describe('additionalProperties', () => {
const stringify = build({
title: 'additionalProperties',
type: 'object',
Expand All @@ -19,11 +19,10 @@ test('additionalProperties', (t) => {
})

const obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: { a: true } }
t.equal(stringify(obj), '{"str":"test","foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]"}')
deepStrictEqual(stringify(obj), '{"str":"test","foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]"}')
})

test('additionalProperties should not change properties', (t) => {
t.plan(1)
describe('additionalProperties should not change properties', () => {
const stringify = build({
title: 'patternProperties should not change properties',
type: 'object',
Expand All @@ -38,11 +37,10 @@ test('additionalProperties should not change properties', (t) => {
})

const obj = { foo: '42', ofoo: 42 }
t.equal(stringify(obj), '{"foo":"42","ofoo":42}')
deepStrictEqual(stringify(obj), '{"foo":"42","ofoo":42}')
})

test('additionalProperties should not change properties and patternProperties', (t) => {
t.plan(1)
describe('additionalProperties should not change properties and patternProperties', () => {
const stringify = build({
title: 'patternProperties should not change properties',
type: 'object',
Expand All @@ -62,11 +60,10 @@ test('additionalProperties should not change properties and patternProperties',
})

const obj = { foo: '42', ofoo: 42, test: '42' }
t.equal(stringify(obj), '{"foo":"42","ofoo":"42","test":42}')
deepStrictEqual(stringify(obj), '{"foo":"42","ofoo":"42","test":42}')
})

test('additionalProperties set to true, use of fast-safe-stringify', (t) => {
t.plan(1)
describe('additionalProperties set to true, use of fast-safe-stringify', () => {
const stringify = build({
title: 'check string coerce',
type: 'object',
Expand All @@ -75,11 +72,10 @@ test('additionalProperties set to true, use of fast-safe-stringify', (t) => {
})

const obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
t.equal(stringify(obj), '{"foo":true,"ofoo":42,"arrfoo":["array","test"],"objfoo":{"a":"world"}}')
deepStrictEqual(stringify(obj), '{"foo":true,"ofoo":42,"arrfoo":["array","test"],"objfoo":{"a":"world"}}')
})

test('additionalProperties - string coerce', (t) => {
t.plan(1)
describe('additionalProperties - string coerce', () => {
const stringify = build({
title: 'check string coerce',
type: 'object',
Expand All @@ -90,11 +86,10 @@ test('additionalProperties - string coerce', (t) => {
})

const obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
t.equal(stringify(obj), '{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}')
deepStrictEqual(stringify(obj), '{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}')
})

test('additionalProperties - number skip', (t) => {
t.plan(1)
describe('additionalProperties - number skip', () => {
const stringify = build({
title: 'check number coerce',
type: 'object',
Expand All @@ -106,11 +101,10 @@ test('additionalProperties - number skip', (t) => {

// const obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
const obj = { foo: true, ofoo: '42' }
t.equal(stringify(obj), '{"foo":1,"ofoo":42}')
deepStrictEqual(stringify(obj), '{"foo":1,"ofoo":42}')
})

test('additionalProperties - boolean coerce', (t) => {
t.plan(1)
describe('additionalProperties - boolean coerce', () => {
const stringify = build({
title: 'check boolean coerce',
type: 'object',
Expand All @@ -121,11 +115,10 @@ test('additionalProperties - boolean coerce', (t) => {
})

const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } }
t.equal(stringify(obj), '{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}')
deepStrictEqual(stringify(obj), '{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}')
})

test('additionalProperties - object coerce', (t) => {
t.plan(1)
describe('additionalProperties - object coerce', () => {
const stringify = build({
title: 'check object coerce',
type: 'object',
Expand All @@ -141,11 +134,10 @@ test('additionalProperties - object coerce', (t) => {
})

const obj = { objfoo: { answer: 42 } }
t.equal(stringify(obj), '{"objfoo":{"answer":42}}')
deepStrictEqual(stringify(obj), '{"objfoo":{"answer":42}}')
})

test('additionalProperties - array coerce', (t) => {
t.plan(2)
describe('additionalProperties - array coerce', () => {
const stringify = build({
title: 'check array coerce',
type: 'object',
Expand All @@ -159,25 +151,23 @@ test('additionalProperties - array coerce', (t) => {
})

const coercibleValues = { arrfoo: [1, 2] }
t.equal(stringify(coercibleValues), '{"arrfoo":["1","2"]}')
deepStrictEqual(stringify(coercibleValues), '{"arrfoo":["1","2"]}')

const incoercibleValues = { foo: 'true', ofoo: 0, objfoo: { tyrion: 'lannister' } }
t.throws(() => stringify(incoercibleValues))
throws(() => stringify(incoercibleValues))
})

test('additionalProperties with empty schema', (t) => {
t.plan(1)
describe('additionalProperties with empty schema', () => {
const stringify = build({
type: 'object',
additionalProperties: {}
})

const obj = { a: 1, b: true, c: null }
t.equal(stringify(obj), '{"a":1,"b":true,"c":null}')
deepStrictEqual(stringify(obj), '{"a":1,"b":true,"c":null}')
})

test('additionalProperties with nested empty schema', (t) => {
t.plan(1)
describe('additionalProperties with nested empty schema', () => {
const stringify = build({
type: 'object',
properties: {
Expand All @@ -187,11 +177,10 @@ test('additionalProperties with nested empty schema', (t) => {
})

const obj = { data: { a: 1, b: true, c: null } }
t.equal(stringify(obj), '{"data":{"a":1,"b":true,"c":null}}')
deepStrictEqual(stringify(obj), '{"data":{"a":1,"b":true,"c":null}}')
})

test('nested additionalProperties', (t) => {
t.plan(1)
describe('nested additionalProperties', () => {
const stringify = build({
title: 'additionalProperties',
type: 'array',
Expand All @@ -207,11 +196,10 @@ test('nested additionalProperties', (t) => {
})

const obj = [{ ap: { value: 'string' } }]
t.equal(stringify(obj), '[{"ap":{"value":"string"}}]')
deepStrictEqual(stringify(obj), '[{"ap":{"value":"string"}}]')
})

test('very nested additionalProperties', (t) => {
t.plan(1)
describe('very nested additionalProperties', () => {
const stringify = build({
title: 'additionalProperties',
type: 'array',
Expand Down Expand Up @@ -244,11 +232,10 @@ test('very nested additionalProperties', (t) => {
})

const obj = [{ ap: { nested: { moarNested: { finally: { value: 'str' } } } } }]
t.equal(stringify(obj), '[{"ap":{"nested":{"moarNested":{"finally":{"value":"str"}}}}}]')
deepStrictEqual(stringify(obj), '[{"ap":{"nested":{"moarNested":{"finally":{"value":"str"}}}}}]')
})

test('nested additionalProperties set to true', (t) => {
t.plan(1)
describe('nested additionalProperties set to true', () => {
const stringify = build({
title: 'nested additionalProperties=true',
type: 'object',
Expand All @@ -261,11 +248,10 @@ test('nested additionalProperties set to true', (t) => {
})

const obj = { ap: { value: 'string', someNumber: 42 } }
t.equal(stringify(obj), '{"ap":{"value":"string","someNumber":42}}')
deepStrictEqual(stringify(obj), '{"ap":{"value":"string","someNumber":42}}')
})

test('field passed to fastSafeStringify as undefined should be removed', (t) => {
t.plan(1)
describe('field passed to fastSafeStringify as undefined should be removed', () => {
const stringify = build({
title: 'nested additionalProperties=true',
type: 'object',
Expand All @@ -278,11 +264,10 @@ test('field passed to fastSafeStringify as undefined should be removed', (t) =>
})

const obj = { ap: { value: 'string', someNumber: undefined } }
t.equal(stringify(obj), '{"ap":{"value":"string"}}')
deepStrictEqual(stringify(obj), '{"ap":{"value":"string"}}')
})

test('property without type but with enum, will acts as additionalProperties', (t) => {
t.plan(1)
describe('property without type but with enum, will acts as additionalProperties', () => {
const stringify = build({
title: 'automatic additionalProperties',
type: 'object',
Expand All @@ -294,11 +279,10 @@ test('property without type but with enum, will acts as additionalProperties', (
})

const obj = { ap: { additional: 'field' } }
t.equal(stringify(obj), '{"ap":{"additional":"field"}}')
deepStrictEqual(stringify(obj), '{"ap":{"additional":"field"}}')
})

test('property without type but with enum, will acts as additionalProperties without overwriting', (t) => {
t.plan(1)
describe('property without type but with enum, will acts as additionalProperties without overwriting', () => {
const stringify = build({
title: 'automatic additionalProperties',
type: 'object',
Expand All @@ -311,11 +295,10 @@ test('property without type but with enum, will acts as additionalProperties wit
})

const obj = { ap: { additional: 'field' } }
t.equal(stringify(obj), '{"ap":{}}')
deepStrictEqual(stringify(obj), '{"ap":{}}')
})

test('function and symbol references are not serialized as undefined', (t) => {
t.plan(1)
describe('function and symbol references are not serialized as undefined', () => {
const stringify = build({
title: 'additionalProperties',
type: 'object',
Expand All @@ -328,5 +311,5 @@ test('function and symbol references are not serialized as undefined', (t) => {
})

const obj = { str: 'x', test: 'test', meth: () => 'x', sym: Symbol('x') }
t.equal(stringify(obj), '{"str":"x","test":"test"}')
deepStrictEqual(stringify(obj), '{"str":"x","test":"test"}')
})
Loading