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

572
Visualizações
Joi validation for a decimal number not working properly

I am validating the request body for one of the API I am working on to insert the data in the DB. DB has precision as 3.

Below is the schema -

const insertDetails = {
  body: Joi.object({
    id: Joi.string().length(6).required(),
    startdate: Joi.date().format('YYYY-MM-DD').required(),
    enddate: Joi.date().format('YYYY-MM-DD').required(),
    role: Joi.number().valid(5).required(),
    fnum: Joi.number().less(1.500).precision(3).required(),
    user: Joi.string().max(8).required()
  })
}

My understanding is for fnum that it'll only allow decimal precision upto 3 and should throw an error if there's a number like 1.345678 saying the precision should be 3.

But it goes through and while inserting the data , it rounds to 3 decimal places in the database.

I tried searching online and see that we can validate or set the convert to false but I am having difficulty doing it for just one of the field here i.e. fnum.

Joi.validate(insertDetails, {"convert": false})

I added above line but it throws an error saying

TypeError: Joi.validate is not a function

Any suggestions as to how can I validate the precision of the fnum in the request and should throw an error if the precision is more?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Define schema like this-

const insertSchema = Joi.object({
    id: Joi.string().length(6).required(),
    startdate: Joi.date().format('YYYY-MM-DD').required(),
    enddate: Joi.date().format('YYYY-MM-DD').required(),
    role: Joi.number().valid(5).required(),
    fnum: Joi.number().less(1.500).precision(3).required(),
    user: Joi.string().max(8).required()
  })

Then Use the validate function with the schema-

const {error, value} = insertSchema.validate(req.body,{"convert": false});

You can place the req.body object as the first params and the next params will be options.

And this function will return an object with error and value.


Or you can create a middleware like this for routes-

const validateRequest = (
  req,
  next,
  schema,
  options = {
    abortEarly: false, // include all errors
    allowUnknown: true, // ignore unknown props
    stripUnknown: true, // remove unknown props
  },
) => {
  const { error, value } = schema.validate(req.body, options);
  if (error) {
    const error_ = new Error(`Validation error: ${error.details.map((x) => x.message).join(', ')}`);
    error_.statusCode = 400;
    throw error_;
  } else {
    req.body = value;
    next();
  }
};

Then create your schema with validateRequest function-


const insertSchema = (req, res, next) => {
  const schema = Joi.object({
      id: Joi.string().length(6).required(),
      startdate: Joi.date().format('YYYY-MM-DD').required(),
      enddate: Joi.date().format('YYYY-MM-DD').required(),
      role: Joi.number().valid(5).required(),
      fnum: Joi.number().less(1.5).precision(3).required(),
      user: Joi.string().max(8).required(),
    });
  validateRequest(req, next, schema);
};

Then use it in your router as a middleware-

router.post('/create-post', insertSchema, (req, res, next) => {
   // Do rest of the code
});
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