Skip to content

Commit

Permalink
fix extra mandatory fields
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreVasseur committed Feb 12, 2024
1 parent a2df018 commit a469dea
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions app/src/js/applications/operations/series/edition/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ import { formatValidation } from 'js/utils/validation';
import { z } from 'zod';

const listOfExtraMandatoryFields = (process.env.REACT_APP_VALIDATION_OPERATION_SERIES_EXTRA_MANDATORY_FIELDS ?? "").split(',');

const fieldToTitleMapping = {
typeCode: D1.operationType,
accrualPeriodicityCode: D1.dataCollectFrequency
};

export const isMandatoryField = fieldName => listOfExtraMandatoryFields.indexOf(fieldName) >= 0;

const Serie = z.object({
let Serie = z.object({
prefLabelLg1: z.string().min(1, {message: D.mandatoryProperty(D1.title)}),
prefLabelLg2: z.string().min(1, {message: D.mandatoryProperty(D2.title)}),
creators: z.string({
required_error: D.mandatoryProperty(D.creatorTitle)
}).array().nonempty({
message: D.mandatoryProperty(D.creatorTitle)
}),
typeCode: z.string({
required_error: D.mandatoryProperty(D1.operationType)
}).min(1, {
message: D.mandatoryProperty(D1.operationType)
}),
accrualPeriodicityCode: z.string({
required_error: D.mandatoryProperty(D1.dataCollectFrequency)
}).min(1, {
message: D.mandatoryProperty(D1.dataCollectFrequency)
}),
family: z.object({
id: z.string(),
}, {
required_error: D.mandatoryProperty(D1.familyTitle)
}),
});

listOfExtraMandatoryFields.forEach(extraMandatoryField => {
Serie = Serie.setKey(extraMandatoryField, z.string({
required_error: D.mandatoryProperty(fieldToTitleMapping[extraMandatoryField] ?? "")
}).min(1, {
message: D.mandatoryProperty(fieldToTitleMapping[extraMandatoryField] ?? "")
}))
})

export const validate = formatValidation(Serie)

0 comments on commit a469dea

Please sign in to comment.