-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom error validation to series (#595)
- Loading branch information
1 parent
47e29b8
commit 651cb08
Showing
4 changed files
with
184 additions
and
95 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
64 changes: 48 additions & 16 deletions
64
app/src/js/applications/operations/series/edition/validation.js
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 |
---|---|---|
@@ -1,30 +1,62 @@ | ||
import D, { D1, D2 } from 'js/i18n'; | ||
|
||
export function validate({ creators, prefLabelLg1, prefLabelLg2, family }) { | ||
const errorMessages = []; | ||
export const listOfExtraMandatoryFields = ( | ||
process.env.REACT_APP_VALIDATION_OPERATION_SERIES_EXTRA_MANDATORY_FIELDS ?? '' | ||
).split(','); | ||
|
||
if(!creators || creators.length === 0){ | ||
errorMessages.push(D.mandatoryProperty(D1.creatorTitle)) | ||
const fieldToTitleMapping = { | ||
creators: D1.creatorTitle, | ||
prefLabelLg1: D1.title, | ||
prefLabelLg2: D2.title, | ||
family: D1.familyTitle, | ||
typeCode: D1.operationType, | ||
accrualPeriodicityCode: D1.dataCollectFrequency, | ||
}; | ||
|
||
export const isMandatoryField = (fieldName) => | ||
listOfExtraMandatoryFields.indexOf(fieldName) >= 0; | ||
|
||
export function validate({ | ||
creators, | ||
prefLabelLg1, | ||
prefLabelLg2, | ||
family, | ||
...otherFields | ||
}) { | ||
const fields = {}; | ||
const errorMessage = []; | ||
|
||
if (!creators || creators.length === 0) { | ||
errorMessage.push(D.mandatoryProperty(fieldToTitleMapping.creators)); | ||
fields.creators = D.mandatoryProperty(fieldToTitleMapping.creators); | ||
} | ||
|
||
if(!prefLabelLg1){ | ||
errorMessages.push(D.mandatoryProperty(D1.title)) | ||
if (!prefLabelLg1) { | ||
errorMessage.push(D.mandatoryProperty(fieldToTitleMapping.prefLabelLg1)); | ||
fields.prefLabelLg1 = D.mandatoryProperty(fieldToTitleMapping.prefLabelLg1); | ||
} | ||
if(!prefLabelLg2){ | ||
errorMessages.push(D.mandatoryProperty(D2.title)) | ||
if (!prefLabelLg2) { | ||
errorMessage.push(D.mandatoryProperty(fieldToTitleMapping.prefLabelLg2)); | ||
fields.prefLabelLg2 = D.mandatoryProperty(fieldToTitleMapping.prefLabelLg2); | ||
} | ||
|
||
if (!family) { | ||
errorMessages.push(D.mandatoryProperty(D1.familyTitle)) | ||
errorMessage.push(D.mandatoryProperty(fieldToTitleMapping.family)); | ||
fields.family = D.mandatoryProperty(fieldToTitleMapping.family); | ||
} | ||
|
||
listOfExtraMandatoryFields.forEach((extraMandatoryField) => { | ||
if (!otherFields[extraMandatoryField]) { | ||
errorMessage.push( | ||
D.mandatoryProperty(fieldToTitleMapping[extraMandatoryField] ?? '') | ||
); | ||
fields[extraMandatoryField] = D.mandatoryProperty( | ||
fieldToTitleMapping[extraMandatoryField] ?? '' | ||
); | ||
} | ||
}); | ||
return { | ||
fields: { | ||
creators: (!creators || creators.length === 0) ? D.mandatoryProperty(D1.creatorTitle) : '', | ||
prefLabelLg1: !prefLabelLg1 ? D.mandatoryProperty(D1.title) : '', | ||
prefLabelLg2: !prefLabelLg2 ? D.mandatoryProperty(D2.title) : '', | ||
family: !family ? D.mandatoryProperty(D1.familyTitle) : '', | ||
}, | ||
errorMessage: errorMessages, | ||
fields, | ||
errorMessage, | ||
}; | ||
} |
Oops, something went wrong.