feature/IO-3701-Harness-Replacement - Implement
This commit is contained in:
44
server/feature-flags/admin-payload.js
Normal file
44
server/feature-flags/admin-payload.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const CREATE_FIELDS = ["name", "description", "default_treatment", "active"];
|
||||
const UPDATE_FIELDS = ["description", "default_treatment", "active"];
|
||||
|
||||
const hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
||||
|
||||
/**
|
||||
* Trims stable feature flag identity fields while leaving free-form text unchanged.
|
||||
*/
|
||||
const normalizeStringField = (key, value) => {
|
||||
if (value == null) return value;
|
||||
if (key !== "name" && key !== "default_treatment") return value;
|
||||
return typeof value === "string" ? value.trim() : value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whitelists fields accepted by feature flag definition create/update admin endpoints.
|
||||
*/
|
||||
const pickFeatureFlagFields = (input, allowedFields) => {
|
||||
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return allowedFields.reduce((payload, field) => {
|
||||
if (hasOwn(input, field)) {
|
||||
payload[field] = normalizeStringField(field, input[field]);
|
||||
}
|
||||
return payload;
|
||||
}, {});
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds a safe payload for creating a feature flag definition.
|
||||
*/
|
||||
const sanitizeFeatureFlagCreatePayload = (input) => pickFeatureFlagFields(input, CREATE_FIELDS);
|
||||
|
||||
/**
|
||||
* Builds a safe payload for updating editable feature flag definition fields.
|
||||
*/
|
||||
const sanitizeFeatureFlagUpdatePayload = (input) => pickFeatureFlagFields(input, UPDATE_FIELDS);
|
||||
|
||||
module.exports = {
|
||||
sanitizeFeatureFlagCreatePayload,
|
||||
sanitizeFeatureFlagUpdatePayload
|
||||
};
|
||||
Reference in New Issue
Block a user