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

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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