Skip to content

Commit 9e21900

Browse files
authored
expose only location instead of Object value (#574)
1 parent ed7cca7 commit 9e21900

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,12 @@ function buildMultiTypeSerializer (location, input) {
734734
}
735735
}
736736
})
737+
let schemaRef = location.getSchemaRef()
738+
if (schemaRef.startsWith(rootSchemaId)) {
739+
schemaRef = schemaRef.replace(rootSchemaId, '')
740+
}
737741
code += `
738-
else throw new Error(\`The value $\{JSON.stringify(${input})} does not match schema definition.\`)
742+
else throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
739743
`
740744

741745
return code
@@ -854,8 +858,13 @@ function buildValue (location, input) {
854858
`
855859
}
856860

861+
let schemaRef = location.getSchemaRef()
862+
if (schemaRef.startsWith(rootSchemaId)) {
863+
schemaRef = schemaRef.replace(rootSchemaId, '')
864+
}
865+
857866
code += `
858-
else throw new Error(\`The value $\{JSON.stringify(${input})} does not match schema definition.\`)
867+
else throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
859868
`
860869
return code
861870
}

test/any.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,80 @@ test('empty schema on anyOf', (t) => {
152152
t.equal(stringify({ kind: 'Foo', value: true }), '{"kind":"Foo","value":true}')
153153
t.equal(stringify({ kind: 'Foo', value: 'hello' }), '{"kind":"Foo","value":"hello"}')
154154
})
155+
156+
test('should throw a TypeError with the path to the key of the invalid value /1', (t) => {
157+
t.plan(1)
158+
159+
// any on Foo codepath.
160+
const schema = {
161+
anyOf: [
162+
{
163+
type: 'object',
164+
properties: {
165+
kind: {
166+
type: 'string',
167+
enum: ['Foo']
168+
},
169+
value: {}
170+
}
171+
},
172+
{
173+
type: 'object',
174+
properties: {
175+
kind: {
176+
type: 'string',
177+
enum: ['Bar']
178+
},
179+
value: {
180+
type: 'number'
181+
}
182+
}
183+
}
184+
]
185+
}
186+
187+
const stringify = build(schema)
188+
189+
t.throws(() => stringify({ kind: 'Baz', value: 1 }), new TypeError('The value of \'#\' does not match schema definition.'))
190+
})
191+
192+
test('should throw a TypeError with the path to the key of the invalid value /2', (t) => {
193+
t.plan(1)
194+
195+
// any on Foo codepath.
196+
const schema = {
197+
type: 'object',
198+
properties: {
199+
data: {
200+
anyOf: [
201+
{
202+
type: 'object',
203+
properties: {
204+
kind: {
205+
type: 'string',
206+
enum: ['Foo']
207+
},
208+
value: {}
209+
}
210+
},
211+
{
212+
type: 'object',
213+
properties: {
214+
kind: {
215+
type: 'string',
216+
enum: ['Bar']
217+
},
218+
value: {
219+
type: 'number'
220+
}
221+
}
222+
}
223+
]
224+
}
225+
}
226+
}
227+
228+
const stringify = build(schema)
229+
230+
t.throws(() => stringify({ data: { kind: 'Baz', value: 1 } }), new TypeError('The value of \'#/properties/data\' does not match schema definition.'))
231+
})

test/array.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ test('array items is a list of schema and additionalItems is false /1', (t) => {
283283
}
284284

285285
const stringify = build(schema)
286-
287286
t.throws(() => stringify({ foo: ['foo', 'bar'] }), new Error('Item at 1 does not match schema definition.'))
288287
})
289288

test/multi-type-serializer.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('should throw a TypeError with the path to the key of the invalid value', (t) => {
7+
t.plan(1)
8+
const schema = {
9+
type: 'object',
10+
properties: {
11+
num: {
12+
type: ['number']
13+
}
14+
}
15+
}
16+
17+
const stringify = build(schema)
18+
t.throws(() => stringify({ num: { bla: 123 } }), new TypeError('The value of \'#/properties/num\' does not match schema definition.'))
19+
})

0 commit comments

Comments
 (0)