diff --git a/index.js b/index.js index 925954de..05c15170 100644 --- a/index.js +++ b/index.js @@ -319,7 +319,6 @@ function buildCode (location) { } const schema = location.schema - const required = schema.required || [] let code = '' @@ -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 } diff --git a/test/allof.test.js b/test/allof.test.js index 019076fe..89bfb79d 100644 --- a/test/allof.test.js +++ b/test/allof.test.js @@ -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', @@ -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' diff --git a/test/required.test.js b/test/required.test.js deleted file mode 100644 index f2aeb119..00000000 --- a/test/required.test.js +++ /dev/null @@ -1,238 +0,0 @@ -'use strict' - -const test = require('tap').test -const build = require('..') - -test('object with required field', (t) => { - t.plan(3) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - str: { - type: 'string' - }, - num: { - type: 'integer' - } - }, - required: ['str'] - } - const stringify = build(schema) - - stringify({ - str: 'string' - }) - t.pass() - - try { - stringify({ - num: 42 - }) - t.fail() - } catch (e) { - t.equal(e.message, '"str" is required!') - t.pass() - } -}) - -test('object with required field not in properties schema', (t) => { - t.plan(4) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - num: { - type: 'integer' - } - }, - required: ['str'] - } - const stringify = build(schema) - - try { - stringify({}) - t.fail() - } catch (e) { - t.equal(e.message, '"str" is required!') - t.pass() - } - - try { - stringify({ - num: 42 - }) - t.fail() - } catch (e) { - t.equal(e.message, '"str" is required!') - t.pass() - } -}) - -test('object with required field not in properties schema with additional properties true', (t) => { - t.plan(4) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - num: { - type: 'integer' - } - }, - additionalProperties: true, - required: ['str'] - } - const stringify = build(schema) - - try { - stringify({}) - t.fail() - } catch (e) { - t.equal(e.message, '"str" is required!') - t.pass() - } - - try { - stringify({ - num: 42 - }) - t.fail() - } catch (e) { - t.equal(e.message, '"str" is required!') - t.pass() - } -}) - -test('object with multiple required field not in properties schema', (t) => { - t.plan(6) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - num: { - type: 'integer' - } - }, - additionalProperties: true, - required: ['num', 'key1', 'key2'] - } - const stringify = build(schema) - - try { - stringify({}) - t.fail() - } catch (e) { - t.equal(e.message, '"num" is required!') - t.pass() - } - - try { - stringify({ - num: 42 - }) - t.fail() - } catch (e) { - t.equal(e.message, '"key1" is required!') - t.pass() - } - - try { - stringify({ - num: 42, - key1: 'some' - }) - t.fail() - } catch (e) { - t.equal(e.message, '"key2" is required!') - t.pass() - } -}) - -test('object with required bool', (t) => { - t.plan(2) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - num: { - type: 'integer' - } - }, - additionalProperties: true, - required: ['bool'] - } - const stringify = build(schema) - - try { - stringify({}) - t.fail() - } catch (e) { - t.equal(e.message, '"bool" is required!') - t.pass() - } - - stringify({ - bool: false - }) -}) - -test('required nullable', (t) => { - t.plan(1) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - num: { - type: ['integer'] - } - }, - additionalProperties: true, - required: ['null'] - } - const stringify = build(schema) - - stringify({ - null: null - }) - t.pass() -}) - -test('required numbers', (t) => { - t.plan(3) - - const schema = { - title: 'object with required field', - type: 'object', - properties: { - str: { - type: 'string' - }, - num: { - type: 'integer' - } - }, - required: ['num'] - } - const stringify = build(schema) - - stringify({ - num: 42 - }) - t.pass() - - try { - stringify({ - num: 'aaa' - }) - t.fail() - } catch (e) { - t.equal(e.message, 'The value "aaa" cannot be converted to an integer.') - t.pass() - } -}) diff --git a/test/sanitize4.test.js b/test/sanitize4.test.js deleted file mode 100644 index 5ec869a4..00000000 --- a/test/sanitize4.test.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict' - -const t = require('tap') -const build = require('..') - -const payload = '(throw "pwoned")' - -const stringify = build({ - required: [`"];${payload}//`] -}) - -t.throws(() => { - stringify({}) -}, 'Error: ""];(throw "pwoned")//" is required!') diff --git a/test/toJSON.test.js b/test/toJSON.test.js index c24b2503..393488f6 100644 --- a/test/toJSON.test.js +++ b/test/toJSON.test.js @@ -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', @@ -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) => {