@@ -7,6 +7,12 @@ import i18n from './i18n';
77
88const MIN_ONE_MESSAGE = i18n ( 'error_min-number' , { count : 1 } ) ;
99const MAX_HUNDRED_MESSAGE = i18n ( 'error_max-number' , { count : 100 } ) ;
10+ const REQUIRED_MESSAGE = i18n ( 'error_required' ) ;
11+ const NUMBER_MESSAGE = i18n ( 'error_number' ) ;
12+
13+ const getRequiredNumberError = ( issue : { input ?: unknown } ) => {
14+ return issue . input === undefined ? REQUIRED_MESSAGE : NUMBER_MESSAGE ;
15+ } ;
1016
1117const addIssue = ( ctx : z . RefinementCtx , path : Array < string | number > , message : string ) => {
1218 ctx . addIssue ( {
@@ -21,8 +27,7 @@ const requiredNumber = (schema?: z.ZodNumber) =>
2127 ( val ) => ( typeof val === 'number' && Number . isNaN ( val ) ? undefined : val ) ,
2228 schema ??
2329 z . number ( {
24- required_error : i18n ( 'error_required' ) ,
25- invalid_type_error : i18n ( 'error_number' ) ,
30+ error : getRequiredNumberError ,
2631 } ) ,
2732 ) ;
2833
@@ -32,14 +37,14 @@ const optionalNumber = (schema?: z.ZodNumber) =>
3237 (
3338 schema ??
3439 z . number ( {
35- invalid_type_error : i18n ( 'error_number' ) ,
40+ error : NUMBER_MESSAGE ,
3641 } )
3742 ) . optional ( ) ,
3843 ) ;
3944
4045const topicNameSchema = z
41- . string ( { required_error : i18n ( 'error_required' ) , invalid_type_error : i18n ( 'error_required' ) } )
42- . min ( 1 , i18n ( 'error_required' ) )
46+ . string ( { error : REQUIRED_MESSAGE } )
47+ . min ( 1 , REQUIRED_MESSAGE )
4348 . superRefine ( ( value , ctx ) => {
4449 if ( ! isValidEntityPath ( value ) ) {
4550 addIssue ( ctx , [ ] , i18n ( 'error_name-regex' ) ) ;
@@ -67,26 +72,23 @@ export function getTopicFormValidationSchema(minPartitions: number) {
6772 shards : requiredNumber (
6873 z
6974 . number ( {
70- required_error : i18n ( 'error_required' ) ,
71- invalid_type_error : i18n ( 'error_number' ) ,
75+ error : getRequiredNumberError ,
7276 } )
7377 . min ( 1 , MIN_ONE_MESSAGE ) ,
7478 ) ,
7579 writeQuotaBytes : requiredNumber ( ) ,
7680 autoPartitioning : z . object ( {
7781 enabled : z . boolean ( ) ,
78- mode : z . string ( ) . min ( 1 , i18n ( 'error_required' ) ) ,
82+ mode : z . string ( { error : REQUIRED_MESSAGE } ) . min ( 1 , REQUIRED_MESSAGE ) ,
7983 minPartitions : optionalNumber (
80- z . number ( { invalid_type_error : i18n ( 'error_number' ) } ) . min ( 1 , MIN_ONE_MESSAGE ) ,
84+ z . number ( { error : NUMBER_MESSAGE } ) . min ( 1 , MIN_ONE_MESSAGE ) ,
8185 ) ,
8286 maxPartitions : optionalNumber (
83- z . number ( { invalid_type_error : i18n ( 'error_number' ) } ) . min ( 1 , MIN_ONE_MESSAGE ) ,
87+ z . number ( { error : NUMBER_MESSAGE } ) . min ( 1 , MIN_ONE_MESSAGE ) ,
8488 ) ,
8589 stabilizationWindow : optionalNumber ( ) ,
8690 upUtilization : optionalNumber (
87- z
88- . number ( { invalid_type_error : i18n ( 'error_number' ) } )
89- . max ( 100 , MAX_HUNDRED_MESSAGE ) ,
91+ z . number ( { error : NUMBER_MESSAGE } ) . max ( 100 , MAX_HUNDRED_MESSAGE ) ,
9092 ) ,
9193 } ) ,
9294 } )
@@ -128,5 +130,5 @@ export function getTopicFormValidationSchema(minPartitions: number) {
128130 validateRequiredNumber ( ctx , stabilizationPath , autoPartitioning . stabilizationWindow ) ;
129131
130132 validateRequiredNumber ( ctx , upUtilizationPath , autoPartitioning . upUtilization ) ;
131- } ) as z . ZodType < TopicFormValues > ;
133+ } ) as z . ZodType < TopicFormValues , TopicFormValues > ;
132134}
0 commit comments