Skip to content

feat: drop nullable const support #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
6 changes: 0 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,12 +765,6 @@ function buildValue (location, input) {
let funcName

if ('const' in schema) {
if (nullable) {
code += `
json += ${input} === null ? 'null' : '${JSON.stringify(schema.const)}'
`
return code
}
code += `json += '${JSON.stringify(schema.const)}'`
return code
}
Expand Down
4 changes: 2 additions & 2 deletions test/const.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ test('schema with const and null as type', (t) => {
foo: null
})

t.equal(output, '{"foo":null}')
t.equal(output, '{"foo":"baz"}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.stringify({foo: null }) is {"foo":null}

So how can it be that stringifying { foo: null } results in {"foo": "baz}

Copy link
Member Author

@ivan-tymoshenko ivan-tymoshenko Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FJS output should be equal to JSON.stringify only when you pass data that match the data schema. You shouldn't pass null as a data input for schema { "type": ["string", "null"], "const": "abc" }, because null doesn't match the schema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want this to work:

const schema = { /*some json schema draft 7*/ }
const data = { /*data that match to schema or can be coerced according to FJS rules */ }

const json = fjs(data)
const parsedData = JSON.parse(json)

validate(schema, data) // should be always true

And if we serialize null as null,

validate({ "type": ["string", "null"], "const": "abc" }, null)

will return false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the same reason, if you pass null to the serializer with schema { "type": "number" }, you will get 0, instead of null.

t.ok(validate(JSON.parse(output)), 'valid schema')

const output2 = stringify({ foo: 'baz' })
Expand All @@ -262,7 +262,7 @@ test('schema with const as nullable', (t) => {
foo: null
})

t.equal(output, '{"foo":null}')
t.equal(output, '{"foo":"baz"}')
t.ok(validate(JSON.parse(output)), 'valid schema')

const output2 = stringify({
Expand Down