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

191
Visualizações
How to validate sum of refs in Joi

How can I validate the total of two refs as below? I would prefer have custom be on the total, yet unsure of how to get the value of the refs in the custom when using Joi.ref.

I can do the following and hang a custom off the entire validation schema, but would prefer to attach it to total.

I don't want to use expression()

I prefer not to change the schema structure as per this answer.

  const widgetValidator = Joi.object({
    a: Joi.number().integer().min(0).required(),
    b: Joi.number().integer().min(1).required(),
    total: Joi.number().integer().min(1).required(),
  }).custom((value: {a: number; b: number; total: number;}, helpers) => {
      if (value.total !== value.a + value.b) {
        throw new Error('invalid balance calculation');
      }

      return value;
    },
  );
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you install custom on an attribute, it will be a little hard to get whole object values. You have to get it from helpers's states:

Joi.object({
    a: Joi.number().integer().min(0).required(),
    b: Joi.number().integer().min(1).required(),
    total: Joi.number().integer().min(1).required().custom((value, helpers) => {
      const { a, b } = helpers.state.ancestors[0];
      if (value !== a + b) {
        throw new Error('invalid balance calculation');
      }

      return value;
    },
  )
})
about 4 years ago · Juan Pablo Isaza 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