Closed throrin19 closed 3 months ago
I need the code for token
and minFields
to investigate this further.
No problem :
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');
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 :)
Hello, I try to use record with specific StringSchema in key param (I want a valid token using our customValidator) :
When I do this, I have an error TS2769. This is the result (in frech, i'm sorry) :
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 LTSThanks in advance.