Phil
Phil

Reputation: 7597

yup schema validation - tuples and alternative objects

In yup's doc I could not find anything for validating tuple arrays and alternative objects. How would an object like this be validated in yup?

interface Example {
    tuple: string[]; // always two elements
    alt: { foo: string; } | { bar: string; }
}

Upvotes: 2

Views: 1899

Answers (1)

Phil
Phil

Reputation: 7597

For alternative objects/types you can use yup.lazy():

yup.lazy((alt: { foo: string; } | { bar: string; }) => {
    if (alt.foo) return yup.object<{ foo: string; }>();
    if (alt.bar) return yup.object<{ bar: string; }>();
})

For tuples, this answer is the best help I could find, but it does not wok in typescript: https://github.com/jquense/yup/issues/528#issuecomment-496353532

Upvotes: 1

Related Questions