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 type EnumSchemaOptions<V extends null | string | number | boolean> = {
8 description?: string;
9 default?: V;
10};
11
12export class EnumSchema<
13 const V extends null | string | number | boolean,
14> extends Schema<V> {
15 constructor(
16 readonly values: readonly V[],
17 readonly options: EnumSchemaOptions<V> = {},
18 ) {
19 super();
20 }
21
22 validateInContext(
23 input: unknown = this.options.default,
24 ctx: ValidatorContext,
25 ): ValidationResult<V> {
26 if (!(this.values as readonly unknown[]).includes(input)) {
27 return ctx.issueInvalidValue(input, this.values);
28 }
29 return ctx.success(input as V);
30 }
31}