Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More meaningful regex error messages #2640

Merged
merged 7 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app/shared/form/builder/form-builder.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ import {
DynamicTextAreaModel,
DynamicTimePickerModel,
} from '@ng-dynamic-forms/core';
import { TranslateService } from '@ngx-translate/core';

import { FormRowModel } from '../../../core/config/models/config-submission-form.model';
import { SubmissionFormsModel } from '../../../core/config/models/config-submission-forms.model';
import { ConfigurationDataService } from '../../../core/data/configuration-data.service';
import { ConfigurationProperty } from '../../../core/shared/configuration-property.model';
import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model';
import { getMockTranslateService } from '../../mocks/translate.service.mock';
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
import { DynamicDsDatePickerModel } from './ds-dynamic-form-ui/models/date-picker/date-picker.model';
import { DynamicConcatModel } from './ds-dynamic-form-ui/models/ds-dynamic-concat.model';
Expand Down Expand Up @@ -85,6 +87,7 @@ describe('FormBuilderService test suite', () => {

beforeEach(() => {
configSpy = createConfigSuccessSpy(typeFieldTestValue);
let translateService = getMockTranslateService();
TestBed.configureTestingModule({
imports: [ReactiveFormsModule],
providers: [
Expand All @@ -93,6 +96,7 @@ describe('FormBuilderService test suite', () => {
{ provide: NG_VALIDATORS, useValue: testValidator, multi: true },
{ provide: NG_ASYNC_VALIDATORS, useValue: testAsyncValidator, multi: true },
{ provide: ConfigurationDataService, useValue: configSpy },
{ provide: TranslateService, useValue: translateService },
],
});

Expand Down
4 changes: 3 additions & 1 deletion src/app/shared/form/builder/parsers/concat-field-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

import {
hasNoValue,
Expand Down Expand Up @@ -34,10 +35,11 @@ export class ConcatFieldParser extends FieldParser {
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
translate: TranslateService,
protected separator: string,
protected firstPlaceholder: string = null,
protected secondPlaceholder: string = null) {
super(submissionId, configData, initFormValues, parserOptions);
super(submissionId, configData, initFormValues, parserOptions, translate);

this.separator = separator;
this.firstPlaceholder = firstPlaceholder;
Expand Down
11 changes: 8 additions & 3 deletions src/app/shared/form/builder/parsers/date-field-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicDsDatePickerModel } from '../ds-dynamic-form-ui/models/date-picker/date-picker.model';
import { FormFieldModel } from '../models/form-field.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
import { DateFieldParser } from './date-field-parser';
import { ParserOptions } from './parser-options';



describe('DateFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues: any = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -37,13 +42,13 @@ describe('DateFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(parser instanceof DateFieldParser).toBe(true);
});

it('should return a DynamicDsDatePickerModel object when repeatable option is false', () => {
const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -56,7 +61,7 @@ describe('DateFieldParser test suite', () => {
};
const expectedValue = '1983-11-18';

const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicDisabledModel } from '../ds-dynamic-form-ui/models/disabled/dynamic-disabled.model';
import { FormFieldModel } from '../models/form-field.model';
import { DisabledFieldParser } from './disabled-field-parser';
Expand All @@ -6,6 +8,7 @@ import { ParserOptions } from './parser-options';
describe('DisabledFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues: any = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -35,13 +38,13 @@ describe('DisabledFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new DisabledFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DisabledFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(parser instanceof DisabledFieldParser).toBe(true);
});

it('should return a DynamicDisabledModel object when repeatable option is false', () => {
const parser = new DisabledFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DisabledFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -56,7 +59,7 @@ describe('DisabledFieldParser test suite', () => {
};
const expectedValue = 'test description';

const parser = new DisabledFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DisabledFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();
expect(fieldModel.value.value).toEqual(expectedValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicScrollableDropdownModel } from '../ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.model';
import { FormFieldModel } from '../models/form-field.model';
import { DropdownFieldParser } from './dropdown-field-parser';
import { ParserOptions } from './parser-options';

describe('DropdownFieldParser test suite', () => {
let field: FormFieldModel;
let translateService = getMockTranslateService();

const submissionId = '1234';
const initFormValues = {};
Expand Down Expand Up @@ -37,13 +40,13 @@ describe('DropdownFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new DropdownFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DropdownFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(parser instanceof DropdownFieldParser).toBe(true);
});

it('should return a DynamicScrollableDropdownModel object when repeatable option is false', () => {
const parser = new DropdownFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DropdownFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -52,7 +55,7 @@ describe('DropdownFieldParser test suite', () => {

it('should throw when authority is not passed', () => {
field.selectableMetadata[0].controlledVocabulary = null;
const parser = new DropdownFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new DropdownFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(() => parser.parse())
.toThrow();
Expand Down
4 changes: 3 additions & 1 deletion src/app/shared/form/builder/parsers/dropdown-field-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject } from '@angular/core';
import { DynamicFormControlLayout } from '@ng-dynamic-forms/core';
import { TranslateService } from '@ngx-translate/core';

import { isNotEmpty } from '../../../empty.util';
import {
Expand All @@ -24,8 +25,9 @@ export class DropdownFieldParser extends FieldParser {
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
translate: TranslateService,
) {
super(submissionId, configData, initFormValues, parserOptions);
super(submissionId, configData, initFormValues, parserOptions, translate);
}

public modelFactory(fieldValue?: FormFieldMetadataValueObject | any, label?: boolean): any {
Expand Down
7 changes: 6 additions & 1 deletion src/app/shared/form/builder/parsers/field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MATCH_VISIBLE,
OR_OPERATOR,
} from '@ng-dynamic-forms/core';
import { TranslateService } from '@ngx-translate/core';
import uniqueId from 'lodash/uniqueId';

import { SubmissionScopeType } from '../../../../core/submission/submission-scope-type';
Expand Down Expand Up @@ -61,6 +62,7 @@ export abstract class FieldParser {
@Inject(CONFIG_DATA) protected configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) protected initFormValues: any,
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions,
protected translate: TranslateService,
) {
}

Expand Down Expand Up @@ -405,11 +407,14 @@ export abstract class FieldParser {
} else {
regex = new RegExp(this.configData.input.regex);
}
const baseTranslationKey = 'error.validation.pattern';
const fieldranslationKey = `${baseTranslationKey}.${controlModel.id}`;
const fieldTranslationExists = this.translate.instant(fieldranslationKey) !== fieldranslationKey;
controlModel.validators = Object.assign({}, controlModel.validators, { pattern: regex });
controlModel.errorMessages = Object.assign(
{},
controlModel.errorMessages,
{ pattern: 'error.validation.pattern' });
{ pattern: fieldTranslationExists ? fieldranslationKey : baseTranslationKey });
}

protected markAsRequired(controlModel) {
Expand Down
11 changes: 7 additions & 4 deletions src/app/shared/form/builder/parsers/list-field-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicListCheckboxGroupModel } from '../ds-dynamic-form-ui/models/list/dynamic-list-checkbox-group.model';
import { DynamicListRadioGroupModel } from '../ds-dynamic-form-ui/models/list/dynamic-list-radio-group.model';
import { FormFieldModel } from '../models/form-field.model';
Expand All @@ -8,6 +10,7 @@ import { ParserOptions } from './parser-options';
describe('ListFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -39,13 +42,13 @@ describe('ListFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(parser instanceof ListFieldParser).toBe(true);
});

it('should return a DynamicListCheckboxGroupModel object when repeatable option is true', () => {
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -54,7 +57,7 @@ describe('ListFieldParser test suite', () => {

it('should return a DynamicListRadioGroupModel object when repeatable option is false', () => {
field.repeatable = false;
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -67,7 +70,7 @@ describe('ListFieldParser test suite', () => {
};
const expectedValue = [new FormFieldMetadataValueObject('test type')];

const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new ListFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicLookupModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup.model';
import { FormFieldModel } from '../models/form-field.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
Expand All @@ -7,6 +9,7 @@ import { ParserOptions } from './parser-options';
describe('LookupFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -38,13 +41,13 @@ describe('LookupFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new LookupFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new LookupFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(parser instanceof LookupFieldParser).toBe(true);
});

it('should return a DynamicLookupModel object when repeatable option is false', () => {
const parser = new LookupFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new LookupFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -57,7 +60,7 @@ describe('LookupFieldParser test suite', () => {
};
const expectedValue = new FormFieldMetadataValueObject('test journal');

const parser = new LookupFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new LookupFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicLookupNameModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model';
import { FormFieldModel } from '../models/form-field.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
Expand All @@ -7,6 +9,7 @@ import { ParserOptions } from './parser-options';
describe('LookupNameFieldParser test suite', () => {
let field: FormFieldModel;
let initFormValues = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -38,13 +41,13 @@ describe('LookupNameFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new LookupNameFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new LookupNameFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

expect(parser instanceof LookupNameFieldParser).toBe(true);
});

it('should return a DynamicLookupNameModel object when repeatable option is false', () => {
const parser = new LookupNameFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new LookupNameFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -57,7 +60,7 @@ describe('LookupNameFieldParser test suite', () => {
};
const expectedValue = new FormFieldMetadataValueObject('test author');

const parser = new LookupNameFieldParser(submissionId, field, initFormValues, parserOptions);
const parser = new LookupNameFieldParser(submissionId, field, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand Down
11 changes: 7 additions & 4 deletions src/app/shared/form/builder/parsers/name-field-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

import { DynamicConcatModel } from '../ds-dynamic-form-ui/models/ds-dynamic-concat.model';
import { FormFieldModel } from '../models/form-field.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
Expand All @@ -9,6 +11,7 @@ describe('NameFieldParser test suite', () => {
let field2: FormFieldModel;
let field3: FormFieldModel;
let initFormValues: any = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -71,21 +74,21 @@ describe('NameFieldParser test suite', () => {
});

it('should init parser properly', () => {
const parser = new NameFieldParser(submissionId, field1, initFormValues, parserOptions);
const parser = new NameFieldParser(submissionId, field1, initFormValues, parserOptions, translateService);

expect(parser instanceof NameFieldParser).toBe(true);
});

it('should return a DynamicConcatModel object when repeatable option is false', () => {
const parser = new NameFieldParser(submissionId, field2, initFormValues, parserOptions);
const parser = new NameFieldParser(submissionId, field2, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

expect(fieldModel instanceof DynamicConcatModel).toBe(true);
});

it('should return a DynamicConcatModel object with the correct separator', () => {
const parser = new NameFieldParser(submissionId, field2, initFormValues, parserOptions);
const parser = new NameFieldParser(submissionId, field2, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand All @@ -98,7 +101,7 @@ describe('NameFieldParser test suite', () => {
};
const expectedValue = new FormFieldMetadataValueObject('test, name', undefined, undefined, 'test');

const parser = new NameFieldParser(submissionId, field1, initFormValues, parserOptions);
const parser = new NameFieldParser(submissionId, field1, initFormValues, parserOptions, translateService);

const fieldModel = parser.parse();

Expand Down
Loading
Loading