Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 41 lines 979 B view raw
1import { 2 Schema, 3 type ValidationResult, 4 type ValidatorContext, 5} from "../validation.ts"; 6 7export type BooleanSchemaOptions = { 8 default?: boolean; 9 const?: boolean; 10}; 11 12export class BooleanSchema< 13 const Options extends BooleanSchemaOptions = any, 14> extends Schema<boolean> { 15 constructor(readonly options: Options) { 16 super(); 17 } 18 19 validateInContext( 20 input: unknown = this.options.default, 21 ctx: ValidatorContext, 22 ): ValidationResult<boolean> { 23 const bool = coerceToBoolean(input); 24 if (bool == null) { 25 return ctx.issueInvalidType(input, "boolean"); 26 } 27 28 if (this.options.const !== undefined && bool !== this.options.const) { 29 return ctx.issueInvalidValue(bool, [this.options.const]); 30 } 31 32 return ctx.success(bool); 33 } 34} 35 36function coerceToBoolean(input: unknown): boolean | null { 37 if (typeof input === "boolean") return input; 38 if (input === "true") return true; 39 if (input === "false") return false; 40 return null; 41}