@@ -9,6 +9,11 @@ const DECIMAL_REGEX = /^\d+(\.(\d{1,2}))?$/
99const INTEGER_REGEX = / ^ \d + $ /
1010const SIGNED_INTEGER_REGEX = / ^ - ? \d + $ /
1111
12+ // Joi error type constants
13+ const ERROR_STRING_PATTERN_BASE = 'string.pattern.base'
14+ const ERROR_STRING_MAX = 'string.max'
15+ const ERROR_STRING_WHOLE_NUMBER_MAX = 'string.whole_number_max'
16+
1217/**
1318 * Validates a carbon decimal field value (up to 2 decimal places).
1419 * Used for tCO₂ fields: build, operation, sequestered, avoided.
@@ -17,17 +22,17 @@ const SIGNED_INTEGER_REGEX = /^-?\d+$/
1722 */
1823const validateCarbonDecimalString = ( value , helpers ) => {
1924 if ( ! DECIMAL_REGEX . test ( value ) ) {
20- return helpers . error ( 'string.pattern.base' )
25+ return helpers . error ( ERROR_STRING_PATTERN_BASE )
2126 }
2227 const [ intPart , decPart ] = value . split ( '.' )
2328 if ( decPart === undefined ) {
2429 // Whole number: max 18 digits
2530 if ( intPart . length > MAX_WHOLE_NUMBER_DIGITS ) {
26- return helpers . error ( 'string.whole_number_max' )
31+ return helpers . error ( ERROR_STRING_WHOLE_NUMBER_MAX )
2732 }
2833 } else if ( intPart . length > MAX_EMISSION_DIGITS ) {
2934 // Decimal: max 16 digits before decimal point
30- return helpers . error ( 'string.max' )
35+ return helpers . error ( ERROR_STRING_MAX )
3136 } else {
3237 // no error
3338 }
@@ -40,10 +45,10 @@ const validateCarbonDecimalString = (value, helpers) => {
4045 */
4146const validateCarbonIntegerString = ( value , helpers ) => {
4247 if ( ! INTEGER_REGEX . test ( value ) ) {
43- return helpers . error ( 'string.pattern.base' )
48+ return helpers . error ( ERROR_STRING_PATTERN_BASE )
4449 }
4550 if ( value . length > MAX_COST_DIGITS ) {
46- return helpers . error ( 'string.max' )
51+ return helpers . error ( ERROR_STRING_MAX )
4752 }
4853 return value
4954}
@@ -55,12 +60,12 @@ const validateCarbonIntegerString = (value, helpers) => {
5560 */
5661const validateCarbonSignedIntegerString = ( value , helpers ) => {
5762 if ( ! SIGNED_INTEGER_REGEX . test ( value ) ) {
58- return helpers . error ( 'string.pattern.base' )
63+ return helpers . error ( ERROR_STRING_PATTERN_BASE )
5964 }
6065 // Count digits excluding minus sign
6166 const digits = value . replace ( / ^ - / , '' )
6267 if ( digits . length > MAX_COST_DIGITS ) {
63- return helpers . error ( 'string.max' )
68+ return helpers . error ( ERROR_STRING_MAX )
6469 }
6570 return value
6671}
0 commit comments