UNPKG

@stacksjs/ts-validation

Version:

A simple TypeScript starter kit using Bun.

27 lines (23 loc) 859 B
import type { EnumValidatorType, ValidationNames } from '../types'; export declare class EnumValidator<T extends string | number> extends BaseValidator<T> implements EnumValidatorType<T> { public name: ValidationNames = 'enum' private allowedValues: readonly T[] constructor(allowedValues: readonly T[]) { super() this.allowedValues = allowedValues this.addRule({ name: 'enum', test: (value: unknown): value is T => this.allowedValues.includes(value as T), message: 'Must be one of: {values}', params: { values: this.allowedValues.join(', ') }, }) } custom(fn: (value: T) => boolean, message: string): this { return this.addRule({ name: 'custom', test: fn, message, }) } } export declare function enum_<T extends string | number>(allowedValues: readonly T[]): EnumValidator<T>;