Conversation
| }) | ||
|
|
||
| t.equal(output, '{"foo":null}') | ||
| t.equal(output, '{"foo":"baz"}') |
There was a problem hiding this comment.
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.
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.
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 trueAnd if we serialize null as null,
validate({ "type": ["string", "null"], "const": "abc" }, null)will return false.
There was a problem hiding this comment.
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:
{ "type": ["string", "null"], "const": "abc" }or
{ "type": "string", "nullable": true, "const": "abc" }it will validate the
nullvalue as false. That means if we serialize thenullvalue 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-validdoesn't handleconstcheck at all.fast-json-stringify/test/const.test.js
Line 242 in ab900c8