@@ -80,6 +80,32 @@ test.group('schema | number.optional', () => {
80
80
expectTypeOf ( value ) . toEqualTypeOf < number | undefined > ( )
81
81
assert . deepEqual ( value , - 22.198 )
82
82
} )
83
+
84
+ test ( 'allow conditional optional' , ( { assert, expectTypeOf } ) => {
85
+ const value = schema . number . optionalWhen ( true ) ( 'PORT' )
86
+ expectTypeOf ( value ) . toEqualTypeOf < number | undefined > ( )
87
+ assert . isUndefined ( value )
88
+
89
+ const value2 = schema . number . optionalWhen ( false ) ( 'PORT' , '22' )
90
+ expectTypeOf ( value2 ) . toEqualTypeOf < number | undefined > ( )
91
+ assert . deepEqual ( value2 , 22 )
92
+
93
+ const fn = ( ) => schema . number . optionalWhen ( false ) ( 'PORT' )
94
+ assert . throws ( fn , 'Missing environment variable "PORT"' )
95
+ } )
96
+
97
+ test ( 'allow conditional optional with function' , ( { assert, expectTypeOf } ) => {
98
+ const value = schema . number . optionalWhen ( ( ) => true ) ( 'PORT' )
99
+ expectTypeOf ( value ) . toEqualTypeOf < number | undefined > ( )
100
+ assert . isUndefined ( value )
101
+
102
+ const value2 = schema . number . optionalWhen ( ( ) => false ) ( 'PORT' , '22' )
103
+ expectTypeOf ( value2 ) . toEqualTypeOf < number | undefined > ( )
104
+ assert . deepEqual ( value2 , 22 )
105
+
106
+ const fn = ( ) => schema . number . optionalWhen ( ( ) => false ) ( 'PORT' )
107
+ assert . throws ( fn , 'Missing environment variable "PORT"' )
108
+ } )
83
109
} )
84
110
85
111
test . group ( 'schema | string' , ( ) => {
@@ -154,6 +180,32 @@ test.group('schema | string.optional', () => {
154
180
'mailgun:1234/v1'
155
181
)
156
182
} )
183
+
184
+ test ( 'allow conditional optional' , ( { assert, expectTypeOf } ) => {
185
+ const value = schema . string . optionalWhen ( true ) ( 'PORT' )
186
+ expectTypeOf ( value ) . toEqualTypeOf < string | undefined > ( )
187
+ assert . deepEqual ( value , undefined )
188
+
189
+ const value2 = schema . string . optionalWhen ( false ) ( 'PORT' , '22' )
190
+ expectTypeOf ( value2 ) . toEqualTypeOf < string | undefined > ( )
191
+ assert . deepEqual ( value2 , '22' )
192
+
193
+ const fn = ( ) => schema . string . optionalWhen ( false ) ( 'PORT' )
194
+ assert . throws ( fn , 'Missing environment variable "PORT"' )
195
+ } )
196
+
197
+ test ( 'allow conditional optional with function' , ( { assert, expectTypeOf } ) => {
198
+ const value = schema . string . optionalWhen ( ( ) => true ) ( 'PORT' )
199
+ expectTypeOf ( value ) . toEqualTypeOf < string | undefined > ( )
200
+ assert . deepEqual ( value , undefined )
201
+
202
+ const value2 = schema . string . optionalWhen ( ( ) => false ) ( 'PORT' , '22' )
203
+ expectTypeOf ( value2 ) . toEqualTypeOf < string | undefined > ( )
204
+ assert . deepEqual ( value2 , '22' )
205
+
206
+ const fn = ( ) => schema . string . optionalWhen ( ( ) => false ) ( 'PORT' )
207
+ assert . throws ( fn , 'Missing environment variable "PORT"' )
208
+ } )
157
209
} )
158
210
159
211
test . group ( 'schema | boolean' , ( ) => {
@@ -238,6 +290,32 @@ test.group('schema | boolean.optional', () => {
238
290
expectTypeOf ( value ) . toEqualTypeOf < boolean | undefined > ( )
239
291
assert . deepEqual ( value , false )
240
292
} )
293
+
294
+ test ( 'allow conditional optional' , ( { assert, expectTypeOf } ) => {
295
+ const value = schema . boolean . optionalWhen ( true ) ( 'PORT' )
296
+ expectTypeOf ( value ) . toEqualTypeOf < boolean | undefined > ( )
297
+ assert . deepEqual ( value , undefined )
298
+
299
+ const value2 = schema . boolean . optionalWhen ( false ) ( 'PORT' , 'true' )
300
+ expectTypeOf ( value2 ) . toEqualTypeOf < boolean | undefined > ( )
301
+ assert . deepEqual ( value2 , true )
302
+
303
+ const fn = ( ) => schema . boolean . optionalWhen ( false ) ( 'PORT' )
304
+ assert . throws ( fn , 'Missing environment variable "PORT"' )
305
+ } )
306
+
307
+ test ( 'allow conditional optional with function' , ( { assert, expectTypeOf } ) => {
308
+ const value = schema . boolean . optionalWhen ( ( ) => true ) ( 'PORT' )
309
+ expectTypeOf ( value ) . toEqualTypeOf < boolean | undefined > ( )
310
+ assert . deepEqual ( value , undefined )
311
+
312
+ const value2 = schema . boolean . optionalWhen ( ( ) => false ) ( 'PORT' , 'true' )
313
+ expectTypeOf ( value2 ) . toEqualTypeOf < boolean | undefined > ( )
314
+ assert . deepEqual ( value2 , true )
315
+
316
+ const fn = ( ) => schema . boolean . optionalWhen ( ( ) => false ) ( 'PORT' )
317
+ assert . throws ( fn , 'Missing environment variable "PORT"' )
318
+ } )
241
319
} )
242
320
243
321
test . group ( 'schema | enum' , ( ) => {
0 commit comments