-
-
Notifications
You must be signed in to change notification settings - Fork 199
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
Conversation
@@ -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"}') |
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
Unfortunately I think we should land this in a |
❗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:
or
it will validate the
null
value as false. That means if we serialize thenull
value asnull
, 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 handleconst
check at all.fast-json-stringify/test/const.test.js
Line 242 in ab900c8