Skip to content

Commit f588e24

Browse files
chore: improve types
1 parent 72dc410 commit f588e24

File tree

14 files changed

+37
-4
lines changed

14 files changed

+37
-4
lines changed

src/types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import type isURL from './lib/isURL'
55
import type { ArrayValidator } from './validators/arrays'
66
import type { BooleanValidator } from './validators/booleans'
77
import type { CustomValidator } from './validators/custom'
8-
import type { DateValidator } from './validators/dates'
9-
import type { DatetimeValidator } from './validators/datetimes'
108
import type { EnumValidator } from './validators/enums'
119
import type { NumberValidator } from './validators/numbers'
1210
import type { ObjectValidator } from './validators/objects'
1311
import type { PasswordValidator } from './validators/password'
1412
import type { StringValidator } from './validators/strings'
15-
import type { TimestampValidator } from './validators/timestamps'
16-
import type { UnixValidator } from './validators/unix'
13+
14+
// Define unique symbols for schema properties
15+
export const SCHEMA_NAME: unique symbol = Symbol('schema_name')
16+
export const INPUT_TYPE: unique symbol = Symbol('input_type')
17+
export const OUTPUT_TYPE: unique symbol = Symbol('output_type')
18+
export const COMPUTED_TYPE: unique symbol = Symbol('computed_type')
19+
export const PARSE: unique symbol = Symbol('parse')
1720

1821
export interface ValidationError {
1922
message: string
@@ -243,6 +246,7 @@ export interface NormalizeEmailOptions {
243246
}
244247

245248
export interface Validator<T> {
249+
name: 'string' | 'number' | 'array' | 'boolean' | 'enum' | 'date' | 'datetime' | 'object' | 'custom' | 'timestamp' | 'unix' | 'password'
246250
test: (value: T) => boolean
247251
validate: (value: T) => ValidationResult
248252
required: () => Validator<T>
@@ -350,3 +354,7 @@ export interface ValidationInstance {
350354
unix: () => UnixValidatorType
351355
password: () => PasswordValidatorType
352356
}
357+
358+
export type ValidationType = StringValidatorType | NumberValidatorType | ArrayValidatorType<any> | BooleanValidatorType | EnumValidatorType<any> | DateValidatorType | DatetimeValidatorType | ObjectValidatorType<any> | CustomValidatorType<any> | TimestampValidatorType | UnixValidatorType | PasswordValidatorType
359+
360+
export type Infer<T> = T extends Validator<infer U> ? U : never

src/validators/arrays.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { ArrayValidatorType, Validator } from '../types'
22
import { BaseValidator } from './base'
33

44
export class ArrayValidator<T> extends BaseValidator<T[]> implements ArrayValidatorType<T> {
5+
public name: string = 'array'
6+
57
constructor() {
68
super()
79
this.addRule({

src/validators/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export abstract class BaseValidator<T> {
55
protected isRequired = true
66
protected fieldName = 'value'
77
protected isPartOfShape = false
8+
public name: string = 'base'
89

910
required(): this {
1011
this.isRequired = true

src/validators/booleans.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { BooleanValidatorType } from '../types'
22
import { BaseValidator } from './base'
33

44
export class BooleanValidator extends BaseValidator<boolean> implements BooleanValidatorType {
5+
public name: string = 'boolean'
6+
57
constructor() {
68
super()
79
this.addRule({

src/validators/custom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { CustomValidatorType } from '../types'
22
import { BaseValidator } from './base'
33

44
export class CustomValidator<T> extends BaseValidator<T> implements CustomValidatorType<T> {
5+
public name: string = 'custom'
6+
57
constructor(validationFn: (value: T) => boolean, message: string) {
68
super()
79
this.addRule({

src/validators/dates.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { DateValidatorType } from '../types'
22
import { BaseValidator } from './base'
33

44
export class DateValidator extends BaseValidator<Date> implements DateValidatorType {
5+
public name: string = 'date'
6+
57
constructor() {
68
super()
79
this.addRule({

src/validators/datetimes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { DatetimeValidatorType } from '../types'
22
import { BaseValidator } from './base'
33

44
export class DatetimeValidator extends BaseValidator<Date> implements DatetimeValidatorType {
5+
public name: string = 'datetime'
6+
57
constructor() {
68
super()
79
this.addRule({

src/validators/enums.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { EnumValidatorType } from '../types'
22
import { BaseValidator } from './base'
33

44
export class EnumValidator<T extends string | number> extends BaseValidator<T> implements EnumValidatorType<T> {
5+
public name: string = 'enum'
6+
57
private allowedValues: readonly T[]
68

79
constructor(allowedValues: readonly T[]) {

src/validators/numbers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import isInt from '../lib/isInt'
66
import { BaseValidator } from './base'
77

88
export class NumberValidator extends BaseValidator<number> implements NumberValidatorType {
9+
public name: string = 'number'
10+
911
constructor() {
1012
super()
1113
this.addRule({

src/validators/objects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { ObjectValidatorType, ValidationErrorMap, ValidationResult, Validat
22
import { BaseValidator } from './base'
33

44
export class ObjectValidator<T extends Record<string, any>> extends BaseValidator<T> implements ObjectValidatorType<T> {
5+
public name: string = 'object'
6+
57
private schema: Record<string, Validator<any>> = {}
68
private strictMode = false
79

0 commit comments

Comments
 (0)