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

[Datx v4] - serializeSchema should use null or omit this value for optional values. #1135

Open
isBatak opened this issue Dec 19, 2022 · 0 comments
Assignees

Comments

@isBatak
Copy link
Contributor

isBatak commented Dec 19, 2022

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:

image

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

  1. Should it remove undefined properties or set them as null?
  2. 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}`,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants