fleet/frontend/pages/ManageControlsPage/Variables/components/AddCustomVariableModal/helpers.ts

122 lines
3.3 KiB
TypeScript
Raw Normal View History

import { IAddCustomVariableFormData } from "./AddCustomVariableModal";
// TODO: create a validator abstraction for this and the other form validation files
export interface IAddCustomVariableFormValidation {
isValid: boolean;
name?: { isValid: boolean; message?: string };
value?: { isValid: boolean; message?: string };
}
type IMessageFunc = (formData: IAddCustomVariableFormData) => string;
type IValidationMessage = string | IMessageFunc;
type IFormValidationKey = keyof Omit<IAddCustomVariableFormData, "isValid">;
interface IValidation {
name: string;
isValid: (
formData: IAddCustomVariableFormData,
validations?: IAddCustomVariableFormValidation
) => boolean;
message?: IValidationMessage;
}
type IFormValidations = Record<
IFormValidationKey,
{ validations: IValidation[] }
>;
const FORM_VALIDATIONS: IFormValidations = {
name: {
validations: [
{
name: "required",
isValid: (formData: IAddCustomVariableFormData) => {
return formData.name.length > 0;
},
message: `Name is required`,
},
{
name: "validName",
isValid: (formData: IAddCustomVariableFormData) => {
if (formData.name.length === 0) {
return true; // Skip this validation if name is empty
}
return !!formData.name.match(/^[a-zA-Z0-9_]+$/);
},
message:
"Name may only include uppercase letters, numbers, and underscores",
},
Custom Variables UI updates (#32304) for #32269 for #32271 for #32285 # Details This PR addresses some front-end issues found during QA testing of the new Custom Variables UI # Checklist for submitter ## Testing - [X] Added/updated automated tests - [ ] QA'd all new/changed functionality manually * Spaces allowed in custom variable values: <img width="659" height="399" alt="image" src="https://github.com/user-attachments/assets/e849636f-d91c-4649-a6f1-82bc2397cbbe" /> * Overflow in add var modal <img width="657" height="412" alt="image" src="https://github.com/user-attachments/assets/d04563e1-df29-457d-976a-b6b05b3aa623" /> * If it's too long, show error <img width="664" height="414" alt="image" src="https://github.com/user-attachments/assets/91100d0c-35b5-4cd2-a779-6a34bfdc9fc3" /> * Overflow in table too <img width="1031" height="178" alt="image" src="https://github.com/user-attachments/assets/4833bd64-2f3f-4de7-9392-1955341a5a96" /> * Added "Learn More" button and correct margin between tabs and description <img width="1053" height="224" alt="image" src="https://github.com/user-attachments/assets/4db5116c-0a77-4427-ad61-9bde6e94679b" /> * Team admins / maintainers can no longer see "Add" or "Delete" var buttons <img width="1031" height="374" alt="image" src="https://github.com/user-attachments/assets/a15ab358-a6de-4d0b-9df9-aa2e7ed5c0cb" /> For unreleased bug fixes in a release candidate, one of: - [X] Confirmed that the fix is not expected to adversely impact load test results - [ ] Alerted the release DRI if additional load testing is needed
2025-08-26 19:08:15 +00:00
{
name: "notTooLong",
isValid: (formData: IAddCustomVariableFormData) => {
return formData.name.length <= 255;
Custom Variables UI updates (#32304) for #32269 for #32271 for #32285 # Details This PR addresses some front-end issues found during QA testing of the new Custom Variables UI # Checklist for submitter ## Testing - [X] Added/updated automated tests - [ ] QA'd all new/changed functionality manually * Spaces allowed in custom variable values: <img width="659" height="399" alt="image" src="https://github.com/user-attachments/assets/e849636f-d91c-4649-a6f1-82bc2397cbbe" /> * Overflow in add var modal <img width="657" height="412" alt="image" src="https://github.com/user-attachments/assets/d04563e1-df29-457d-976a-b6b05b3aa623" /> * If it's too long, show error <img width="664" height="414" alt="image" src="https://github.com/user-attachments/assets/91100d0c-35b5-4cd2-a779-6a34bfdc9fc3" /> * Overflow in table too <img width="1031" height="178" alt="image" src="https://github.com/user-attachments/assets/4833bd64-2f3f-4de7-9392-1955341a5a96" /> * Added "Learn More" button and correct margin between tabs and description <img width="1053" height="224" alt="image" src="https://github.com/user-attachments/assets/4db5116c-0a77-4427-ad61-9bde6e94679b" /> * Team admins / maintainers can no longer see "Add" or "Delete" var buttons <img width="1031" height="374" alt="image" src="https://github.com/user-attachments/assets/a15ab358-a6de-4d0b-9df9-aa2e7ed5c0cb" /> For unreleased bug fixes in a release candidate, one of: - [X] Confirmed that the fix is not expected to adversely impact load test results - [ ] Alerted the release DRI if additional load testing is needed
2025-08-26 19:08:15 +00:00
},
message: "Name may not exceed 255 characters",
Custom Variables UI updates (#32304) for #32269 for #32271 for #32285 # Details This PR addresses some front-end issues found during QA testing of the new Custom Variables UI # Checklist for submitter ## Testing - [X] Added/updated automated tests - [ ] QA'd all new/changed functionality manually * Spaces allowed in custom variable values: <img width="659" height="399" alt="image" src="https://github.com/user-attachments/assets/e849636f-d91c-4649-a6f1-82bc2397cbbe" /> * Overflow in add var modal <img width="657" height="412" alt="image" src="https://github.com/user-attachments/assets/d04563e1-df29-457d-976a-b6b05b3aa623" /> * If it's too long, show error <img width="664" height="414" alt="image" src="https://github.com/user-attachments/assets/91100d0c-35b5-4cd2-a779-6a34bfdc9fc3" /> * Overflow in table too <img width="1031" height="178" alt="image" src="https://github.com/user-attachments/assets/4833bd64-2f3f-4de7-9392-1955341a5a96" /> * Added "Learn More" button and correct margin between tabs and description <img width="1053" height="224" alt="image" src="https://github.com/user-attachments/assets/4db5116c-0a77-4427-ad61-9bde6e94679b" /> * Team admins / maintainers can no longer see "Add" or "Delete" var buttons <img width="1031" height="374" alt="image" src="https://github.com/user-attachments/assets/a15ab358-a6de-4d0b-9df9-aa2e7ed5c0cb" /> For unreleased bug fixes in a release candidate, one of: - [X] Confirmed that the fix is not expected to adversely impact load test results - [ ] Alerted the release DRI if additional load testing is needed
2025-08-26 19:08:15 +00:00
},
{
name: "doesNotIncludePrefix",
isValid: (formData: IAddCustomVariableFormData) => {
return !formData.name.match(/^FLEET_SECRET_/);
},
message: `Name should not include variable prefix`,
},
],
},
value: {
validations: [
{
name: "required",
isValid: (formData: IAddCustomVariableFormData) => {
return formData.value.length > 0;
},
message: `Value is required`,
},
],
},
};
const getErrorMessage = (
formData: IAddCustomVariableFormData,
message?: IValidationMessage
) => {
if (message === undefined || typeof message === "string") {
return message;
}
return message(formData);
};
export const validateFormData = (
formData: IAddCustomVariableFormData,
isSaving = false
) => {
const formValidation: IAddCustomVariableFormValidation = {
isValid: true,
};
Object.keys(FORM_VALIDATIONS).forEach((key) => {
const objKey = key as keyof typeof FORM_VALIDATIONS;
const failedValidation = FORM_VALIDATIONS[objKey].validations.find(
(validation) => {
if (!isSaving && validation.name === "required") {
return false; // Skip this validation if not saving
}
return !validation.isValid(formData, formValidation);
}
);
if (!failedValidation) {
formValidation[objKey] = {
isValid: true,
};
} else {
formValidation.isValid = false;
formValidation[objKey] = {
isValid: false,
message: getErrorMessage(formData, failedValidation.message),
};
}
});
return formValidation;
};