@@ -20,6 +20,77 @@ describe('Validation Library', () => {
20
20
expect ( validator . test ( '' ) ) . toBe ( true )
21
21
} )
22
22
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
+
23
94
test ( 'min length validation' , ( ) => {
24
95
const validator = v . string ( ) . min ( 5 )
25
96
expect ( validator . test ( 'hello' ) ) . toBe ( true )
0 commit comments