vine
vine copied to clipboard
Discussion for a new feature - Support Template Literals
Hello,
I’d like to request template-literal support in Vine for narrow string validations, maybe something like this:
import { vine } from "vine";
const literalValidator = vine
.string()
.literal(`${vine.number()}.foo.${vine.string().toLowerCase()}`);
const result = await request.validateUsing(literalValidator);
// typeof result === `${number}.foo.${Lowercase<string>}`
Zod does not quite solve this either, but has a handy:
const literal = z.custom<`${number}.${number}.${number}`>((val) =>
/^\d+\.\d+\.\d+$/g.test(val as string)
);
This issue on Zod describes how they do it currently: https://github.com/colinhacks/zod/issues/419
Would also be cool if things like email validation returned a stricter type, like this:
vine.object({
email: vine
.string()
.email(optionsGoesHere)
})
typeof email === `${string}@${string}`