Skip to content

Commit 77a982e

Browse files
committed
[DAPS-1531] Address Object -> object, address copilot comments
1 parent 2243fa3 commit 77a982e

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

core/database/foxx/api/repo_router_new.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ router
112112
g_db.admin.save({ _from: saved._id, _to: admin }),
113113
);
114114

115-
res.send([cleanRepoData(saved)]);
115+
res.send(cleanRepoData(saved));
116116
},
117117
});
118118
} catch (e) {

core/database/foxx/api/repository/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @type {Readonly<{GLOBUS: string, METADATA_ONLY: string}>}
1313
* @see https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html
1414
*/
15-
const RepositoryType = object.freeze({
15+
const RepositoryType = Object.freeze({
1616
GLOBUS: "globus",
1717
METADATA_ONLY: "metadata_only",
1818
});
@@ -102,7 +102,7 @@ const createRepository = (type, data) => ({
102102
* Another enum-like constant representing different execution strategies
103103
* @type {Readonly<{TASK: string, DIRECT: string}>}
104104
*/
105-
const ExecutionMethod = object.freeze({
105+
const ExecutionMethod = Object.freeze({
106106
TASK: "task",
107107
DIRECT: "direct",
108108
});

core/database/foxx/api/repository/validation.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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} is required and must be a non-empty string`,
24+
});
25+
}
26+
return Result.ok(true);
27+
};
28+
1729
// Validate common repository fields
1830
// Pure function - no side effects, deterministic output
1931
const 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) => {
174192
const 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

199218
module.exports = {
219+
validateNonEmptyString,
200220
validateCommonFields,
201221
validatePOSIXPath,
202222
validateRepositoryPath,

0 commit comments

Comments
 (0)