Skip to content

Commit

Permalink
zod on families
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreVasseur committed Feb 13, 2024
1 parent b907ea0 commit d3eb41d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
43 changes: 26 additions & 17 deletions app/src/js/applications/operations/families/edition/validation.js
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>Intitulé</strong> is required.'],
fields: {
'prefLabelLg1': 'The property <strong>Intitulé</strong> is required.',
Expand All @@ -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 <strong>Title</strong> is required.'],
fields: {
'prefLabelLg1': '',
Expand All @@ -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 <strong>Intitulé</strong> is required.',
'The property <strong>Title</strong> is required.'
Expand Down

0 comments on commit d3eb41d

Please sign in to comment.