Skip to content

Discussion for a new feature - Support Template Literals #120

@AdrianAndersen

Description

@AdrianAndersen

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:
colinhacks/zod#419

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions