-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67554d3
commit e96f5ce
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |