Skip to content
Closed
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
12 changes: 0 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ function buildCode (location) {
}

const schema = location.schema
const required = schema.required || []

let code = ''

Expand Down Expand Up @@ -351,23 +350,12 @@ function buildCode (location) {
${addComma}
json += ${asString} + ':' + ${JSON.stringify(JSON.stringify(defaultValue))}
`
} else if (required.includes(key)) {
code += `
} else {
throw new Error('${sanitized} is required!')
`
}

code += `
}
`
})

for (const requiredProperty of required) {
if (schema.properties && schema.properties[requiredProperty] !== undefined) continue
code += `if (obj['${requiredProperty}'] === undefined) throw new Error('"${requiredProperty}" is required!')\n`
}

return code
}

Expand Down
18 changes: 1 addition & 17 deletions test/allof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test('allOf: combine pattern properties', (t) => {
})

test('object with allOf and multiple schema on the allOf', (t) => {
t.plan(4)
t.plan(2)

const schema = {
title: 'object with allOf and multiple schema on the allOf',
Expand Down Expand Up @@ -95,22 +95,6 @@ test('object with allOf and multiple schema on the allOf', (t) => {
}
const stringify = build(schema)

try {
stringify({
id: 1
})
} catch (e) {
t.equal(e.message, '"name" is required!')
}

try {
stringify({
name: 'string'
})
} catch (e) {
t.equal(e.message, '"id" is required!')
}

t.equal(stringify({
id: 1,
name: 'string'
Expand Down
238 changes: 0 additions & 238 deletions test/required.test.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/sanitize4.test.js

This file was deleted.

7 changes: 1 addition & 6 deletions test/toJSON.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ test('toJSON forwards nullable types', (t) => {
})

test('toJSON supports required types', (t) => {
t.plan(2)
t.plan(1)

const stringify = build({
title: 'object of required primitive (json) types',
Expand Down Expand Up @@ -1134,11 +1134,6 @@ test('toJSON supports required types', (t) => {
}
const expected = '{"_bool":true,"_int":42,"_null":null,"_num":3.14,"_str":"whatever"}'
t.equal(stringify(input), expected)

const invalidInput = {
toJSON () { return {} }
}
t.throws(() => { stringify(invalidInput) })
})

test('use toJSON recursively', (t) => {
Expand Down