1
0

[1950][DURACOM-101] JSDoc and regex validation tests

This commit is contained in:
Vincenzo Mecca
2022-12-20 10:23:19 +01:00
parent 6e2279d155
commit 8f91e146de
2 changed files with 66 additions and 1 deletions

View File

@@ -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<string> = new InjectionToken<string>(
export const CONFIG_DATA: InjectionToken<FormFieldModel> = new InjectionToken<FormFieldModel>('configData');
export const INIT_FORM_VALUES: InjectionToken<any> = new InjectionToken<any>('initFormValues');
export const PARSER_OPTIONS: InjectionToken<ParserOptions> = new InjectionToken<ParserOptions>('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;

View File

@@ -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);
});
});
});