@stacksjs/ts-validation
Version:
A simple TypeScript starter kit using Bun.
73 lines (72 loc) • 1.81 kB
TypeScript
// Define unique symbols for schema properties
export declare const SCHEMA_NAME: unique symbol;
export declare const INPUT_TYPE: unique symbol;
export declare const OUTPUT_TYPE: unique symbol;
export declare const COMPUTED_TYPE: unique symbol;
export declare const PARSE: unique symbol;
export declare interface ValidationError {
message: string
}
export declare interface ValidationErrorMap {
}
export declare interface ValidationResult {
valid: boolean
errors: ValidationErrors
}
export declare interface ValidationRule<T> {
name: string
test: (value: T) => boolean
message: string
params?: Record<string, any>
}
export declare interface Validator<T> {
name: ValidationNames
isRequired: boolean
getRules: () => ValidationRule<T>[]
test: (value: T) => boolean
validate: (value: T) => ValidationResult
required: () => this
optional: () => this
}
// Internal interface for implementation details
export declare interface ValidatorInternal<T> extends Validator<T> {
isPartOfShape: boolean
rules: ValidationRule<T>[]
}
export declare interface ValidationConfig {
verbose: boolean
strictMode?: boolean
cacheResults?: boolean
errorMessages?: Record<string, string>
}
export declare interface LengthValidator<T> {
min: (length: number) => T
max: (length: number) => T
length: (length: number) => T
}
export type ValidationErrors = ValidationError[] | ValidationErrorMap
export type ValidationNames = 'base' |
'string' |
'number' |
'array' |
'boolean' |
'enum' |
'date' |
'datetime' |
'object' |
'custom' |
'timestamp' |
'unix' |
'password' |
'text' |
'bigint' |
'timestampTz' |
'float' |
'decimal' |
'time' |
'smallint' |
'integer' |
'json' |
'blob' |
'binary'
export type Infer<T> = T extends Validator<infer U> ? U : never