valibot icon indicating copy to clipboard operation
valibot copied to clipboard

Returned Type of startsWith is still string

Open CordlessWool opened this issue 1 year ago • 1 comments

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.

CordlessWool avatar Sep 10 '24 17:09 CordlessWool

Can you share a playground link with an example? Maybe custom is what you are looking for.

fabian-hiller avatar Sep 10 '24 18:09 fabian-hiller

Ran into a similar issue - v.custom does indeed solve it.

Using @CordlessWool's example,

Image

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);

nktnet1 avatar Aug 29 '25 02:08 nktnet1