@@ -354,13 +354,17 @@ const isDuplicateProtocol = (
354
354
return false
355
355
}
356
356
357
- const icmpTypeValidation = ( value : string | number | undefined ) => {
358
- if ( value === undefined || value === '' ) return undefined // allow empty
357
+ type ParseResult < T > = { success : true ; data : T } | { success : false ; message : string }
358
+
359
+ const parseIcmpType = (
360
+ value : string | number | undefined
361
+ ) : ParseResult < number | undefined > => {
362
+ if ( value === undefined || value === '' ) return { success : true , data : undefined }
359
363
const parsed = typeof value === 'string' ? parseInt ( value , 10 ) : value
360
364
if ( isNaN ( parsed ) || parsed < 0 || parsed > 255 ) {
361
- return `ICMP type must be a number between 0 and 255`
365
+ return { success : false , message : `ICMP type must be a number between 0 and 255` }
362
366
}
363
- return undefined
367
+ return { success : true , data : parsed }
364
368
}
365
369
366
370
const icmpCodeValidation = ( value : string | undefined ) => {
@@ -418,23 +422,18 @@ const ProtocolFilters = ({ control }: { control: Control<FirewallRuleValues> })
418
422
if ( values . protocolType === 'tcp' || values . protocolType === 'udp' ) {
419
423
addProtocolIfUnique ( { type : values . protocolType } )
420
424
} else if ( values . protocolType === 'icmp' ) {
421
- if ( values . icmpType === undefined || values . icmpType === '' ) {
425
+ // this parse should never fail because we've already validated, but doing
426
+ // it this way keeps the just-in-case early return logic consistent
427
+ const parseResult = parseIcmpType ( values . icmpType )
428
+ if ( ! parseResult . success ) return
429
+
430
+ const icmpType = parseResult . data
431
+ if ( icmpType === undefined ) {
422
432
// All ICMP types
423
433
addProtocolIfUnique ( { type : 'icmp' , value : null } )
424
434
} else {
425
435
// Specific ICMP type
426
- const parsedIcmpType =
427
- typeof values . icmpType === 'string'
428
- ? parseInt ( values . icmpType , 10 )
429
- : values . icmpType
430
-
431
- if ( isNaN ( parsedIcmpType ) || parsedIcmpType < 0 || parsedIcmpType > 255 ) {
432
- return
433
- }
434
-
435
- const icmpValue : VpcFirewallIcmpFilter = {
436
- icmpType : parsedIcmpType ,
437
- }
436
+ const icmpValue : VpcFirewallIcmpFilter = { icmpType }
438
437
if ( values . icmpCode ) {
439
438
icmpValue . code = values . icmpCode
440
439
}
@@ -473,7 +472,10 @@ const ProtocolFilters = ({ control }: { control: Control<FirewallRuleValues> })
473
472
placeholder = ""
474
473
allowArbitraryValues
475
474
items = { icmpTypeItems }
476
- validate = { icmpTypeValidation }
475
+ validate = { ( value ) => {
476
+ const result = parseIcmpType ( value )
477
+ if ( ! result . success ) return result . message
478
+ } }
477
479
/>
478
480
479
481
{ selectedIcmpType !== undefined && selectedIcmpType !== '' && (
0 commit comments