Skip to content

Commit e9d9c0e

Browse files
fix: tests
1 parent 9507154 commit e9d9c0e

File tree

19 files changed

+335
-92
lines changed

19 files changed

+335
-92
lines changed

src/types/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ValidationResult {
2222

2323
export interface ValidationRule<T> {
2424
name: string
25-
test: (value: T) => boolean
25+
test: (value: T | null | undefined) => boolean
2626
message: string
2727
params?: Record<string, any>
2828
}
@@ -31,7 +31,7 @@ export interface Validator<T> {
3131
name: ValidationNames
3232
isRequired: boolean
3333
getRules: () => ValidationRule<T>[]
34-
test: (value: T) => boolean
34+
test: (value: T | null | undefined) => boolean
3535
validate: (value: T | undefined | null) => ValidationResult
3636
required: () => this
3737
optional: () => this

src/types/decimal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export interface DecimalValidatorType extends Validator<number>, LengthValidator
1212
positive: () => DecimalValidator
1313
negative: () => DecimalValidator
1414
divisibleBy: (divisor: number) => DecimalValidator
15-
custom: (fn: (value: number) => boolean, message: string) => DecimalValidator
15+
custom: (fn: (value: number | null | undefined) => boolean, message: string) => DecimalValidator
1616
}

src/types/float.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export interface FloatValidatorType extends Validator<number>, LengthValidator<F
1313
positive: () => FloatValidator
1414
negative: () => FloatValidator
1515
divisibleBy: (divisor: number) => FloatValidator
16-
custom: (fn: (value: number) => boolean, message: string) => FloatValidator
16+
custom: (fn: (value: number | null | undefined) => boolean, message: string) => FloatValidator
1717
}

src/types/json.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { JsonValidator } from '../validators/json'
21
import type { LengthValidator, Validator } from './base'
32

4-
export interface JsonValidatorType extends Validator<string>, LengthValidator<JsonValidator> {
5-
custom: (fn: (value: string) => boolean, message: string) => JsonValidator
3+
export interface JsonValidatorType extends Validator<string>, LengthValidator<any> {
4+
custom: (fn: (value: string | null | undefined) => boolean, message: string) => any
65
}

src/types/number.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface NumberValidatorType extends Validator<number>, LengthValidator<
1919
positive: () => NumberValidator
2020
negative: () => NumberValidator
2121
divisibleBy: (divisor: number) => NumberValidator
22-
custom: (fn: (value: number) => boolean, message: string) => NumberValidator
22+
custom: (fn: (value: number | null | undefined) => boolean, message: string) => NumberValidator
2323
}
2424

2525
export interface BigintValidatorType extends Validator<bigint> {

src/types/smallint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export interface SmallintValidatorType extends Validator<number>, LengthValidato
55
positive: () => SmallintValidator
66
negative: () => SmallintValidator
77
divisibleBy: (divisor: number) => SmallintValidator
8-
custom: (fn: (value: number) => boolean, message: string) => SmallintValidator
8+
custom: (fn: (value: number | null | undefined) => boolean, message: string) => SmallintValidator
99
}

src/types/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface StringValidatorType extends Validator<string>, LengthValidator<
8181
alphanumeric: () => StringValidator
8282
alpha: () => StringValidator
8383
numeric: () => StringValidator
84-
custom: (fn: (value: string) => boolean, message: string) => StringValidator
84+
custom: (fn: (value: string | null | undefined) => boolean, message: string) => StringValidator
8585
}
8686

8787
export interface TextValidatorType extends StringValidatorType {}

src/types/time.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export interface TimeValidatorType extends Validator<string> {
55
min: (min: string) => TimeValidator
66
max: (max: string) => TimeValidator
77
length: (length: number) => TimeValidator
8-
custom: (fn: (value: string) => boolean, message: string) => TimeValidator
8+
custom: (fn: (value: string | null | undefined) => boolean, message: string) => TimeValidator
99
}

src/validators/base.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ export abstract class BaseValidator<T> implements Validator<T> {
3030
validate(value: T | undefined | null): ValidationResult {
3131
const errors: ValidationError[] = []
3232

33-
if ((value === undefined || value === null || value === '')) {
34-
if (!this.isRequired) {
35-
return this.isPartOfShape
36-
? { valid: true, errors: {} }
37-
: { valid: true, errors: [] }
38-
}
39-
else {
40-
const error = { message: 'This field is required' }
41-
return this.isPartOfShape
42-
? { valid: false, errors: { [this.fieldName]: [error] } }
43-
: { valid: false, errors: [error] }
44-
}
33+
if (!this.isRequired && (value === undefined || value === null || value === '')) {
34+
return this.isPartOfShape
35+
? { valid: true, errors: {} }
36+
: { valid: true, errors: [] }
37+
}
38+
39+
if (this.isRequired && (value === undefined || value === null || value === '')) {
40+
const error = { message: 'This field is required' }
41+
return this.isPartOfShape
42+
? { valid: false, errors: { [this.fieldName]: [error] } }
43+
: { valid: false, errors: [error] }
4544
}
4645

4746
for (const rule of this.rules) {
@@ -69,7 +68,7 @@ export abstract class BaseValidator<T> implements Validator<T> {
6968
return this.rules
7069
}
7170

72-
test(value: T): boolean {
71+
test(value: T | null | undefined): boolean {
7372
return this.validate(value).valid
7473
}
7574

src/validators/decimal.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export class DecimalValidator extends NumberValidator implements DecimalValidato
2929
min(min: number): this {
3030
return this.addRule({
3131
name: 'min',
32-
test: (value: number) => value >= min,
32+
test: (value: number | null | undefined) => {
33+
if (typeof value !== 'number')
34+
return false
35+
return value >= min
36+
},
3337
message: 'Must be at least {min}',
3438
params: { min },
3539
})
@@ -38,7 +42,11 @@ export class DecimalValidator extends NumberValidator implements DecimalValidato
3842
max(max: number): this {
3943
return this.addRule({
4044
name: 'max',
41-
test: (value: number) => value <= max,
45+
test: (value: number | null | undefined) => {
46+
if (typeof value !== 'number')
47+
return false
48+
return value <= max
49+
},
4250
message: 'Must be at most {max}',
4351
params: { max },
4452
})
@@ -47,7 +55,11 @@ export class DecimalValidator extends NumberValidator implements DecimalValidato
4755
length(length: number): this {
4856
return this.addRule({
4957
name: 'length',
50-
test: (value: number) => value.toString().length === length,
58+
test: (value: number | null | undefined) => {
59+
if (typeof value !== 'number')
60+
return false
61+
return value.toString().length === length
62+
},
5163
message: 'Must be exactly {length} digits',
5264
params: { length },
5365
})
@@ -56,35 +68,59 @@ export class DecimalValidator extends NumberValidator implements DecimalValidato
5668
positive(): this {
5769
return this.addRule({
5870
name: 'positive',
59-
test: (value: number) => value > 0,
71+
test: (value: number | null | undefined) => {
72+
if (typeof value !== 'number')
73+
return false
74+
return value > 0
75+
},
6076
message: 'Must be a positive number',
6177
})
6278
}
6379

6480
negative(): this {
6581
return this.addRule({
6682
name: 'negative',
67-
test: (value: number) => value < 0,
83+
test: (value: number | null | undefined) => {
84+
if (typeof value !== 'number')
85+
return false
86+
return value < 0
87+
},
6888
message: 'Must be a negative number',
6989
})
7090
}
7191

7292
divisibleBy(divisor: number): this {
7393
return this.addRule({
7494
name: 'divisibleBy',
75-
test: (value: number) => isDivisibleBy(String(value), divisor),
95+
test: (value: number | null | undefined) => {
96+
if (typeof value !== 'number')
97+
return false
98+
return isDivisibleBy(String(value), divisor)
99+
},
76100
message: 'Must be divisible by {divisor}',
77101
params: { divisor },
78102
})
79103
}
80104

81-
custom(fn: (value: number) => boolean, message: string): this {
105+
custom(fn: (value: number | null | undefined) => boolean, message: string): this {
82106
return this.addRule({
83107
name: 'custom',
84108
test: fn,
85109
message,
86110
})
87111
}
112+
113+
validate(value: number | undefined | null): any {
114+
// Only allow actual numbers
115+
if (typeof value !== 'number' || Number.isNaN(value)) {
116+
const error = { message: 'This field is required' }
117+
return this.isPartOfShape
118+
? { valid: false, errors: { [this.fieldName]: [error] } }
119+
: { valid: false, errors: [error] }
120+
}
121+
// Otherwise, use the base validation
122+
return super.validate(value)
123+
}
88124
}
89125

90126
// Export a function to create decimal validators

0 commit comments

Comments
 (0)