From 27c67499dd7a0ff31e406827d73f99959cc7f487 Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Thu, 11 May 2023 16:43:07 +0200 Subject: [PATCH] Feat/validation codelist (#404) * feat: make contributor editable for a structure #362 * Particla Code List identifier should contain only a-zA-Z0-9_ #382 --- packages/codelists/src/i18n/dictionary.js | 4 +++ packages/codelists/src/utils/index.js | 4 +++ packages/codelists/src/utils/index.spec.js | 34 ++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 packages/codelists/src/utils/index.spec.js diff --git a/packages/codelists/src/i18n/dictionary.js b/packages/codelists/src/i18n/dictionary.js index 20cc208c5..25658210b 100644 --- a/packages/codelists/src/i18n/dictionary.js +++ b/packages/codelists/src/i18n/dictionary.js @@ -238,6 +238,10 @@ const dictionary = { fr: propertyName => `La propriété ${propertyName} est obligatoire.`, en: propertyName => `The property ${propertyName} is required.` }, + validCharactersProperty: { + fr: propertyName => `La propriété ${propertyName} possède des caractères invalid.`, + en: propertyName => `The property ${propertyName} has invalid characters.` + }, errors: { GlobalClientSideErrorBloc: { fr: 'Vous avez des erreurs dans ce formulaire.', diff --git a/packages/codelists/src/utils/index.js b/packages/codelists/src/utils/index.js index bd7bfe7a9..630c6a706 100644 --- a/packages/codelists/src/utils/index.js +++ b/packages/codelists/src/utils/index.js @@ -63,8 +63,12 @@ export const validatePartialCodelist = (codelist) => { if(!codelist.id){ errorMessage.push(D.mandatoryProperty(D.idTitle)); fields.id = D.mandatoryProperty(D.idTitle); + } else if(!/^[a-zA-Z0-9_]*$/.test(codelist.id)){ + errorMessage.push(D.validCharactersProperty(D1.idTitle)); + fields.id = D.validCharactersProperty(D1.idTitle); } + if(!codelist.parentCode){ errorMessage.push(D.mandatoryProperty(D.parentCodelist)); fields.parentCode = D.mandatoryProperty(D.parentCodelist); diff --git a/packages/codelists/src/utils/index.spec.js b/packages/codelists/src/utils/index.spec.js new file mode 100644 index 000000000..939a75ff6 --- /dev/null +++ b/packages/codelists/src/utils/index.spec.js @@ -0,0 +1,34 @@ +import { validatePartialCodelist } from './index'; + +describe('validatePartialCodelist', () => { + it('should return no error', () => { + expect(validatePartialCodelist({ + id: 'id', + parentCode: 'parentCode', + labelLg1: 'labelLg1', + labelLg2: 'labelLg2', + creator: 'creator', + disseminationStatus: 'disseminationStatus', + })).toEqual({ + errorMessage: [], + fields: {} + }) + }) + it('should return the error if the labelLg1 and labelLg2 contain unacceptable characters', () => { + expect(validatePartialCodelist({ + id: 'i d', + parentCode: 'parentCode', + labelLg1: 'labelLg1', + labelLg2: 'labelLg2', + creator: 'creator', + disseminationStatus: 'disseminationStatus', + })).toEqual({ + errorMessage: [ + 'The property Identifiant has invalid characters.' + ], + fields: { + id: 'The property Identifiant has invalid characters.' + } + }) + }) +})