Skip to content

Commit 3a7f2aa

Browse files
chore: improve validator types
1 parent 2ab603a commit 3a7f2aa

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,19 @@ export interface NormalizeEmailOptions {
247247

248248
export interface Validator<T> {
249249
name: ValidationNames
250+
getRules: () => ValidationRule<T>[]
250251
test: (value: T) => boolean
251252
validate: (value: T) => ValidationResult
252253
required: () => this
253254
optional: () => this
254255
}
255256

257+
// Internal interface for implementation details
258+
export interface ValidatorInternal<T> extends Validator<T> {
259+
isPartOfShape: boolean
260+
rules: ValidationRule<T>[]
261+
}
262+
256263
export interface ValidationConfig {
257264
verbose: boolean
258265
strictMode?: boolean
@@ -299,6 +306,7 @@ export interface BooleanValidatorType extends Validator<boolean> {
299306
}
300307

301308
export interface EnumValidatorType<T extends string | number> extends Validator<T> {
309+
allowedValues: readonly T[]
302310
custom: (fn: (value: T) => boolean, message: string) => EnumValidator<T>
303311
}
304312

@@ -351,7 +359,7 @@ export interface ValidationInstance {
351359
password: () => PasswordValidatorType
352360
}
353361

354-
export type ValidationType = StringValidatorType | NumberValidatorType | ArrayValidatorType<any> | BooleanValidatorType | EnumValidatorType<any> | DateValidatorType | DatetimeValidatorType | ObjectValidatorType<any> | CustomValidatorType<any> | TimestampValidatorType | UnixValidatorType | PasswordValidatorType
362+
export type ValidationType = StringValidatorType | NumberValidatorType | ArrayValidatorType<string | number | boolean | Date> | BooleanValidatorType | EnumValidatorType<string | number> | DateValidatorType | DatetimeValidatorType | ObjectValidatorType<Record<string, any>> | CustomValidatorType<Record<string, any>> | TimestampValidatorType | UnixValidatorType | PasswordValidatorType
355363

356364
export type Infer<T> = T extends Validator<infer U> ? U : never
357365

src/validators/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export abstract class BaseValidator<T> {
6565
}
6666
}
6767

68+
getRules(): ValidationRule<T>[] {
69+
return this.rules
70+
}
71+
6872
test(value: T): boolean {
6973
return this.validate(value).valid
7074
}

0 commit comments

Comments
 (0)