mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
relates to #19869 > NOTE: API integration will be included in a separate PR This adds the UI updates to support the new VPP feature on the software pages. This includes the software titles page and software titles details page. The new UI includes: **Add Vpp apps tab in Add software modal:**  **Various updates to the SoftwareIcon component to support icons from an external source:**  **Various updates to the SoftwarePackageCard compont to support app store apps.**  - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Added/updated testss: - [x] Manual QA for all new/changed functionality
161 lines
4.2 KiB
TypeScript
161 lines
4.2 KiB
TypeScript
import validator from "validator";
|
|
|
|
// @ts-ignore
|
|
import validateQuery from "components/forms/validators/validate_query";
|
|
|
|
import { IAddSoftwareFormData, IFormValidation } from "./AddSoftwareForm";
|
|
|
|
type IAddSoftwareFormValidatorKey = Exclude<
|
|
keyof IAddSoftwareFormData,
|
|
"installScript"
|
|
>;
|
|
|
|
type IMessageFunc = (formData: IAddSoftwareFormData) => string;
|
|
type IValidationMessage = string | IMessageFunc;
|
|
|
|
interface IValidation {
|
|
name: string;
|
|
isValid: (
|
|
formData: IAddSoftwareFormData,
|
|
enabledPreInstallCondition?: boolean,
|
|
enabledPostInstallScript?: boolean
|
|
) => boolean;
|
|
message?: IValidationMessage;
|
|
}
|
|
|
|
/** configuration defines validations for each filed in the form. It defines rules
|
|
* to determine if a field is valid, and rules for generating an error message.
|
|
*/
|
|
const FORM_VALIDATION_CONFIG: Record<
|
|
IAddSoftwareFormValidatorKey,
|
|
{ validations: IValidation[] }
|
|
> = {
|
|
software: {
|
|
validations: [
|
|
{
|
|
name: "required",
|
|
isValid: (formData) => formData.software !== null,
|
|
},
|
|
],
|
|
},
|
|
preInstallCondition: {
|
|
validations: [
|
|
{
|
|
name: "required",
|
|
isValid: (
|
|
formData: IAddSoftwareFormData,
|
|
enabledPreInstallCondition
|
|
) => {
|
|
if (!enabledPreInstallCondition) {
|
|
return true;
|
|
}
|
|
return (
|
|
formData.preInstallCondition !== undefined &&
|
|
!validator.isEmpty(formData.preInstallCondition)
|
|
);
|
|
},
|
|
message: (formData) => {
|
|
// we dont want an error message until the user has interacted with
|
|
// the field. This is why we check for undefined here.
|
|
if (formData.preInstallCondition === undefined) {
|
|
return "";
|
|
}
|
|
return "Pre-install condition is required when enabled.";
|
|
},
|
|
},
|
|
{
|
|
name: "invalidQuery",
|
|
isValid: (formData, enabledPreInstallCondition) => {
|
|
if (!enabledPreInstallCondition) {
|
|
return true;
|
|
}
|
|
return (
|
|
formData.preInstallCondition !== undefined &&
|
|
validateQuery(formData.preInstallCondition).valid
|
|
);
|
|
},
|
|
message: (formData) =>
|
|
validateQuery(formData.preInstallCondition).error,
|
|
},
|
|
],
|
|
},
|
|
postInstallScript: {
|
|
validations: [
|
|
{
|
|
name: "required",
|
|
message: (formData) => {
|
|
// we dont want an error message until the user has interacted with
|
|
// the field. This is why we check for undefined here.
|
|
if (formData.postInstallScript === undefined) {
|
|
return "";
|
|
}
|
|
return "Post-install script is required when enabled.";
|
|
},
|
|
isValid: (formData, _, enabledPostInstallScript) => {
|
|
if (!enabledPostInstallScript) {
|
|
return true;
|
|
}
|
|
return (
|
|
formData.postInstallScript !== undefined &&
|
|
!validator.isEmpty(formData.postInstallScript)
|
|
);
|
|
},
|
|
},
|
|
],
|
|
},
|
|
selfService: {
|
|
// no validations related to self service
|
|
validations: [],
|
|
},
|
|
};
|
|
|
|
const getErrorMessage = (
|
|
formData: IAddSoftwareFormData,
|
|
message?: IValidationMessage
|
|
) => {
|
|
if (message === undefined || typeof message === "string") {
|
|
return message;
|
|
}
|
|
return message(formData);
|
|
};
|
|
|
|
export const generateFormValidation = (
|
|
formData: IAddSoftwareFormData,
|
|
showingPreInstallCondition: boolean,
|
|
showingPostInstallScript: boolean
|
|
) => {
|
|
const formValidation: IFormValidation = {
|
|
isValid: true,
|
|
software: {
|
|
isValid: false,
|
|
},
|
|
};
|
|
|
|
Object.keys(FORM_VALIDATION_CONFIG).forEach((key) => {
|
|
const objKey = key as keyof typeof FORM_VALIDATION_CONFIG;
|
|
const failedValidation = FORM_VALIDATION_CONFIG[objKey].validations.find(
|
|
(validation) =>
|
|
!validation.isValid(
|
|
formData,
|
|
showingPreInstallCondition,
|
|
showingPostInstallScript
|
|
)
|
|
);
|
|
|
|
if (!failedValidation) {
|
|
formValidation[objKey] = {
|
|
isValid: true,
|
|
};
|
|
} else {
|
|
formValidation.isValid = false;
|
|
formValidation[objKey] = {
|
|
isValid: false,
|
|
message: getErrorMessage(formData, failedValidation.message),
|
|
};
|
|
}
|
|
});
|
|
|
|
return formValidation;
|
|
};
|
|
|
|
export default generateFormValidation;
|