question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sum type / oneOfType

See original GitHub issue

Hi there. Sorry if this is already included, but I can’t find any documentation or issues regarding it.

I’m looking for a way to validate that a given type is one of multiple possible types. In FP, it would look like number | string, for example. React PropTypes offers this as oneOfType. I think yup would probably have either oneOfType or .or as in .string().or(yup.number()).

Is anything like this supported?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
colinhackscommented, Mar 8, 2020

@rivertam @Aaronius @sublimeye I hit against this issue as well (among others) so I made a validation library that supports it: https://github.com/vriad/zod

Specifically, there is a built-in “union” type, like so:

const numOrString = z.union([ z.string(), z.number() ])
8reactions
jquensecommented, Oct 8, 2018

something like?

const schema = yup.lazy(value => typeof value === 'number' yup.number() : yup.string())

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
1 More types - Cornell CS
Intuitively, a product holds two values, one of type τ1, and one of type τ2. By contrast, a sum holds a single value...
Read more >
3.9. Algebraic Data Types — OCaml Programming
The sum types come from the fact that a value of a variant is formed by one of the constructors. The product types...
Read more >
Adding Either type to the Standard Library - #80 by filip-sakel
Until this thread, I never saw a sum-tuple idea with just a label for ... Nonetheless Varidadic Generics are required to make an...
Read more >
Typed Compilation of Recursive Datatypes
The generativity of datatypes poses some challenges for type- directed compilation of SML. ... into one of type δ, and the latter is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found