Skip to content

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

@isBatak

Description

@isBatak

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}`,
);

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions