-
-
Notifications
You must be signed in to change notification settings - Fork 208
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
BigInt serialization error on Integer and Null union type #501
Comments
I think the problem based on |
Isn't there a BigInt serializer to Number on fastify-json-stringify though? |
When using |
Ahhh I see how it is, so the culprit here are |
I don't think we can ever fix this without some Ajv support. Maybe we should document this caveat? |
Actually yes, we can! |
Actually, I was about to write a post/issue about the whole validation situation. In short, we use the same syntax of schema to describe how to serialize data (with type coercion, it's important) and to validate the same data. Here we have some inconsistency problems: date type (already fixed), bigint, regexp, etc. |
Can you make a list of needed data type validators? |
I have carefully studied this topic. Ajv does not support bigint type or bigint cast. It also cannot be resolved in the way we did for the Date type, because the Date type is the union of existing in Ajv types: string and date, while the bigint type is not a number or an integer. This can be solved by specifying your own Ajv keyword. All occurrences of integer in user schemas will need to be replaced with this keyword. Support for integer formats will not automatically work with this keyword. I don't know of a simple solution to this problem right now. I leave here a test for anyone who wants to try to solve this problem. test('anyOf with bigint type', (t) => {
t.plan(1)
const schema = {
type: 'object',
properties: {
id: {
anyOf: [
{
type: 'integer'
},
{
type: 'null'
}
]
}
}
}
const stringify = build(schema)
const data = { id: 12n }
const output = stringify(data)
t.equal(output, '{"id":12}')
}) Ajv issue about that: ajv-validator/ajv#1116 |
Hi. Anyway. The first thing I got to modify is multiple types (such as // Works, because `pattern` is ignored, AJV is unused.
build({
type: 'string',
pattern: '[A-Z]+',
}).stringify('123') === '"123"'
// Throws an error, because `anyOf` does validation call to AJV.
build({
anyOf: [{
type: 'string',
pattern: '[A-Z]+'
}]
}).stringify('123') === '"123"' If AJV is used only "sometimes", I'd consider a solution where it's unused in the serialization flow graph at all, and FJS only supports a subset of validation-only (
Now I'd like to explain for people struggling with this problem why it even exists.
Excuse me for my crooked english, I hope this text sounds sensibly. |
Prerequisites
Fastify version
4.3.0
Plugin version
No response
Node.js version
16.16.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
elementary OS 6.1 Jólnir (Ubuntu 20.04.3 LTS)
Description
Using typebox as the schema generator, whenever I made a type on the schema where it is a union of Integer and Null, Fastify always return as such.
With the stacktrace of,
Steps to Reproduce
Create a endpoint (using Typebox v0.24.20) as such
Call the endpoint
Expected Behavior
The serializer should serialize as an integer if it's an BigInt, and null if it's not
The text was updated successfully, but these errors were encountered: