Skip to content

Commit

Permalink
Migration to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
tibendadavis committed Jul 19, 2024
1 parent 67554d3 commit e96f5ce
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/shared/src/utils/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import i18n from "@dhis2/d2-i18n";

import { forIn, get, isEmpty } from "lodash";
import {
accessPageFields,
dataConfigurationPageFields,
generalPageFields,
highlightedIndicatorPageFields,
optionsPageFields,
REQUIRED_FIELDS,
} from "../constants";

function validateRequiredFields(scorecard: any, requiredFieldsPath = []) {
const errors: any = {};
forIn(scorecard, (value, key) => {
if (requiredFieldsPath.includes(key) && isEmpty(get(scorecard, key))) {
errors[`${key}`] = i18n.t("This field is required");
}
});
return errors;
}

export function validateGroups(dataGroups: any = []) {
const errors: any = {};
for (const group of dataGroups) {
if (isEmpty(group.dataHolders)) {
errors[group.id] = i18n.t(
"This group does not have any configured data sources",
);
}
}
return errors;
}

export default function validateScorecard(scorecard: any) {
return (
{
...validateRequiredFields(scorecard, REQUIRED_FIELDS),
...validateGroups(scorecard?.dataSelection?.dataGroups),
} ?? {}
);
}

export function getValidationPageFields(form: any, activePage: any) {
switch (activePage.id) {
case "general":
return generalPageFields;
case "dataConfiguration":
return dataConfigurationPageFields;
case "highlightedIndicator":
return highlightedIndicatorPageFields;
case "access":
return accessPageFields;
case "options":
return optionsPageFields;
default:
return [];
}
}

0 comments on commit e96f5ce

Please sign in to comment.