Skip to content

Commit eda824f

Browse files
chore: improve tests and validator type
1 parent 8ca9253 commit eda824f

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ export interface Validator<T> {
249249
name: ValidationNames
250250
test: (value: T) => boolean
251251
validate: (value: T) => ValidationResult
252-
required: () => Validator<T>
253-
optional: () => Validator<T>
252+
required: () => this
253+
optional: () => this
254254
}
255255

256256
export interface ValidationConfig {

src/validators/password.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class PasswordValidator extends BaseValidator<string> implements Password
66

77
constructor() {
88
super()
9+
this.isRequired = false // Make password optional by default
910
this.addRule({
1011
name: 'string',
1112
test: (value: unknown): value is string => typeof value === 'string',

src/validators/strings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class StringValidator extends BaseValidator<string> implements StringVali
1111

1212
constructor() {
1313
super()
14+
this.isRequired = false // Make string optional by default
1415
this.addRule({
1516
name: 'string',
1617
test: (value: unknown): value is string => typeof value === 'string',

test/validation.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,77 @@ describe('Validation Library', () => {
2020
expect(validator.test('')).toBe(true)
2121
})
2222

23+
test('required string validation', () => {
24+
const validator = v.string().required()
25+
expect(validator.test('hello')).toBe(true)
26+
expect(validator.test('123')).toBe(true)
27+
expect(validator.test('')).toBe(false)
28+
expect(validator.test(null as any)).toBe(false)
29+
expect(validator.test(undefined as any)).toBe(false)
30+
})
31+
32+
test('optional string validation', () => {
33+
const validator = v.string().optional()
34+
expect(validator.test('hello')).toBe(true)
35+
expect(validator.test('123')).toBe(true)
36+
expect(validator.test('')).toBe(true)
37+
expect(validator.test(null as any)).toBe(true)
38+
expect(validator.test(undefined as any)).toBe(true)
39+
})
40+
41+
test('required string with other validations', () => {
42+
const validator = v.string().required().min(3).max(10)
43+
expect(validator.test('hello')).toBe(true)
44+
expect(validator.test('hi')).toBe(false) // too short
45+
expect(validator.test('hello world')).toBe(false) // too long
46+
expect(validator.test('')).toBe(false) // required
47+
expect(validator.test(null as any)).toBe(false) // required
48+
})
49+
50+
test('optional string with other validations', () => {
51+
const validator = v.string().optional().min(3).max(10)
52+
expect(validator.test('hello')).toBe(true)
53+
expect(validator.test('hi')).toBe(false) // too short
54+
expect(validator.test('hello world')).toBe(false) // too long
55+
expect(validator.test('')).toBe(true) // optional
56+
expect(validator.test(null as any)).toBe(true) // optional
57+
expect(validator.test(undefined as any)).toBe(true) // optional
58+
})
59+
60+
test('required string validation with detailed errors', () => {
61+
const validator = v.string().required()
62+
const result = validator.validate('')
63+
expect(result.valid).toBe(false)
64+
if (isValidationErrorArray(result.errors)) {
65+
expect(result.errors[0].message).toBe('This field is required')
66+
}
67+
})
68+
69+
test('required string validation with null', () => {
70+
const validator = v.string().required()
71+
const result = validator.validate(null as any)
72+
expect(result.valid).toBe(false)
73+
if (isValidationErrorArray(result.errors)) {
74+
expect(result.errors[0].message).toBe('This field is required')
75+
}
76+
})
77+
78+
test('required string validation with undefined', () => {
79+
const validator = v.string().required()
80+
const result = validator.validate(undefined as any)
81+
expect(result.valid).toBe(false)
82+
if (isValidationErrorArray(result.errors)) {
83+
expect(result.errors[0].message).toBe('This field is required')
84+
}
85+
})
86+
87+
test('optional string validation with empty values', () => {
88+
const validator = v.string().optional()
89+
expect(validator.validate('').valid).toBe(true)
90+
expect(validator.validate(null as any).valid).toBe(true)
91+
expect(validator.validate(undefined as any).valid).toBe(true)
92+
})
93+
2394
test('min length validation', () => {
2495
const validator = v.string().min(5)
2596
expect(validator.test('hello')).toBe(true)

0 commit comments

Comments
 (0)