Skip to content

Joi.array() rewrites custom error from inner item #3083

@fudoggg

Description

@fudoggg

Runtime

node.js

Runtime version

22.14

Module version

17.10

Used with

No response

Any other relevant information

I want to check value in array with custom message and next take this error, but array function seems rewrites inner errors and i just take 2 errors like array.includes and array.includesRequiredUnknowns

I tried it with helpers.message as in example and helpers.error with .messages() description

I also tried remove .required and .length() and error also was of part array

Maybe i make some errors or array object always makes this but how to achieve the expected behavior?

How can we help?

export default function (fileDate) {
  return Joi.array()
    .items(
      Joi.number().integer().min(1).required(),
      Joi.string()
        .pattern(/^\d+-\d+$/)
        .required(),
      Joi.string()
        .pattern(/^\d{2}\.\d{2}\.\d{4}$/)
        .required()
        .custom((value, helpers) => {
          const [day, month, year] = value.split('.');
          const date = new Date(`${year}-${month}-${day}`);
          if (moment(date).isAfter(moment(fileDate, 'DD.MM.YYYY').subtract(13, 'days'))) {
            return helpers.message({
              custom: `Validated orders need 2 weeks to be valid, file actual date is ${fileDate}, date order is ${value}`
            });
          }

          return date.toString() === 'Invalid Date'
            ? helpers.message({ custom: `Incorrect date format: "${value}"` })
            : value;
        })
    )
    .length(3);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions