Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

725
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda