diff --git a/app/src/js/applications/operations/families/edition/validation.js b/app/src/js/applications/operations/families/edition/validation.js index fbf92e86a..8c6f52dc6 100644 --- a/app/src/js/applications/operations/families/edition/validation.js +++ b/app/src/js/applications/operations/families/edition/validation.js @@ -1,19 +1,28 @@ -import D, {D1, D2} from 'js/i18n'; +import D, { D1, D2 } from 'js/i18n'; +import { formatValidation } from 'js/utils/validation'; +import { z } from 'zod'; -export function validate({ prefLabelLg1, prefLabelLg2 }) { +// export function validate({ prefLabelLg1, prefLabelLg2 }) { - const errorMessages = []; - if(!prefLabelLg1){ - errorMessages.push(D.mandatoryProperty(D1.title)) - } - if(!prefLabelLg2){ - errorMessages.push(D.mandatoryProperty(D2.title)) - } - return { - fields: { - prefLabelLg1: !prefLabelLg1 ? D.mandatoryProperty(D1.title) : '', - prefLabelLg2: !prefLabelLg2 ? D.mandatoryProperty(D2.title) : '', - }, - errorMessage: errorMessages - }; -} +// const errorMessages = []; +// if(!prefLabelLg1){ +// errorMessages.push(D.mandatoryProperty(D1.title)) +// } +// if(!prefLabelLg2){ +// errorMessages.push(D.mandatoryProperty(D2.title)) +// } +// return { +// fields: { +// prefLabelLg1: !prefLabelLg1 ? D.mandatoryProperty(D1.title) : '', +// prefLabelLg2: !prefLabelLg2 ? D.mandatoryProperty(D2.title) : '', +// }, +// errorMessage: errorMessages +// }; +// } + +const Family = z.object({ + prefLabelLg1: z.string().min(1, {message: D.mandatoryProperty(D1.title)}), + prefLabelLg2: z.string().min(1, {message: D.mandatoryProperty(D2.title)}), +}); + +export const validate = formatValidation(Family) \ No newline at end of file diff --git a/app/src/js/applications/operations/families/edition/validation.spec.js b/app/src/js/applications/operations/families/edition/validation.spec.js index 30f4f348c..1b582e1bb 100644 --- a/app/src/js/applications/operations/families/edition/validation.spec.js +++ b/app/src/js/applications/operations/families/edition/validation.spec.js @@ -2,7 +2,7 @@ import { validate } from './validation'; describe('validation', function() { it('should return an error for prefLabelLg1', function() { - expect(validate({ prefLabelLg2: 'prefLabelLg2' })).toEqual({ + expect(validate({ prefLabelLg1: '', prefLabelLg2: 'prefLabelLg2' })).toEqual({ errorMessage: ['The property Intitulé is required.'], fields: { 'prefLabelLg1': 'The property Intitulé is required.', @@ -11,7 +11,7 @@ describe('validation', function() { }); }); it('should return an error for prefLabelLg2', function() { - expect(validate({ prefLabelLg1: 'prefLabelLg1' })).toEqual({ + expect(validate({ prefLabelLg1: 'prefLabelLg1', prefLabelLg2: '' })).toEqual({ errorMessage: ['The property Title is required.'], fields: { 'prefLabelLg1': '', @@ -20,7 +20,7 @@ describe('validation', function() { }); }); it('should return an error for prefLabelLg1 and prefLabelLg2', function() { - expect(validate({ })).toEqual({ + expect(validate({ prefLabelLg1: '', prefLabelLg2: '' })).toEqual({ errorMessage: [ 'The property Intitulé is required.', 'The property Title is required.'