@stacksjs/ts-validation
Version:
A simple TypeScript starter kit using Bun.
31 lines (26 loc) • 883 B
TypeScript
import type { EnumValidatorType, ValidationNames } from '../types';
export declare class EnumValidator extends BaseValidator<string> implements EnumValidatorType {
public name: ValidationNames = 'enum'
private allowedValues: readonly string[]
constructor(allowedValues: readonly string[]) {
super()
this.allowedValues = allowedValues
this.addRule({
name: 'enum',
test: (value: string) => this.allowedValues.includes(value),
message: 'Must be one of: {values}',
params: { values: this.allowedValues.join(', ') },
})
}
getAllowedValues(): readonly string[] {
return this.allowedValues
}
custom(fn: (value: string) => boolean, message: string): this {
return this.addRule({
name: 'custom',
test: fn,
message,
})
}
}
export declare function enum_(allowedValues: readonly string[]): EnumValidator;