Skip to content

Commit 6f4ce71

Browse files
chore: refactor types
1 parent f5fe154 commit 6f4ce71

File tree

18 files changed

+468
-2
lines changed

18 files changed

+468
-2
lines changed

src/lib/isAfter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IsAfterOptions } from './types'
1+
import type { IsAfterOptions } from '../types/date'
22
import toDate from './toDate'
33

44
/**

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ export interface StringValidatorType extends Validator<string>, LengthValidator<
285285
custom: (fn: (value: string) => boolean, message: string) => StringValidator
286286
}
287287

288+
export interface TextValidatorType extends StringValidatorType {}
289+
288290
export interface NumberValidatorType extends Validator<number>, LengthValidator<NumberValidator> {
289291
integer: (options?: IsIntOptions) => NumberValidator
290292
float: (options?: IsFloatOptions) => NumberValidator
@@ -295,6 +297,8 @@ export interface NumberValidatorType extends Validator<number>, LengthValidator<
295297
custom: (fn: (value: number) => boolean, message: string) => NumberValidator
296298
}
297299

300+
export interface BigintValidatorType extends NumberValidatorType {}
301+
298302
export interface ArrayValidatorType<T> extends Validator<T[]>, LengthValidator<ArrayValidator<T>> {
299303
each: (validator: Validator<T>) => ArrayValidator<T>
300304
unique: () => ArrayValidator<T>
@@ -347,7 +351,9 @@ export interface PasswordValidatorType extends Validator<string>, LengthValidato
347351

348352
export interface ValidationInstance {
349353
string: () => StringValidatorType
354+
text: () => TextValidatorType
350355
number: () => NumberValidatorType
356+
bigint: () => BigintValidatorType
351357
array: <T>() => ArrayValidatorType<T>
352358
boolean: () => BooleanValidatorType
353359
enum: <T extends string | number>(values: readonly T[]) => EnumValidatorType<T>
@@ -366,4 +372,4 @@ export type ValidationType = {
366372

367373
export type Infer<T> = T extends Validator<infer U> ? U : never
368374

369-
export type ValidationNames = 'base' | 'string' | 'number' | 'array' | 'boolean' | 'enum' | 'date' | 'datetime' | 'object' | 'custom' | 'timestamp' | 'unix' | 'password'
375+
export type ValidationNames = 'base' | 'string' | 'number' | 'array' | 'boolean' | 'enum' | 'date' | 'datetime' | 'object' | 'custom' | 'timestamp' | 'unix' | 'password' | 'text' | 'bigint'

src/types/array.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { ArrayValidator } from '../validators/arrays'
2+
import type { LengthValidator, Validator } from './base'
3+
4+
export interface ArrayValidatorType<T> extends Validator<T[]>, LengthValidator<ArrayValidator<T>> {
5+
each: (validator: Validator<T>) => ArrayValidator<T>
6+
unique: () => ArrayValidator<T>
7+
}

src/types/base.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Define unique symbols for schema properties
2+
export const SCHEMA_NAME: unique symbol = Symbol('schema_name')
3+
export const INPUT_TYPE: unique symbol = Symbol('input_type')
4+
export const OUTPUT_TYPE: unique symbol = Symbol('output_type')
5+
export const COMPUTED_TYPE: unique symbol = Symbol('computed_type')
6+
export const PARSE: unique symbol = Symbol('parse')
7+
8+
export interface ValidationError {
9+
message: string
10+
}
11+
12+
export interface ValidationErrorMap {
13+
[field: string]: ValidationError[]
14+
}
15+
16+
export type ValidationErrors = ValidationError[] | ValidationErrorMap
17+
18+
export interface ValidationResult {
19+
valid: boolean
20+
errors: ValidationErrors
21+
}
22+
23+
export interface ValidationRule<T> {
24+
name: string
25+
test: (value: T) => boolean
26+
message: string
27+
params?: Record<string, any>
28+
}
29+
30+
export interface Validator<T> {
31+
name: ValidationNames
32+
isRequired: boolean
33+
getRules: () => ValidationRule<T>[]
34+
test: (value: T) => boolean
35+
validate: (value: T) => ValidationResult
36+
required: () => this
37+
optional: () => this
38+
}
39+
40+
// Internal interface for implementation details
41+
export interface ValidatorInternal<T> extends Validator<T> {
42+
isPartOfShape: boolean
43+
rules: ValidationRule<T>[]
44+
}
45+
46+
export interface ValidationConfig {
47+
verbose: boolean
48+
strictMode?: boolean
49+
cacheResults?: boolean
50+
errorMessages?: Record<string, string>
51+
}
52+
53+
export interface LengthValidator<T> {
54+
min: (length: number) => T
55+
max: (length: number) => T
56+
length: (length: number) => T
57+
}
58+
59+
export type ValidationNames = 'base' | 'string' | 'number' | 'array' | 'boolean' | 'enum' | 'date' | 'datetime' | 'object' | 'custom' | 'timestamp' | 'unix' | 'password' | 'text' | 'bigint'
60+
61+
export type Infer<T> = T extends Validator<infer U> ? U : never

src/types/boolean.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { BooleanValidator } from '../validators/booleans'
2+
import type { Validator } from './base'
3+
4+
export interface IsBooleanOptions {
5+
loose?: boolean
6+
}
7+
8+
export interface BooleanValidatorType extends Validator<boolean> {
9+
isTrue: () => BooleanValidator
10+
isFalse: () => BooleanValidator
11+
custom: (fn: (value: boolean) => boolean, message: string) => BooleanValidator
12+
}

src/types/custom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Validator } from './base'
2+
3+
export interface CustomValidatorType<T> extends Validator<T> {
4+
// Custom validator is simple, just implements the base Validator interface
5+
}

src/types/date.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Validator } from './base'
2+
3+
export interface DateOptions {
4+
format: string
5+
delimiters: string[]
6+
strictMode: boolean
7+
}
8+
9+
export interface DateObj {
10+
y: string
11+
m: string
12+
d: string
13+
}
14+
15+
export interface IsBeforeOptions {
16+
comparisonDate?: string | number | Date
17+
}
18+
19+
export interface IsAfterOptions {
20+
comparisonDate?: string | number | Date
21+
}
22+
23+
export interface DateValidatorType extends Validator<Date> {
24+
// Base date validator is simple, just implements the base Validator interface
25+
}
26+
27+
export interface DatetimeValidatorType extends Validator<Date> {
28+
// Datetime validator is simple, just implements the base Validator interface
29+
}

src/types/enum.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { EnumValidator } from '../validators/enums'
2+
import type { Validator } from './base'
3+
4+
export interface EnumValidatorType<T extends string | number> extends Validator<T> {
5+
getAllowedValues: () => readonly T[]
6+
custom: (fn: (value: T) => boolean, message: string) => EnumValidator<T>
7+
}

src/types/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { ArrayValidatorType } from './array'
2+
import type { BooleanValidatorType } from './boolean'
3+
import type { CustomValidatorType } from './custom'
4+
import type { DatetimeValidatorType, DateValidatorType } from './date'
5+
import type { EnumValidatorType } from './enum'
6+
import type { BigintValidatorType, NumberValidatorType } from './number'
7+
import type { ObjectValidatorType } from './object'
8+
import type { PasswordValidatorType } from './password'
9+
// Re-export ValidationInstance type
10+
import type { StringValidatorType, TextValidatorType } from './string'
11+
import type { TimestampValidatorType, UnixValidatorType } from './timestamp'
12+
13+
export * from './array'
14+
15+
// Base types
16+
export * from './base'
17+
export * from './boolean'
18+
export * from './custom'
19+
export * from './date'
20+
export * from './enum'
21+
export * from './number'
22+
export * from './object'
23+
// Options types
24+
export * from './options'
25+
export * from './password'
26+
// Validator types
27+
export * from './string'
28+
29+
export * from './timestamp'
30+
31+
export interface ValidationInstance {
32+
string: () => StringValidatorType
33+
text: () => TextValidatorType
34+
number: () => NumberValidatorType
35+
bigint: () => BigintValidatorType
36+
array: <T>() => ArrayValidatorType<T>
37+
boolean: () => BooleanValidatorType
38+
enum: <T extends string | number>(values: readonly T[]) => EnumValidatorType<T>
39+
date: () => DateValidatorType
40+
datetime: () => DatetimeValidatorType
41+
object: <T extends Record<string, any>>() => ObjectValidatorType<T>
42+
custom: <T>(validationFn: (value: T) => boolean, message: string) => CustomValidatorType<T>
43+
timestamp: () => TimestampValidatorType
44+
unix: () => UnixValidatorType
45+
password: () => PasswordValidatorType
46+
}
47+
48+
export type ValidationType = {
49+
[K in keyof ValidationInstance]: ReturnType<ValidationInstance[K]>
50+
}[keyof ValidationInstance]

src/types/number.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type isDecimal from '../lib/isDecimal'
2+
import type { NumberValidator } from '../validators/numbers'
3+
import type { LengthValidator, Validator } from './base'
4+
5+
export interface IsFloatOptions {
6+
locale?: string
7+
min?: number
8+
max?: number
9+
lt?: number
10+
gt?: number
11+
}
12+
13+
export interface IsIntOptions {
14+
allow_leading_zeroes?: boolean
15+
min?: number
16+
max?: number
17+
lt?: number
18+
gt?: number
19+
}
20+
21+
export interface DecimalOptions {
22+
force_decimal: boolean
23+
decimal_digits: string
24+
locale: string
25+
}
26+
27+
export interface NumericOptions {
28+
no_symbols?: boolean
29+
locale?: string
30+
}
31+
32+
export interface NumberValidatorType extends Validator<number>, LengthValidator<NumberValidator> {
33+
integer: (options?: IsIntOptions) => NumberValidator
34+
float: (options?: IsFloatOptions) => NumberValidator
35+
decimal: (options?: Parameters<typeof isDecimal>[1]) => NumberValidator
36+
positive: () => NumberValidator
37+
negative: () => NumberValidator
38+
divisibleBy: (divisor: number) => NumberValidator
39+
custom: (fn: (value: number) => boolean, message: string) => NumberValidator
40+
}
41+
42+
export interface BigintValidatorType extends NumberValidatorType {}

0 commit comments

Comments
 (0)