Spaces:
Runtime error
Runtime error
File size: 3,879 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
'use strict'
const { test } = require('node:test')
const build = require('..')
test('patternProperties', (t) => {
t.plan(1)
const stringify = build({
title: 'patternProperties',
type: 'object',
properties: {
str: {
type: 'string'
}
},
patternProperties: {
foo: {
type: 'string'
}
}
})
const obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: { a: true }, notMe: false }
t.assert.equal(stringify(obj), '{"str":"test","foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]"}')
})
test('patternProperties should not change properties', (t) => {
t.plan(1)
const stringify = build({
title: 'patternProperties should not change properties',
type: 'object',
properties: {
foo: {
type: 'string'
}
},
patternProperties: {
foo: {
type: 'number'
}
}
})
const obj = { foo: '42', ofoo: 42 }
t.assert.equal(stringify(obj), '{"foo":"42","ofoo":42}')
})
test('patternProperties - string coerce', (t) => {
t.plan(1)
const stringify = build({
title: 'check string coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'string'
}
}
})
const obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
t.assert.equal(stringify(obj), '{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}')
})
test('patternProperties - number coerce', (t) => {
t.plan(2)
const stringify = build({
title: 'check number coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'number'
}
}
})
const coercibleValues = { foo: true, ofoo: '42' }
t.assert.equal(stringify(coercibleValues), '{"foo":1,"ofoo":42}')
const incoercibleValues = { xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
try {
stringify(incoercibleValues)
t.fail('should throw an error')
} catch (err) {
t.assert.ok(err)
}
})
test('patternProperties - boolean coerce', (t) => {
t.plan(1)
const stringify = build({
title: 'check boolean coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'boolean'
}
}
})
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } }
t.assert.equal(stringify(obj), '{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}')
})
test('patternProperties - object coerce', (t) => {
t.plan(1)
const stringify = build({
title: 'check object coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'object',
properties: {
answer: {
type: 'number'
}
}
}
}
})
const obj = { objfoo: { answer: 42 } }
t.assert.equal(stringify(obj), '{"objfoo":{"answer":42}}')
})
test('patternProperties - array coerce', (t) => {
t.plan(2)
const stringify = build({
title: 'check array coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'array',
items: {
type: 'string'
}
}
}
})
const coercibleValues = { arrfoo: [1, 2] }
t.assert.equal(stringify(coercibleValues), '{"arrfoo":["1","2"]}')
const incoercibleValues = { foo: 'true', ofoo: 0, objfoo: { tyrion: 'lannister' } }
t.assert.throws(() => stringify(incoercibleValues))
})
test('patternProperties - fail on invalid regex, handled by ajv', (t) => {
t.plan(1)
t.assert.throws(() => build({
title: 'check array coerce',
type: 'object',
properties: {},
patternProperties: {
'foo/\\': {
type: 'array',
items: {
type: 'string'
}
}
}
}), new Error('schema is invalid: data/patternProperties must match format "regex"'))
})
|