-
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/extend Zod on datasets and distributions (#605)
* add tests and zod template * fix tests * fix tests * fix tests * fix tests * fix tests * replace conditions with zod * minor change * minor change * minor change * reshape
- Loading branch information
1 parent
c2173fb
commit 2112b0d
Showing
5 changed files
with
196 additions
and
62 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 |
---|---|---|
@@ -1,43 +1,14 @@ | ||
import D, { D1, D2 } from '../../../i18n/build-dictionary'; | ||
import { formatValidation } from 'js/utils/validation'; | ||
import { z } from 'zod'; | ||
|
||
export function validate({ | ||
catalogRecord, | ||
disseminationStatus, | ||
labelLg1, | ||
labelLg2, | ||
idSerie, | ||
}) { | ||
const errorMessages = []; | ||
const { creator, contributor } = catalogRecord ?? {}; | ||
if (!labelLg1) { | ||
errorMessages.push(D.mandatoryProperty(D1.title)); | ||
} | ||
if (!labelLg2) { | ||
errorMessages.push(D.mandatoryProperty(D2.title)); | ||
} | ||
if (!creator) { | ||
errorMessages.push(D.mandatoryProperty(D1.creatorTitle)); | ||
} | ||
if (!contributor) { | ||
errorMessages.push(D.mandatoryProperty(D1.contributorTitle)); | ||
} | ||
if (!disseminationStatus) { | ||
errorMessages.push(D.mandatoryProperty(D1.disseminationStatusTitle)); | ||
} | ||
if (!idSerie) { | ||
errorMessages.push(D.mandatoryProperty(D1.generatedBy)); | ||
} | ||
return { | ||
fields: { | ||
labelLg1: !labelLg1 ? D.mandatoryProperty(D1.title) : '', | ||
labelLg2: !labelLg2 ? D.mandatoryProperty(D2.title) : '', | ||
creator: !creator ? D.mandatoryProperty(D1.creatorTitle) : '', | ||
contributor: !contributor ? D.mandatoryProperty(D1.contributorTitle) : '', | ||
disseminationStatus: !disseminationStatus | ||
? D.mandatoryProperty(D1.disseminationStatusTitle) | ||
: '', | ||
idSerie: !idSerie ? D.mandatoryProperty(D1.generatedBy) : '', | ||
}, | ||
errorMessage: errorMessages, | ||
}; | ||
} | ||
const Dataset = z.object({ | ||
labelLg1: z.string({ required_error: D.mandatoryProperty(D1.title) }).min(1, { message: D.mandatoryProperty(D1.title) }), | ||
labelLg2: z.string({ required_error: D.mandatoryProperty(D2.title) }).min(1, { message: D.mandatoryProperty(D2.title) }), | ||
creator: z.string({ required_error: D.mandatoryProperty(D1.creatorTitle) }), | ||
contributor: z.string({ required_error: D.mandatoryProperty(D1.contributorTitle) }), | ||
disseminationStatus: z.string({ required_error: D.mandatoryProperty(D1.disseminationStatusTitle) }), | ||
idSerie: z.string({ required_error: D.mandatoryProperty(D1.generatedBy) }), | ||
}); | ||
|
||
export const validate = ({catalogRecord, ...otherFields}) => formatValidation(Dataset)({creator: catalogRecord?.creator, contributor: catalogRecord?.contributor, ...otherFields}); |
101 changes: 101 additions & 0 deletions
101
app/src/js/applications/datasets/datasets/validation.spec.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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { validate } from './validation'; | ||
|
||
describe('validation', function() { | ||
it('should return an error for labelLg1', function() { | ||
expect( | ||
validate({ | ||
labelLg2: 'labelLg2', | ||
catalogRecord: { | ||
creator: 'creator', | ||
contributor: 'contributor', | ||
}, | ||
disseminationStatus: 'status', | ||
idSerie: 'id', | ||
}) | ||
).toEqual({ | ||
errorMessage: [ | ||
'The property <strong>Intitulé</strong> is required.', | ||
], | ||
fields: { | ||
labelLg1: 'The property <strong>Intitulé</strong> is required.', | ||
labelLg2: '', | ||
creator: '', | ||
contributor: '', | ||
disseminationStatus: '', | ||
idSerie: '', | ||
}, | ||
}); | ||
}); | ||
it('should return an error for labelLg2', function() { | ||
expect( | ||
validate({ | ||
labelLg1: 'labelLg1', | ||
catalogRecord: { | ||
creator: 'creator', | ||
contributor: 'contributor', | ||
}, | ||
disseminationStatus: 'status', | ||
idSerie: 'id', | ||
}) | ||
).toEqual({ | ||
errorMessage: [ | ||
'The property <strong>Title</strong> is required.', | ||
], | ||
fields: { | ||
labelLg1: '', | ||
labelLg2: 'The property <strong>Title</strong> is required.', | ||
creator: '', | ||
contributor: '', | ||
disseminationStatus: '', | ||
idSerie: '', | ||
}, | ||
}); | ||
}); | ||
it('should return an error for creator, contributor, disseminationStatus and idSerie', function() { | ||
expect( | ||
validate({ | ||
labelLg1: 'labelLg2', | ||
labelLg2: 'labelLg2', | ||
}) | ||
).toEqual({ | ||
errorMessage: [ | ||
'The property <strong>Propriétaire</strong> is required.', | ||
'The property <strong>Gestionnaire</strong> is required.', | ||
'The property <strong>Statut de diffusion</strong> is required.', | ||
'The property <strong>Produit de</strong> is required.', | ||
], | ||
fields: { | ||
labelLg1: '', | ||
labelLg2: '', | ||
creator: 'The property <strong>Propriétaire</strong> is required.', | ||
contributor: 'The property <strong>Gestionnaire</strong> is required.', | ||
disseminationStatus: 'The property <strong>Statut de diffusion</strong> is required.', | ||
idSerie: 'The property <strong>Produit de</strong> is required.', | ||
}, | ||
}); | ||
}); | ||
it('should return no error', function() { | ||
expect( | ||
validate({ | ||
labelLg1: 'labelLg2', | ||
labelLg2: 'labelLg2', | ||
catalogRecord: { | ||
creator: 'creator', | ||
contributor: 'contributor', | ||
}, | ||
disseminationStatus: 'status', | ||
idSerie: 'id', | ||
}) | ||
).toEqual({ | ||
errorMessage: [], | ||
fields: { | ||
labelLg1: '', | ||
labelLg2: '', | ||
creator: '', | ||
contributor: '', | ||
disseminationStatus: '', | ||
idSerie: '', | ||
}, | ||
}); | ||
}); | ||
}); |
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
11 changes: 11 additions & 0 deletions
11
app/src/js/applications/datasets/distributions/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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import D, { D1, D2 } from '../../../i18n/build-dictionary'; | ||
import { formatValidation } from 'js/utils/validation'; | ||
import { z } from 'zod'; | ||
|
||
const Distribution = z.object({ | ||
labelLg1: z.string({ required_error: D.mandatoryProperty(D1.title) }).min(1, { message: D.mandatoryProperty(D1.title) }), | ||
labelLg2: z.string({ required_error: D.mandatoryProperty(D2.title) }).min(1, { message: D.mandatoryProperty(D2.title) }), | ||
idDataset: z.string({ required_error: D.mandatoryProperty(D1.datasetsTitle) }), | ||
}); | ||
|
||
export const validate = formatValidation(Distribution); |
71 changes: 71 additions & 0 deletions
71
app/src/js/applications/datasets/distributions/validation.spec.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { validate } from './validation'; | ||
|
||
describe('validation', function() { | ||
it('should return an error for labelLg1', function() { | ||
expect( | ||
validate({ | ||
labelLg2: 'labelLg2', | ||
idDataset: 'id', | ||
}) | ||
).toEqual({ | ||
errorMessage: [ | ||
'The property <strong>Intitulé</strong> is required.', | ||
], | ||
fields: { | ||
labelLg1: 'The property <strong>Intitulé</strong> is required.', | ||
labelLg2: '', | ||
idDataset: '', | ||
}, | ||
}); | ||
}); | ||
it('should return an error for labelLg2', function() { | ||
expect( | ||
validate({ | ||
labelLg1: 'labelLg1', | ||
idDataset: 'id', | ||
}) | ||
).toEqual({ | ||
errorMessage: [ | ||
'The property <strong>Title</strong> is required.', | ||
], | ||
fields: { | ||
labelLg1: '', | ||
labelLg2: 'The property <strong>Title</strong> is required.', | ||
idDataset: '', | ||
}, | ||
}); | ||
}); | ||
it('should return an error for idDataset', function() { | ||
expect( | ||
validate({ | ||
labelLg1: 'labelLg1', | ||
labelLg2: 'labelLg2', | ||
}) | ||
).toEqual({ | ||
errorMessage: [ | ||
'The property <strong>Jeu de Données</strong> is required.', | ||
], | ||
fields: { | ||
labelLg1: '', | ||
labelLg2: '', | ||
idDataset: 'The property <strong>Jeu de Données</strong> is required.', | ||
}, | ||
}); | ||
}); | ||
it('should return no error', function() { | ||
expect( | ||
validate({ | ||
labelLg1: 'labelLg2', | ||
labelLg2: 'labelLg2', | ||
idDataset: 'id', | ||
}) | ||
).toEqual({ | ||
errorMessage: [], | ||
fields: { | ||
labelLg1: '', | ||
labelLg2: '', | ||
idDataset: '', | ||
}, | ||
}); | ||
}); | ||
}); |