fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖

Home Page:https://valibot.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Record with params (key, value, pipe) return error TS2769

throrin19 opened this issue · comments

Hello, I try to use record with specific StringSchema in key param (I want a valid token using our customValidator) :

record(string([token(true)]), literal('value'), [minFields(1)])

When I do this, I have an error TS2769. This is the result (in frech, i'm sorry) :

Aucune surcharge ne correspond Ă  cet appel.
  La surcharge 1 sur 4, '(value: StringSchema<string>, message?: ErrorMessage | undefined, pipe?: Pipe<{ [x: string]: string; }> | undefined): RecordSchema<StringSchema<string>, StringSchema<...>, { ...; }>', a généré l'erreur suivante.
    L'argument de type 'LiteralSchema<"value", "value">' n'est pas attribuable au paramètre de type 'ErrorMessage | undefined'.
  La surcharge 2 sur 4, '(key: StringSchema<string>, value: LiteralSchema<"value", "value">, pipe?: Pipe<{ [x: string]: "value"; }> | undefined): RecordSchema<StringSchema<string>, LiteralSchema<...>, { ...; }>', a généré l'erreur suivante.
    Impossible d'assigner le type 'CustomValidation<{ [x: string]: string; }>' au type 'BaseValidation<{ [x: string]: "value"; }> | BaseTransformation<{ [x: string]: "value"; }>'.
      Impossible d'assigner le type 'CustomValidation<{ [x: string]: string; }>' au type 'BaseValidation<{ [x: string]: "value"; }>'.
        Les types retournés par '_parse(...)' sont incompatibles entre eux.
          Impossible d'assigner le type 'PipeActionResult<{ [x: string]: string; }>' au type 'PipeActionResult<{ [x: string]: "value"; }>'.
            Impossible d'assigner le type 'ValidActionResult<{ [x: string]: string; }>' au type 'PipeActionResult<{ [x: string]: "value"; }>'.
              Impossible d'assigner le type 'ValidActionResult<{ [x: string]: string; }>' au type 'ValidActionResult<{ [x: string]: "value"; }>'.
                Impossible d'assigner le type '{ [x: string]: string; }' au type '{ [x: string]: "value"; }'.
                  Les signatures d'index « string » sont incompatibles.
                    Impossible d'assigner le type 'string' au type '"value"'.
  La surcharge 3 sur 4, '(key: StringSchema<string>, value: LiteralSchema<"value", "value">, message?: ErrorMessage | undefined, pipe?: Pipe<{ [x: string]: "value"; }> | undefined): RecordSchema<...>', a généré l'erreur suivante.
    L'argument de type 'CustomValidation<{ [x: string]: string; }>[]' n'est pas attribuable au paramètre de type 'ErrorMessage | undefined'.

I verify all my types and the type signature of record and RecordKey and normally it would works fine.

Have you a solution for that ?

I use valibot version 0.30.0 on node 20 LTS

Thanks in advance.

I need the code for token and minFields to investigate this further.

No problem :

  • token :
import { custom } from 'valibot';

/**
 * Validate if passed string is a validate token.
 * Requires the string value to only contain a-z, A-Z, 0-9, and underscore _.
 *
 * @param mesg - Custom Error message
 */
export const token = (mesg?: string) =>
    custom<string>((input) => {
        return /^\w+$/.test(input);
    }, mesg || 'Value must only contain alpha-numeric and underscore characters');
  • minFields :
import { custom } from 'valibot';

/**
 * Validator for object and record schema to check if
 * object/recore have at least the passed number of keys
 *
 * @param size - desired min number of fields
 */
export const minFields = <T extends object>(size: number) =>
    custom<T>((input) => Object.keys(input).length >= size, `The object must have at least ${size} key(s)`);

The way you wrote the minFields code, TypeScript is not able to automatically infer the type correctly. Here is a workaround:

const Schema = record(string([token()]), literal("value"), [
  minFields<Record<string, "value">>(1),
]);

It works fine. Thanks :)