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

145
Visualizações
Using when with objects in array in Yup

I have the following object:

const values = {
  people: [
    {
      name: "Mark",
      age: null
    },
    {
      name: "Shark",
      age: 31
    }
  ],
  isAgeRequired: false
};

When isAgeRequired flips to true, in my Yup schema I would like to make the age value for each object in the people array require.

I have the following yup schema definition, but right now it seems to validate successfully even if the above requirements are not met:

const validator = object({
  people: array(
    object({
      name: string().required(),
      age: number()
        .nullable()
        .when("isAgeRequired", {
          is: true,
          then: (schema) => schema.required()
        })
    })
  )
});
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is that the parent context is not available in parent , so one way around this was to "raise" the isAgeRequired .when .when sibling like so:

 // Declare the default person schema separately because I'll want to reuse it. const person = { name: string().required(), age: number().nullable() } const validator = object({ people: array( object(personSchema) ).when('isAgeRequired', (isAgeRequired, schema) => { if (Required) { return array({ ...personSchema, age: personSchema.age.required() // Override existing age rule and attach .required to it }) } return schema; }) });
about 4 years ago · Santiago Trujillo Relatório
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