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

Add new issue reason for non-matching variant key #277

Closed
wants to merge 2 commits into from

Conversation

lo1tuma
Copy link
Contributor

@lo1tuma lo1tuma commented Nov 29, 2023

Fixes #235.

This is a breaking change because with this change only literal schemas are allowed for the discriminator.

Copy link

netlify bot commented Nov 29, 2023

Deploy Preview for valibot canceled.

Name Link
🔨 Latest commit 7069548
🔍 Latest deploy log https://app.netlify.com/sites/valibot/deploys/656893a87d7eaa00090bb9ea


/**
* Variant option type.
*/
export type VariantOption<TKey extends string> =
| ObjectSchema<Record<TKey, BaseSchema>, any>
| ObjectSchema<Record<TKey, LiteralSchema<Literal>>, any>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this line correctly, only literals can be passed as values in the object variants now. This would make variant useless in my eyes. Did I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as discussed here. Why do you think it would make variant useless? I have a hard time finding real-world use-cases with anything else than literals. Zod also doesn’t support much more, see here. Although Zod supports some special cases like literals that are wrapped in ZodLazy or ZodEffects as well as null, undefined and enums.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it correctly, each key of the objects must now have a literal as its value. However, usually only the discriminator is a literal and the rest is some other data type. For example, is the following code still possible after your changes?

const Schema = variant('type', [
  object({ type: literal('a'), a: string() }),
  object({ type: literal('b'), b: object({ key: string() }) }),
]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I get your point. I need to take a closer look at VariantOption and ObjectSchema. Because from just reading the code I come to the same conclusion as you. But for some reason the example you provided works fine. See also the unit tests I’ve added in this PR, the val property uses non-literal schemas and there is no typescript compilation error... 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I had thought of that and wondered about it. What is the reason why we can only accept literals as discriminators? Presumably you have to access the .literal for the error info, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason why we can only accept literals as discriminators? Presumably you have to access the .literal for the error info, right?

Exactly. An alternative solution would be to just pass the schema objects of every option to the issue requirements instead of limiting the schemas to literal schemas. In this case the consumer needs to decide what to do with the requirements and could extract the .literal value in case the schema object is a literal schema.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will think about it. However, I probably won't be able to work on this PR for another three to four weeks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve just pushed another commit which explores the idea of passing the variant options as issue.requirement.

However, I probably won't be able to work on this PR for another three to four weeks.

No worries. There is zero pressure from side. If I can help you out with anything just let me know 😉.

@fabian-hiller fabian-hiller self-assigned this Nov 29, 2023
@fabian-hiller fabian-hiller added the enhancement New feature or request label Nov 29, 2023
fabian-hiller added a commit that referenced this pull request Dec 27, 2023
@fabian-hiller
Copy link
Owner

This is partially fixed in v0.25.0. Sorry that I did not build on this PR to implement it.

@fabian-hiller
Copy link
Owner

I think this is now completely fixed with the changes in v0.28.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can’t distinguish invalid key from invalid input in new variant schema
2 participants