Как я могу создать ошибку для поля массива с помощью Yup?

Я использую Yup, и я хочу создать ошибку для поля массива, а не для его дочерних элементов, и я не знаю, как заставить его не создавать объект.

Моя схема:

const schema = Yup.object().shape({
  detail_cuotas: Yup.array()
    .of(
      Yup.object({
        condicion_pago: Yup.number().integer(),
        nro_cuota: Yup.number().integer(),
        dias: Yup.number().required().integer().positive().min(0),
        porcentaje: Yup.number().required().positive().min(0).max(100),
      })
    )
    .required()
    .totalPercentage(),
});

Мой метод:

Yup.addMethod(Yup.array, "totalPercentage", function () {
return this.test(function (value) {
  const { path, createError } = this;
  if (value) {
    let sum = 0;
    value.map((cuota) => {
      if (cuota && cuota["porcentaje"] != undefined) {
        sum += Number(cuota.porcentaje);
      }
    });

    return sum == 100
      ? true
      : createError({
          path,
          message: "La sumatoria de porcentajes debe ser igual a 100. ",
        });
  }
  return true;
});

Результат:

{"detail_cuotas": [{"0":"L","1":"a","2":" ","3":"s","4":"u","5":"m","6":"a","7":"t","8":"o","9":"r","10":"i","11":"a","12":" ","13":"d","14":"e","15":" ","16":"p","17":"o","18":"r","19":"c","20":"e","21":"n","22":"t","23":"a","24":"j","25":"e","26":"s","27":" ","28":"d","29":"e","30":"b","31":"e","32":" ","33":"s","34":"e","35":"r","36":" ","37":"i","38":"g","39":"u","40":"a","41":"l","42":" ","43":"a","44":" ","45":"1","46":"0","47":"0","48":".","49":" "}]}

Я ожидаю:

{"detail_cuotas": ["La sumatoria de porcentajes debe ser igual a 100. "]}

0 ответов

Другие вопросы по тегам