From 8f91e146de6514c8dc7cb6e278cd6d06ec761737 Mon Sep 17 00:00:00 2001 From: Vincenzo Mecca Date: Tue, 20 Dec 2022 10:23:19 +0100 Subject: [PATCH] [1950][DURACOM-101] JSDoc and regex validation tests --- .../form/builder/parsers/field-parser.ts | 19 +++++++- .../parsers/onebox-field-parser.spec.ts | 48 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/app/shared/form/builder/parsers/field-parser.ts b/src/app/shared/form/builder/parsers/field-parser.ts index d446877897..c64de0e3ff 100644 --- a/src/app/shared/form/builder/parsers/field-parser.ts +++ b/src/app/shared/form/builder/parsers/field-parser.ts @@ -1,7 +1,12 @@ import { Inject, InjectionToken } from '@angular/core'; import uniqueId from 'lodash/uniqueId'; -import { DynamicFormControlLayout, DynamicFormControlRelation, MATCH_VISIBLE, OR_OPERATOR } from '@ng-dynamic-forms/core'; +import { + DynamicFormControlLayout, + DynamicFormControlRelation, + MATCH_VISIBLE, + OR_OPERATOR +} from '@ng-dynamic-forms/core'; import { hasValue, isNotEmpty, isNotNull, isNotUndefined } from '../../../empty.util'; import { FormFieldModel } from '../models/form-field.model'; @@ -22,6 +27,11 @@ export const SUBMISSION_ID: InjectionToken = new InjectionToken( export const CONFIG_DATA: InjectionToken = new InjectionToken('configData'); export const INIT_FORM_VALUES: InjectionToken = new InjectionToken('initFormValues'); export const PARSER_OPTIONS: InjectionToken = new InjectionToken('parserOptions'); +/** + * This pattern checks that a regex field uses the common ECMAScript format: `/{pattern}/{flags}`, in which the flags + * are part of the regex, or a simpler one with only pattern `/{pattern}/` or `{pattern}`. + * The regex itself is encapsulated inside a `RegExp` object, that will validate the pattern syntax. + */ export const REGEX_FIELD_VALIDATOR = new RegExp('(\\/?)(.+)\\1([gimsuy]*)', 'i'); export abstract class FieldParser { @@ -345,6 +355,13 @@ export abstract class FieldParser { return hasValue(this.configData.input.regex); } + /** + * Adds pattern validation to `controlModel`, it uses the encapsulated `configData` to test the regex, + * contained in the input config, against the common `ECMAScript` standard validator {@link REGEX_FIELD_VALIDATOR}, + * and creates an equivalent `RegExp` object that will be used during form-validation against the user-input. + * @param controlModel + * @protected + */ protected addPatternValidator(controlModel) { const validatorMatcher = this.configData.input.regex.match(REGEX_FIELD_VALIDATOR); let regex; diff --git a/src/app/shared/form/builder/parsers/onebox-field-parser.spec.ts b/src/app/shared/form/builder/parsers/onebox-field-parser.spec.ts index e7e68a6461..a4c71d1f42 100644 --- a/src/app/shared/form/builder/parsers/onebox-field-parser.spec.ts +++ b/src/app/shared/form/builder/parsers/onebox-field-parser.spec.ts @@ -4,6 +4,7 @@ import { DynamicQualdropModel } from '../ds-dynamic-form-ui/models/ds-dynamic-qu import { DynamicOneboxModel } from '../ds-dynamic-form-ui/models/onebox/dynamic-onebox.model'; import { DsDynamicInputModel } from '../ds-dynamic-form-ui/models/ds-dynamic-input.model'; import { ParserOptions } from './parser-options'; +import { FieldParser } from './field-parser'; describe('OneboxFieldParser test suite', () => { let field1: FormFieldModel; @@ -101,4 +102,51 @@ describe('OneboxFieldParser test suite', () => { expect(fieldModel instanceof DynamicOneboxModel).toBe(true); }); + describe('should handle a DynamicOneboxModel with regex', () => { + let regexField: FormFieldModel; + let parser: FieldParser; + let fieldModel: any; + + beforeEach(() => { + regexField = { + input: { type: 'onebox', regex: '/[a-z]+/mi' }, + label: 'Title', + mandatory: 'false', + repeatable: false, + hints: 'Enter the name of the events, if any.', + selectableMetadata: [ + { + metadata: 'title', + controlledVocabulary: 'EVENTAuthority', + closed: false + } + ], + languageCodes: [] + } as FormFieldModel; + + parser = new OneboxFieldParser(submissionId, regexField, initFormValues, parserOptions); + fieldModel = parser.parse(); + }); + + it('should have initialized pattern validator', () => { + expect(fieldModel instanceof DynamicOneboxModel).toBe(true); + expect(fieldModel.validators).not.toBeNull(); + expect(fieldModel.validators.pattern).not.toBeNull(); + }); + + it('should mark valid not case sensitive basic characters regex in multiline', () => { + let pattern = fieldModel.validators.pattern as RegExp; + expect(pattern.test('HELLO')).toBe(true); + expect(pattern.test('hello')).toBe(true); + expect(pattern.test('hello\nhello\nhello')).toBe(true); + expect(pattern.test('HeLlO')).toBe(true); + }); + + it('should be invalid for non-basic alphabet characters', () => { + let pattern = fieldModel.validators.pattern as RegExp; + expect(pattern.test('12345')).toBe(false); + expect(pattern.test('àèìòùáéíóú')).toBe(false); + }); + }); + });