valibot: TS error when using custom()

The property schema: ‘object’ gives me a TS error and I can’t put it to work.

const FormSchema = v.object(
  {
    dataFimValidadeFim: v.nullable(
      v.optional(v.coerce(v.date('validation.date'), (input) => new Date(input as number | string | Date))),
    ),
    dataFimValidadeInicio: v.nullable(
      v.optional(v.coerce(v.date('validation.date'), (input) => new Date(input as number | string | Date))),
    ),
    dataInicioValidadeFim: v.nullable(
      v.optional(v.coerce(v.date('validation.date'), (input) => new Date(input as number | string | Date))),
    ),
    dataInicioValidadeInicio: v.nullable(
      v.optional(v.coerce(v.date('validation.date'), (input) => new Date(input as number | string | Date))),
    ),
    dataPedidoFim: v.nullable(
      v.optional(v.coerce(v.date('validation.date'), (input) => new Date(input as number | string | Date))),
    ),
    dataPedidoInicio: v.nullable(
      v.optional(v.coerce(v.date('validation.date'), (input) => new Date(input as number | string | Date))),
    ),
    idEstado: v.optional(v.coerce(v.string(), String)),
    idInterno: v.optional(v.coerce(v.string(), String)),
    identificador: v.optional(v.string()),
    nome: v.optional(v.string()),
  },
  [
    v.custom((inputs) => {
      if (inputs.dataInicioValidadeInicio && inputs.dataInicioValidadeFim) {
        if (dateRangeValidator(inputs.dataInicioValidadeInicio, inputs.dataInicioValidadeFim)) {
          throw new ValiError([
            {
              input: inputs.dataInicioValidadeInicio,
              message: 'validation.minDateExceeded',
              origin: 'value',
              reason: 'date',
              validation: 'custom',
              path: [
                {
                  input: inputs,
                  key: 'dataInicioValidadeInicio',
                  schema: 'object', <--- Object literal may only specify known properties, and 'schema' does not exist in type 'PathItem'.
                  value: inputs.dataInicioValidadeInicio,
                },
              ],
            },
          ]);
        }
      }

      return true;
    }),
  ],
);

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 28 (14 by maintainers)

Most upvoted comments

I agree, let’s do the following, I’ll close this issue and open a new one with a recap of this one.

Is this issue resolved? I will close it for now, but will reopen it if not.

I’ll check it ASAP and let you know.

Feel free to create a new issue (or PR) to add an items action in the next weeks.

I’ll open one issue latter today.

Feel free to create a new issue (or PR) to add an items action in the next weeks.

Thanks, I’ll play with it again.

I am working on it and will probably add it later today.

You are right. I will probably add it today or tomorrow. I will ping you then.

Oh, that’s what I need, I thought that the custom was deprecated. Thanks. I’ll play a bit with the current state of the lib.

Please update this issue when this action gets done.

Although it’s not recommended, I’ll let v.custom with throw as it is working as I need, I’ll come here again when the rewrite is done to update it. And perhaps close the issue then with a solution.

In summary, I would like to have:

  • An option to validate every element of the array and add the issue to the index of the occurrence (in this example, otherNationalities.2.
  • An option to add issues globally to the schema based on field relations:
const Schema = v.object(
  {
     mainNationality: v.string([v.minLength(1, 'required.nationality')]),
     otherNationalities: v.array(v.string([v.minLength(1, 'required.otherNationalities')]), [
        v.minLength(1, 'required.otherNationalities'),
        (custom valiration to check duplicated)
     ])
    (...other fields)
  },
  [
    v.<someMethod>((inputs) => {
      // If some element in otherNationalities is equal to the mainNationality, 
      // add an issue to the otherNationalities item index
    }),
  ],
);

No, but that’s an interesting idea!

I think so, in my current implementation, this is important to select the duplicated option, and with the code that I’ve shared here, it works. image

With my current code pipeline actions accept a dataset as the first argument and return a dataset as a result. This will allow you to easily write your own actions and add as many issues as you want. I plan to publish a first draft next week.

Those are great news, I’m looking forward to testing it.