jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

Incorrect Typings: array.ensure() should not produce undefined #2180

Open denis-pakhorukov opened 8 months ago

denis-pakhorukov commented 8 months ago

Describe the bug According to the documentation, array.ensure() sets the default to [] and transforms null and undefined values to an empty array. However, the typings imply that undefined is still a possible value.

To Reproduce

import * as Yup from "yup";

const userSchema = Yup.object().shape({
  array1: Yup.array().of(Yup.string().required()),
  array2: Yup.array().of(Yup.string().required()).default([]),
  array3: Yup.array().of(Yup.string().required()).ensure(),
});

type User = Yup.InferType<typeof userSchema>;

Actual behavior

type User = {
    array1?: string[] | undefined;
    array2: string[];
    array3?: string[] | undefined;
}

Expected behavior

type User = {
    array1?: string[] | undefined;
    array2: string[];
    array3: string[];
}