Skip to content

Commit ee644aa

Browse files
fix: test types
1 parent 7baa0af commit ee644aa

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

src/lib/contains.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export default function contains(str: string, elem: string, options: ContainsOpt
2121
options = merge(options, defaultContainsOptions)
2222

2323
if (options.ignoreCase) {
24-
return str.toLowerCase().split(toString(elem).toLowerCase()).length > options.minOccurrences
24+
return str.toLowerCase().split(toString(elem).toLowerCase()).length > (options.minOccurrences ?? 0)
2525
}
2626

27-
return str.split(toString(elem)).length > options.minOccurrences
27+
return str.split(toString(elem)).length > (options.minOccurrences ?? 0)
2828
}

src/types/decimal.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import type isDecimal from '../lib/isDecimal'
21
import type { DecimalValidator } from '../validators/decimal'
32
import type { LengthValidator, Validator } from './base'
43

5-
export interface DecimalValidatorOptions {
6-
force_decimal: boolean
7-
decimal_digits: string
8-
locale: string
9-
}
10-
114
export interface DecimalValidatorType extends Validator<number>, LengthValidator<DecimalValidator> {
125
positive: () => DecimalValidator
136
negative: () => DecimalValidator

src/types/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export interface CurrencyOptions {
3434
allow_space_after_digits: boolean
3535
}
3636

37+
export interface DecimalValidatorOptions {
38+
force_decimal: boolean
39+
decimal_digits: string
40+
locale: string
41+
}
42+
3743
export interface IsIMEIOptions {
3844
allow_hyphens?: boolean | string
3945
}

src/validators/arrays.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export class ArrayValidator<T> extends BaseValidator<T[]> implements ArrayValida
1616
min(length: number): this {
1717
return this.addRule({
1818
name: 'min',
19-
test: (value: T[]) => value.length >= length,
19+
test: (value: T[] | null | undefined) => {
20+
if (value === null || value === undefined)
21+
return false
22+
return value.length >= length
23+
},
2024
message: 'Must have at least {length} items',
2125
params: { length },
2226
})
@@ -25,7 +29,11 @@ export class ArrayValidator<T> extends BaseValidator<T[]> implements ArrayValida
2529
max(length: number): this {
2630
return this.addRule({
2731
name: 'max',
28-
test: (value: T[]) => value.length <= length,
32+
test: (value: T[] | null | undefined) => {
33+
if (value === null || value === undefined)
34+
return false
35+
return value.length <= length
36+
},
2937
message: 'Must have at most {length} items',
3038
params: { length },
3139
})
@@ -34,7 +42,11 @@ export class ArrayValidator<T> extends BaseValidator<T[]> implements ArrayValida
3442
length(length: number): this {
3543
return this.addRule({
3644
name: 'length',
37-
test: (value: T[]) => value.length === length,
45+
test: (value: T[] | null | undefined) => {
46+
if (value === null || value === undefined)
47+
return false
48+
return value.length === length
49+
},
3850
message: 'Must have exactly {length} items',
3951
params: { length },
4052
})
@@ -43,15 +55,21 @@ export class ArrayValidator<T> extends BaseValidator<T[]> implements ArrayValida
4355
each(validator: Validator<T>): this {
4456
return this.addRule({
4557
name: 'each',
46-
test: (value: T[]) => value.every(item => validator.test(item)),
58+
test: (value: T[] | null | undefined) => {
59+
if (value === null || value === undefined)
60+
return false
61+
return value.every(item => validator.test(item))
62+
},
4763
message: 'Each item in array is invalid',
4864
})
4965
}
5066

5167
unique(): this {
5268
return this.addRule({
5369
name: 'unique',
54-
test: (value: T[]) => {
70+
test: (value: T[] | null | undefined) => {
71+
if (value === null || value === undefined)
72+
return false
5573
const seen = new Set()
5674
return value.every((item) => {
5775
const key = JSON.stringify(item)

src/validators/decimal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DecimalValidatorOptions, DecimalValidatorType, ValidationNames } from '../types'
1+
import type { DecimalValidatorType, ValidationNames } from '../types'
22
import isDivisibleBy from '../lib/isDivisibleBy'
33
import { NumberValidator } from './numbers'
44

0 commit comments

Comments
 (0)