Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

730
Vistas
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?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda