-
Notifications
You must be signed in to change notification settings - Fork 8
[Datx v4] - serializeSchema should use null or omit this value for optional values. #1135
Copy link
Copy link
Open
Labels
Description
Description
Next.js v13
export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
const commentResponse = await fetcher('http://localhost:3000/api/datx/comment/1');
const collection = new Collection();
const comment = parseSchema(Comment, commentResponse, collection);
return {
props: {
fallback: {
[commentKey]: serializeSchema(Comment, comment),
},
},
};
};Error:
Test example:
it('should use `null` or omit this value. Reason: `undefined` cannot be serialized as JSON.', () => {
const Foo = new Schema(
'foo',
{
name: String,
featured: { type: Boolean, optional: true },
},
(data: IResource<Schema>) => `foo/${data.name}`,
);
const foo: IResource<typeof Foo> = {
name: 'foo',
};
const rawFoo = serializeSchema(Foo, foo);
expect(rawFoo).toEqual({ name: 'foo' });
// or
expect(rawFoo).toEqual({ name: 'foo', featured: null });
});Things to discuss
- Should it remove undefined properties or set them as
null? - Should it allow opt-in to one of these behaviors through some configuration
Workaround
The current workaround is to set defaultValue to null explicitly.
Is it a good enough solution?
const Foo = new Schema(
'foo',
{
name: String,
featured: { type: Boolean, optional: true, defaultValue: null },
},
(data: IResource<Schema>) => `foo/${data.name}`,
);Reactions are currently unavailable
