@@ -35,15 +35,21 @@ impl ValidationError {
3535
3636// ── Validation rules ──────────────────────────────────────────────────────────
3737
38- /// Validate a contract ID: must be a 56-character alphanumeric Stellar address.
38+ /// Required length of a Stellar contract ID (a base32-style strkey/address string).
39+ const STELLAR_CONTRACT_ID_LEN : usize = 56 ;
40+
41+ /// Maximum allowed length of a route name.
42+ const MAX_ROUTE_NAME_LEN : usize = 64 ;
43+
44+ /// Validate a contract ID: must be a `STELLAR_CONTRACT_ID_LEN`-character alphanumeric Stellar address.
3945pub fn validate_contract_id ( id : & str ) -> Result < ( ) , ValidationError > {
4046 if id. is_empty ( ) {
4147 return Err ( ValidationError :: new ( "contract_id must not be empty" ) ) ;
4248 }
43- if id. len ( ) != 56 {
44- return Err ( ValidationError :: new (
45- "contract_id must be exactly 56 characters" ,
46- ) ) ;
49+ if id. len ( ) != STELLAR_CONTRACT_ID_LEN {
50+ return Err ( ValidationError :: new ( format ! (
51+ "contract_id must be exactly {STELLAR_CONTRACT_ID_LEN} characters"
52+ ) ) ) ;
4753 }
4854 if !id. chars ( ) . all ( |c| c. is_ascii_alphanumeric ( ) ) {
4955 return Err ( ValidationError :: new (
@@ -53,7 +59,7 @@ pub fn validate_contract_id(id: &str) -> Result<(), ValidationError> {
5359 Ok ( ( ) )
5460}
5561
56- /// Validate a route name: non-empty, max 64 chars, alphanumeric + underscore/hyphen.
62+ /// Validate a route name: non-empty, max `MAX_ROUTE_NAME_LEN` chars, alphanumeric + underscore/hyphen.
5763///
5864/// Not yet called from any handler — kept for validating route-name query
5965/// params once an endpoint accepts one.
@@ -62,10 +68,10 @@ pub fn validate_route_name(name: &str) -> Result<(), ValidationError> {
6268 if name. is_empty ( ) {
6369 return Err ( ValidationError :: new ( "route name must not be empty" ) ) ;
6470 }
65- if name. len ( ) > 64 {
66- return Err ( ValidationError :: new (
67- "route name must be 64 characters or fewer" ,
68- ) ) ;
71+ if name. len ( ) > MAX_ROUTE_NAME_LEN {
72+ return Err ( ValidationError :: new ( format ! (
73+ "route name must be {MAX_ROUTE_NAME_LEN} characters or fewer"
74+ ) ) ) ;
6975 }
7076 if !name
7177 . chars ( )
@@ -108,7 +114,7 @@ mod tests {
108114
109115 #[ test]
110116 fn valid_contract_id ( ) {
111- let id = "A" . repeat ( 56 ) ;
117+ let id = "A" . repeat ( STELLAR_CONTRACT_ID_LEN ) ;
112118 assert ! ( validate_contract_id( & id) . is_ok( ) ) ;
113119 }
114120
@@ -124,7 +130,7 @@ mod tests {
124130
125131 #[ test]
126132 fn contract_id_with_special_chars_rejected ( ) {
127- let id = format ! ( "{}!" , "A" . repeat( 55 ) ) ;
133+ let id = format ! ( "{}!" , "A" . repeat( STELLAR_CONTRACT_ID_LEN - 1 ) ) ;
128134 assert ! ( validate_contract_id( & id) . is_err( ) ) ;
129135 }
130136
@@ -140,7 +146,7 @@ mod tests {
140146
141147 #[ test]
142148 fn long_route_name_rejected ( ) {
143- assert ! ( validate_route_name( & "a" . repeat( 65 ) ) . is_err( ) ) ;
149+ assert ! ( validate_route_name( & "a" . repeat( MAX_ROUTE_NAME_LEN + 1 ) ) . is_err( ) ) ;
144150 }
145151
146152 #[ test]
0 commit comments