Suite of AT Protocol TypeScript libraries built on web standards
1import {
2 Schema,
3 type ValidationResult,
4 type ValidatorContext,
5} from "../validation.ts";
6
7export class RegexpSchema<T extends string = string> extends Schema<T> {
8 constructor(readonly pattern: RegExp) {
9 super();
10 }
11
12 validateInContext(
13 input: unknown,
14 ctx: ValidatorContext,
15 ): ValidationResult<T> {
16 if (typeof input !== "string") {
17 return ctx.issueInvalidType(input, "string");
18 }
19 if (!this.pattern.test(input)) {
20 return ctx.issueInvalidFormat(input, this.pattern.toString());
21 }
22 return ctx.success(input as T);
23 }
24}