@@ -4,7 +4,7 @@ import { enum_ } from '../../src/validators/enums'
4
4
describe ( 'EnumValidator' , ( ) => {
5
5
describe ( 'basic validation' , ( ) => {
6
6
test ( 'should validate string enum values' , ( ) => {
7
- const validator = enum_ ( [ 'red' , 'green' , 'blue' ] as const )
7
+ const validator = enum_ ( [ 'red' , 'green' , 'blue' ] )
8
8
expect ( validator . test ( 'red' ) ) . toBe ( true )
9
9
expect ( validator . test ( 'green' ) ) . toBe ( true )
10
10
expect ( validator . test ( 'blue' ) ) . toBe ( true )
@@ -13,37 +13,24 @@ describe('EnumValidator', () => {
13
13
expect ( validator . test ( '' as any ) ) . toBe ( true ) // empty string is valid when optional
14
14
} )
15
15
16
- test ( 'should validate number enum values' , ( ) => {
17
- const validator = enum_ ( [ 1 , 2 , 3 ] as const )
18
- expect ( validator . test ( 1 ) ) . toBe ( true )
19
- expect ( validator . test ( 2 ) ) . toBe ( true )
20
- expect ( validator . test ( 3 ) ) . toBe ( true )
21
- expect ( validator . test ( 4 as any ) ) . toBe ( false )
22
- expect ( validator . test ( 0 as any ) ) . toBe ( false )
23
- expect ( validator . test ( '1' as any ) ) . toBe ( false ) // type sensitive
24
- } )
25
-
26
16
test ( 'should have correct name' , ( ) => {
27
- const validator = enum_ ( [ 'a' , 'b' ] as const )
17
+ const validator = enum_ ( [ 'a' , 'b' ] )
28
18
expect ( validator . name ) . toBe ( 'enum' )
29
19
} )
30
20
} )
31
21
32
- describe ( 'mixed type enums' , ( ) => {
33
- test ( 'should validate mixed string and number enums' , ( ) => {
34
- const validator = enum_ ( [ 'active' , 'inactive' , 1 , 2 ] as const )
22
+ describe ( 'string enums' , ( ) => {
23
+ test ( 'should validate mixed string enums' , ( ) => {
24
+ const validator = enum_ ( [ 'active' , 'inactive' , 'pending' , 'completed' ] )
35
25
expect ( validator . test ( 'active' ) ) . toBe ( true )
36
26
expect ( validator . test ( 'inactive' ) ) . toBe ( true )
37
- expect ( validator . test ( 1 ) ) . toBe ( true )
38
- expect ( validator . test ( 2 ) ) . toBe ( true )
39
- expect ( validator . test ( '1' as any ) ) . toBe ( false )
40
- expect ( validator . test ( '2' as any ) ) . toBe ( false )
41
- expect ( validator . test ( 3 as any ) ) . toBe ( false )
42
- expect ( validator . test ( 'pending' as any ) ) . toBe ( false )
27
+ expect ( validator . test ( 'pending' ) ) . toBe ( true )
28
+ expect ( validator . test ( 'completed' ) ) . toBe ( true )
29
+ expect ( validator . test ( 'archived' as any ) ) . toBe ( false )
43
30
} )
44
31
45
32
test ( 'should handle string representations of booleans' , ( ) => {
46
- const validator = enum_ ( [ 'true' , 'false' , 'maybe' ] as const )
33
+ const validator = enum_ ( [ 'true' , 'false' , 'maybe' ] )
47
34
expect ( validator . test ( 'true' ) ) . toBe ( true )
48
35
expect ( validator . test ( 'false' ) ) . toBe ( true )
49
36
expect ( validator . test ( 'maybe' ) ) . toBe ( true )
@@ -56,7 +43,7 @@ describe('EnumValidator', () => {
56
43
57
44
describe ( 'real-world enum scenarios' , ( ) => {
58
45
test ( 'should validate user roles' , ( ) => {
59
- const roleValidator = enum_ ( [ 'admin' , 'user' , 'guest' , 'moderator' ] as const )
46
+ const roleValidator = enum_ ( [ 'admin' , 'user' , 'guest' , 'moderator' ] )
60
47
expect ( roleValidator . test ( 'admin' ) ) . toBe ( true )
61
48
expect ( roleValidator . test ( 'user' ) ) . toBe ( true )
62
49
expect ( roleValidator . test ( 'guest' ) ) . toBe ( true )
@@ -66,16 +53,16 @@ describe('EnumValidator', () => {
66
53
} )
67
54
68
55
test ( 'should validate status codes' , ( ) => {
69
- const statusValidator = enum_ ( [ 200 , 201 , 400 , 401 , 403 , 404 , 500 ] as const )
70
- expect ( statusValidator . test ( 200 ) ) . toBe ( true )
71
- expect ( statusValidator . test ( 404 ) ) . toBe ( true )
72
- expect ( statusValidator . test ( 500 ) ) . toBe ( true )
73
- expect ( statusValidator . test ( 418 as any ) ) . toBe ( false ) // I'm a teapot
74
- expect ( statusValidator . test ( ' 200' as any ) ) . toBe ( false )
56
+ const statusValidator = enum_ ( [ ' 200' , ' 201' , ' 400' , ' 401' , ' 403' , ' 404' , ' 500' ] )
57
+ expect ( statusValidator . test ( ' 200' ) ) . toBe ( true )
58
+ expect ( statusValidator . test ( ' 404' ) ) . toBe ( true )
59
+ expect ( statusValidator . test ( ' 500' ) ) . toBe ( true )
60
+ expect ( statusValidator . test ( ' 418' as any ) ) . toBe ( false ) // I'm a teapot
61
+ expect ( statusValidator . test ( 200 as any ) ) . toBe ( false )
75
62
} )
76
63
77
64
test ( 'should validate priority levels' , ( ) => {
78
- const priorityValidator = enum_ ( [ 'low' , 'medium' , 'high' , 'urgent' ] as const )
65
+ const priorityValidator = enum_ ( [ 'low' , 'medium' , 'high' , 'urgent' ] )
79
66
expect ( priorityValidator . test ( 'low' ) ) . toBe ( true )
80
67
expect ( priorityValidator . test ( 'medium' ) ) . toBe ( true )
81
68
expect ( priorityValidator . test ( 'high' ) ) . toBe ( true )
@@ -85,7 +72,7 @@ describe('EnumValidator', () => {
85
72
} )
86
73
87
74
test ( 'should validate file types' , ( ) => {
88
- const fileTypeValidator = enum_ ( [ '.jpg' , '.png' , '.gif' , '.pdf' , '.doc' ] as const )
75
+ const fileTypeValidator = enum_ ( [ '.jpg' , '.png' , '.gif' , '.pdf' , '.doc' ] )
89
76
expect ( fileTypeValidator . test ( '.jpg' ) ) . toBe ( true )
90
77
expect ( fileTypeValidator . test ( '.png' ) ) . toBe ( true )
91
78
expect ( fileTypeValidator . test ( '.pdf' ) ) . toBe ( true )
@@ -96,7 +83,7 @@ describe('EnumValidator', () => {
96
83
97
84
describe ( 'custom validation' , ( ) => {
98
85
test ( 'custom() should accept custom validation functions' , ( ) => {
99
- const validator = enum_ ( [ 'small' , 'medium' , 'large' ] as const ) . custom (
86
+ const validator = enum_ ( [ 'small' , 'medium' , 'large' ] ) . custom (
100
87
value => value !== 'medium' ,
101
88
'Medium size not available' ,
102
89
)
@@ -107,7 +94,7 @@ describe('EnumValidator', () => {
107
94
} )
108
95
109
96
test ( 'should provide custom error messages' , ( ) => {
110
- const validator = enum_ ( [ 'read' , 'write' , 'execute' ] as const ) . custom (
97
+ const validator = enum_ ( [ 'read' , 'write' , 'execute' ] ) . custom (
111
98
value => value !== 'execute' ,
112
99
'Execute permission not allowed' ,
113
100
)
@@ -120,7 +107,7 @@ describe('EnumValidator', () => {
120
107
} )
121
108
122
109
test ( 'should chain enum and custom validations' , ( ) => {
123
- const validator = enum_ ( [ 'bronze' , 'silver' , 'gold' , 'platinum' ] as const ) . custom (
110
+ const validator = enum_ ( [ 'bronze' , 'silver' , 'gold' , 'platinum' ] ) . custom (
124
111
value => value !== 'bronze' ,
125
112
'Bronze tier not supported' ,
126
113
)
@@ -134,43 +121,43 @@ describe('EnumValidator', () => {
134
121
135
122
describe ( 'getAllowedValues method' , ( ) => {
136
123
test ( 'should return all allowed values' , ( ) => {
137
- const values = [ 'apple' , 'banana' , 'cherry' ] as const
124
+ const values = [ 'apple' , 'banana' , 'cherry' ]
138
125
const validator = enum_ ( values )
139
126
expect ( validator . getAllowedValues ( ) ) . toEqual ( values )
140
127
} )
141
128
142
- test ( 'should return numeric values correctly' , ( ) => {
143
- const values = [ 1 , 2 , 3 , 5 , 8 ] as const
129
+ test ( 'should return string values correctly' , ( ) => {
130
+ const values = [ 'one' , 'two' , 'three' , 'five' , 'eight' ]
144
131
const validator = enum_ ( values )
145
132
expect ( validator . getAllowedValues ( ) ) . toEqual ( values )
146
133
} )
147
134
148
- test ( 'should return mixed type values correctly' , ( ) => {
149
- const values = [ 'active' , 1 , 'inactive' , 0 ] as const
135
+ test ( 'should return mixed string values correctly' , ( ) => {
136
+ const values = [ 'active' , 'one' , 'inactive' , 'zero' ]
150
137
const validator = enum_ ( values )
151
138
expect ( validator . getAllowedValues ( ) ) . toEqual ( values )
152
139
} )
153
140
} )
154
141
155
142
describe ( 'required and optional' , ( ) => {
156
143
test ( 'required() should reject null/undefined' , ( ) => {
157
- const validator = enum_ ( [ 'yes' , 'no' ] as const ) . required ( )
144
+ const validator = enum_ ( [ 'yes' , 'no' ] ) . required ( )
158
145
expect ( validator . test ( 'yes' ) ) . toBe ( true )
159
146
expect ( validator . test ( 'no' ) ) . toBe ( true )
160
147
expect ( validator . test ( null as any ) ) . toBe ( false ) // required validator should reject null
161
148
expect ( validator . test ( undefined as any ) ) . toBe ( false ) // required validator should reject undefined
162
149
} )
163
150
164
151
test ( 'optional() should accept null/undefined' , ( ) => {
165
- const validator = enum_ ( [ 'yes' , 'no' ] as const ) . optional ( )
152
+ const validator = enum_ ( [ 'yes' , 'no' ] ) . optional ( )
166
153
expect ( validator . test ( 'yes' ) ) . toBe ( true )
167
154
expect ( validator . test ( 'no' ) ) . toBe ( true )
168
155
expect ( validator . test ( null as any ) ) . toBe ( true )
169
156
expect ( validator . test ( undefined as any ) ) . toBe ( true )
170
157
} )
171
158
172
159
test ( 'should work with custom validations when optional' , ( ) => {
173
- const validator = enum_ ( [ 'small' , 'large' ] as const )
160
+ const validator = enum_ ( [ 'small' , 'large' ] )
174
161
. optional ( )
175
162
. custom ( value => value !== 'small' , 'Small not allowed' )
176
163
@@ -184,7 +171,7 @@ describe('EnumValidator', () => {
184
171
185
172
describe ( 'validation results' , ( ) => {
186
173
test ( 'should return detailed validation results' , ( ) => {
187
- const validator = enum_ ( [ 'cat' , 'dog' , 'bird' ] as const )
174
+ const validator = enum_ ( [ 'cat' , 'dog' , 'bird' ] )
188
175
const result = validator . validate ( 'fish' as any )
189
176
expect ( result . valid ) . toBe ( false )
190
177
expect ( Array . isArray ( result . errors ) ) . toBe ( true )
@@ -194,7 +181,7 @@ describe('EnumValidator', () => {
194
181
} )
195
182
196
183
test ( 'should return multiple errors for multiple failed validations' , ( ) => {
197
- const validator = enum_ ( [ 'xs' , 's' , 'm' , 'l' , 'xl' ] as const ) . custom (
184
+ const validator = enum_ ( [ 'xs' , 's' , 'm' , 'l' , 'xl' ] ) . custom (
198
185
value => value !== 'xs' ,
199
186
'XS size unavailable' ,
200
187
)
@@ -209,17 +196,17 @@ describe('EnumValidator', () => {
209
196
210
197
describe ( 'edge cases' , ( ) => {
211
198
test ( 'should handle single value enums' , ( ) => {
212
- const stringValidator = enum_ ( [ 'only' ] as const )
199
+ const stringValidator = enum_ ( [ 'only' ] )
213
200
expect ( stringValidator . test ( 'only' ) ) . toBe ( true )
214
201
expect ( stringValidator . test ( 'other' as any ) ) . toBe ( false )
215
202
216
- const numberValidator = enum_ ( [ 42 ] as const )
217
- expect ( numberValidator . test ( 42 ) ) . toBe ( true )
218
- expect ( numberValidator . test ( 41 as any ) ) . toBe ( false )
203
+ const singleValidator = enum_ ( [ 'single' ] )
204
+ expect ( singleValidator . test ( 'single' ) ) . toBe ( true )
205
+ expect ( singleValidator . test ( 'other' as any ) ) . toBe ( false )
219
206
} )
220
207
221
208
test ( 'should handle special string values' , ( ) => {
222
- const validator = enum_ ( [ '' , ' ' , '\n' , '\t' , 'null' , 'undefined' ] as const )
209
+ const validator = enum_ ( [ '' , ' ' , '\n' , '\t' , 'null' , 'undefined' ] )
223
210
expect ( validator . test ( '' ) ) . toBe ( true )
224
211
expect ( validator . test ( ' ' ) ) . toBe ( true )
225
212
expect ( validator . test ( '\n' ) ) . toBe ( true )
@@ -230,19 +217,19 @@ describe('EnumValidator', () => {
230
217
expect ( validator . test ( undefined as any ) ) . toBe ( true ) // undefined is valid when optional
231
218
} )
232
219
233
- test ( 'should handle numeric edge cases' , ( ) => {
234
- const validator = enum_ ( [ 0 , - 1 , 3.14 , Number . POSITIVE_INFINITY , Number . NEGATIVE_INFINITY ] as const )
235
- expect ( validator . test ( 0 ) ) . toBe ( true )
236
- expect ( validator . test ( - 1 ) ) . toBe ( true )
237
- expect ( validator . test ( 3.14 ) ) . toBe ( true )
238
- expect ( validator . test ( Number . POSITIVE_INFINITY ) ) . toBe ( true )
239
- expect ( validator . test ( Number . NEGATIVE_INFINITY ) ) . toBe ( true )
240
- expect ( validator . test ( Number . NaN as any ) ) . toBe ( false )
241
- expect ( validator . test ( 1 as any ) ) . toBe ( false )
220
+ test ( 'should handle string edge cases' , ( ) => {
221
+ const validator = enum_ ( [ '0' , '-1' , ' 3.14' , 'infinity' , '-infinity' ] )
222
+ expect ( validator . test ( '0' ) ) . toBe ( true )
223
+ expect ( validator . test ( '-1' ) ) . toBe ( true )
224
+ expect ( validator . test ( ' 3.14' ) ) . toBe ( true )
225
+ expect ( validator . test ( 'infinity' ) ) . toBe ( true )
226
+ expect ( validator . test ( '-infinity' ) ) . toBe ( true )
227
+ expect ( validator . test ( ' NaN' as any ) ) . toBe ( false )
228
+ expect ( validator . test ( '1' as any ) ) . toBe ( false )
242
229
} )
243
230
244
231
test ( 'should handle unicode strings' , ( ) => {
245
- const validator = enum_ ( [ '🚀' , '🌟' , '✨' , 'café' , 'naïve' ] as const )
232
+ const validator = enum_ ( [ '🚀' , '🌟' , '✨' , 'café' , 'naïve' ] )
246
233
expect ( validator . test ( '🚀' ) ) . toBe ( true )
247
234
expect ( validator . test ( '🌟' ) ) . toBe ( true )
248
235
expect ( validator . test ( '✨' ) ) . toBe ( true )
@@ -253,7 +240,7 @@ describe('EnumValidator', () => {
253
240
} )
254
241
255
242
test ( 'should be case and type sensitive' , ( ) => {
256
- const validator = enum_ ( [ 'True' , 'False' , '0' , '1' ] as const )
243
+ const validator = enum_ ( [ 'True' , 'False' , '0' , '1' ] )
257
244
expect ( validator . test ( 'True' ) ) . toBe ( true )
258
245
expect ( validator . test ( 'False' ) ) . toBe ( true )
259
246
expect ( validator . test ( '0' ) ) . toBe ( true )
@@ -269,15 +256,15 @@ describe('EnumValidator', () => {
269
256
270
257
describe ( 'type safety' , ( ) => {
271
258
test ( 'should work with readonly arrays' , ( ) => {
272
- const colors = [ 'red' , 'green' , 'blue' ] as const
259
+ const colors = [ 'red' , 'green' , 'blue' ]
273
260
const validator = enum_ ( colors )
274
261
expect ( validator . test ( 'red' ) ) . toBe ( true )
275
262
expect ( validator . test ( 'purple' as any ) ) . toBe ( false )
276
263
expect ( validator . getAllowedValues ( ) ) . toEqual ( colors )
277
264
} )
278
265
279
266
test ( 'should maintain type information' , ( ) => {
280
- const sizes = [ 'xs' , 's' , 'm' , 'l' , 'xl' ] as const
267
+ const sizes = [ 'xs' , 's' , 'm' , 'l' , 'xl' ]
281
268
const validator = enum_ ( sizes )
282
269
// TypeScript would enforce that only these values are valid
283
270
expect ( validator . test ( 'm' ) ) . toBe ( true )
0 commit comments