'yup validation for duplicates in array

is there any option to validate the uniqness of elements in array with yup? just an array of strings like ['1', '2', '3'] i tried the methods from other topics like here Yup - How to validate duplicate value in array of objects?

but i face the problem when its doesnt display all the field errors in one array, but only first finded. i read about validateSync( {aboutEarly: false}) thing, but i can't implement it with formik for some reason.

maybe anyone had the same expirience in production things ?

yup.addMethod(yup.array, 'unique', function (message) {
    return this.test('unique', message, function (array) {
        if (!array) return false;

        const uniqueData = Array.from(
            new Set(array.map((row) => row)),
        );

        const isUnique = array?.length === uniqueData.length;

        if (isUnique) {
            return true;
        }

        const index = array.findIndex(
            (row, i) => row !== uniqueData[i],
        );

        if (array?.[index] === '') {
            return true;
        }

        console.log(`${this.path}.${index}`)

        return this.createError({
            path: `${this.path}.${index}`,
            message,
        });
    });
});

//{[key: string]: Array<email>} kind of object

const validationSchema = yup.object().shape({
    emailsByUniqKeys: yup.lazy(obj => yup.object(
        mapValues(obj, () =>
            yup.array().unique('duplicate')
                .of(yup.string().email().required())
        )
    ))
})

***

                <Formik
                    enableReinitialize
                    initialValues={initialValues}
                    onSubmit={handleSubmit}
                    validationSchema={validationSchema}
                >

***



if there any solution, how should i implement an index array in validateSync function?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source