vine icon indicating copy to clipboard operation
vine copied to clipboard

Discussion for a new feature - Support Template Literals

Open AdrianAndersen opened this issue 8 months ago • 1 comments

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

AdrianAndersen avatar Jul 09 '25 16:07 AdrianAndersen

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}`

AdrianAndersen avatar Jul 09 '25 16:07 AdrianAndersen