Skip to content
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

feat: drop nullable const support #520

Closed
wants to merge 1 commit into from

Conversation

ivan-tymoshenko
Copy link
Member

❗BREAKING CHANGES

I didn't check it here #511 but turns out if the schema has const property, it can't be nullable.

If I have a schema like that:

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

or

{ "type": "string", "nullable": true, "const": "abc" }

it will validate the null value as false. That means if we serialize the null value as null, the serialized object would fail the validation.

P.S. I was confused because I saw the validation check, but it turns out that is-my-json-valid doesn't handle const check at all.

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

@@ -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

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.

@mcollina
Copy link
Member

mcollina commented Sep 5, 2022

Unfortunately I think we should land this in a next branch and wait for the Fastify v5 release train (2023).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants