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

Empty object type returned by flattening a Variant Schema #700

Closed
ShlokDesai33 opened this issue Jul 6, 2024 · 5 comments
Closed

Empty object type returned by flattening a Variant Schema #700

ShlokDesai33 opened this issue Jul 6, 2024 · 5 comments
Assignees
Labels
fix A smaller enhancement or bug fix workaround Workaround fixes problem

Comments

@ShlokDesai33
Copy link

When I try to safe parse with a Variant schema, the nested issues have type = {} | undefined. Here's an example:

const VariantSchema = v.variant('type', [
	v.object({
		type: v.literal('email'),
		email: v.pipe(v.string(), v.email()),
	}),
	v.object({
		type: v.literal('url'),
		url: v.pipe(v.string(), v.url()),
	}),
	v.object({
		type: v.literal('date'),
		date: v.pipe(v.string(), v.isoDate()),
	}),
]);

const { success, issues } = v.safeParse(VariantSchema, { hi: 'hello ' });

if (!success) {
	const { nested, root } = v.flatten<typeof VariantSchema>(issues);
}

Is there a fix / workaround to this issue?

@fabian-hiller
Copy link
Owner

fabian-hiller commented Jul 6, 2024

Yes, this is a known issue that will be fixed in the future. Currently there is a circular dependency that I need to investigate. See the code here.

The simplest workaround for now is to simply not pass a generic to flatten. Sorry for the inconvenience.

const { nested, root } = v.flatten(issues);

@fabian-hiller fabian-hiller self-assigned this Jul 6, 2024
@fabian-hiller fabian-hiller added workaround Workaround fixes problem fix A smaller enhancement or bug fix labels Jul 6, 2024
@ShlokDesai33
Copy link
Author

ShlokDesai33 commented Jul 6, 2024

You don't need to apologize, this library is the best validation tool out there! Thank you for the workaround!

I think a better solution to this problem would be to use a union of objects instead. This returns the correct types for both the output and errors. But I'm seeing weird behavior. When I switch the variant schema to a union of objects, the nested errors is undefined and the root error = 'Invalid type: Expected Object but received Object'. Is this expected behavior?

@fabian-hiller
Copy link
Owner

When I switch the variant schema to a union of objects, the nested errors is undefined

In my case they are optional undefined because we have no guarantee that every path has an issue. Please take a look at this playground and let me know if it looks good.

[...] the root error = 'Invalid type: Expected Object but received Object'

I do not have a better solution for this at the moment. This is a rare case, as you would normally use variant for objects.

@ShlokDesai33
Copy link
Author

The example provided in the playground is exactly the behavior I was looking for! Closing this issue for now since I have implemented the work around you provided. Thank you for the help!

@fabian-hiller
Copy link
Owner

This is fixed in v0.39.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A smaller enhancement or bug fix workaround Workaround fixes problem
Projects
None yet
Development

No branches or pull requests

2 participants