@@ -14,17 +14,31 @@ const g_lib = require("../support");
1414 * Rust emphasizes explicit error handling through Result types 
1515 */ 
1616
17+ // Validate that a value is a non-empty string 
18+ // Reusable helper following DRY principle 
19+ const  validateNonEmptyString  =  ( value ,  fieldName )  =>  { 
20+     if  ( ! value  ||  typeof  value  !==  "string"  ||  value . trim ( )  ===  "" )  { 
21+         return  Result . err ( { 
22+             code : g_lib . ERR_INVALID_PARAM , 
23+             message : `${ fieldName }  , 
24+         } ) ; 
25+     } 
26+     return  Result . ok ( true ) ; 
27+ } ; 
28+ 
1729// Validate common repository fields 
1830// Pure function - no side effects, deterministic output 
1931const  validateCommonFields  =  ( config )  =>  { 
2032    const  errors  =  [ ] ; 
2133
22-     if  ( ! config . id  ||  typeof  config . id  !==  "string"  ||  config . id . trim ( )  ===  "" )  { 
23-         errors . push ( "Repository ID is required and must be a non-empty string" ) ; 
34+     const  idValidation  =  validateNonEmptyString ( config . id ,  "Repository ID" ) ; 
35+     if  ( ! idValidation . ok )  { 
36+         errors . push ( idValidation . error . message ) ; 
2437    } 
2538
26-     if  ( ! config . title  ||  typeof  config . title  !==  "string"  ||  config . title . trim ( )  ===  "" )  { 
27-         errors . push ( "Repository title is required and must be a non-empty string" ) ; 
39+     const  titleValidation  =  validateNonEmptyString ( config . title ,  "Repository title" ) ; 
40+     if  ( ! titleValidation . ok )  { 
41+         errors . push ( titleValidation . error . message ) ; 
2842    } 
2943
3044    if  ( typeof  config . capacity  !==  "number"  ||  config . capacity  <=  0 )  { 
@@ -86,7 +100,7 @@ const validateRepositoryPath = (path, repoId) => {
86100
87101    // Extract last component 
88102    const  idx  =  normalizedPath . lastIndexOf ( "/" ,  normalizedPath . length  -  2 ) ; 
89-     const  lastComponent  =  normalizedPath . substr ( idx  +  1 ,  normalizedPath . length  -  idx   -   2 ) ; 
103+     const  lastComponent  =  normalizedPath . slice ( idx  +  1 ,  normalizedPath . length  -  1 ) ; 
90104
91105    if  ( lastComponent  !==  repoId )  { 
92106        return  Result . err ( { 
@@ -108,20 +122,24 @@ const validateGlobusConfig = (config) => {
108122    const  errors  =  [ ] ; 
109123
110124    // Validate required Globus fields 
111-     if  ( ! config . pub_key  ||  typeof  config . pub_key  !==  "string" )  { 
112-         errors . push ( "Public key is required for Globus repositories" ) ; 
125+     const  pubKeyValidation  =  validateNonEmptyString ( config . pub_key ,  "Public key" ) ; 
126+     if  ( ! pubKeyValidation . ok )  { 
127+         errors . push ( pubKeyValidation . error . message ) ; 
113128    } 
114129
115-     if  ( ! config . address  ||  typeof  config . address  !==  "string" )  { 
116-         errors . push ( "Address is required for Globus repositories" ) ; 
130+     const  addressValidation  =  validateNonEmptyString ( config . address ,  "Address" ) ; 
131+     if  ( ! addressValidation . ok )  { 
132+         errors . push ( addressValidation . error . message ) ; 
117133    } 
118134
119-     if  ( ! config . endpoint  ||  typeof  config . endpoint  !==  "string" )  { 
120-         errors . push ( "Endpoint is required for Globus repositories" ) ; 
135+     const  endpointValidation  =  validateNonEmptyString ( config . endpoint ,  "Endpoint" ) ; 
136+     if  ( ! endpointValidation . ok )  { 
137+         errors . push ( endpointValidation . error . message ) ; 
121138    } 
122139
123-     if  ( ! config . domain  ||  typeof  config . domain  !==  "string" )  { 
124-         errors . push ( "Domain is required for Globus repositories" ) ; 
140+     const  domainValidation  =  validateNonEmptyString ( config . domain ,  "Domain" ) ; 
141+     if  ( ! domainValidation . ok )  { 
142+         errors . push ( domainValidation . error . message ) ; 
125143    } 
126144
127145    if  ( errors . length  >  0 )  { 
@@ -174,8 +192,9 @@ const validateMetadataConfig = (config) => {
174192const  validateAllocationParams  =  ( params )  =>  { 
175193    const  errors  =  [ ] ; 
176194
177-     if  ( ! params . subject  ||  typeof  params . subject  !==  "string" )  { 
178-         errors . push ( "Allocation subject is required" ) ; 
195+     const  subjectValidation  =  validateNonEmptyString ( params . subject ,  "Allocation subject" ) ; 
196+     if  ( ! subjectValidation . ok )  { 
197+         errors . push ( subjectValidation . error . message ) ; 
179198    } 
180199
181200    if  ( typeof  params . size  !==  "number"  ||  params . size  <=  0 )  { 
@@ -197,6 +216,7 @@ const validateAllocationParams = (params) => {
197216} ; 
198217
199218module . exports  =  { 
219+     validateNonEmptyString, 
200220    validateCommonFields, 
201221    validatePOSIXPath, 
202222    validateRepositoryPath, 
0 commit comments