valibot
valibot copied to clipboard
Returned Type of startsWith is still string
I am using TypeIds and want to validate if the given string is validateing the typeId. This is at least not a problem write now. But I use the typeId also in types, so my id type is x-${string} for example. The InferOutput is a string for this type.
If I use transform I can set the type correctly, but then I have a transform for just converting the typescript type, seem to be unnecassery? I also did not find a genreic param to define the return type.
Can you share a playground link with an example? Maybe custom is what you are looking for.
Ran into a similar issue - v.custom does indeed solve it.
Using @CordlessWool's example,
source code
import * as v from 'valibot';
const schema = v.custom<`x-${string}`>(
(input) => typeof input === 'string' && input.startsWith('x-'),
);
const value = v.parse(schema, 'x-some-value');
console.log(value);