Skip to content

Commit

Permalink
Merge pull request #2641 from hutattedonmyarm/regex-error-messages-ba…
Browse files Browse the repository at this point in the history
…ckport-dspace7x

[Port dspace-7_x] More meaningful regex error messages
  • Loading branch information
tdonohue authored May 1, 2024
2 parents 91d72c1 + ab23613 commit 971c076
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 63 deletions.
6 changes: 5 additions & 1 deletion src/app/shared/form/builder/form-builder.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import { FormRowModel } from '../../../core/config/models/config-submission-form
import {ConfigurationDataService} from '../../../core/data/configuration-data.service';
import {createSuccessfulRemoteDataObject$} from '../../remote-data.utils';
import {ConfigurationProperty} from '../../../core/shared/configuration-property.model';
import { getMockTranslateService } from '../../mocks/translate.service.mock';
import { TranslateService } from '@ngx-translate/core';

describe('FormBuilderService test suite', () => {

Expand Down Expand Up @@ -81,14 +83,16 @@ describe('FormBuilderService test suite', () => {

beforeEach(() => {
configSpy = createConfigSuccessSpy(typeFieldTestValue);
let translateService = getMockTranslateService();
TestBed.configureTestingModule({
imports: [ReactiveFormsModule],
providers: [
{ provide: FormBuilderService, useClass: FormBuilderService },
{ provide: DynamicFormValidationService, useValue: {} },
{ provide: NG_VALIDATORS, useValue: testValidator, multi: true },
{ provide: NG_ASYNC_VALIDATORS, useValue: testAsyncValidator, multi: true },
{ provide: ConfigurationDataService, useValue: configSpy }
{ 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
Expand Up @@ -19,6 +19,7 @@ import {
SUBMISSION_ID
} from './field-parser';
import { DsDynamicInputModel, DsDynamicInputModelConfig } from '../ds-dynamic-form-ui/models/ds-dynamic-input.model';
import { TranslateService } from '@ngx-translate/core';

export class ConcatFieldParser extends FieldParser {

Expand All @@ -27,10 +28,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
10 changes: 7 additions & 3 deletions src/app/shared/form/builder/parsers/date-field-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { DateFieldParser } from './date-field-parser';
import { DynamicDsDatePickerModel } from '../ds-dynamic-form-ui/models/date-picker/date-picker.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
import { ParserOptions } from './parser-options';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';



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 +41,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 +60,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
Expand Up @@ -2,10 +2,12 @@ import { FormFieldModel } from '../models/form-field.model';
import { ParserOptions } from './parser-options';
import { DisabledFieldParser } from './disabled-field-parser';
import { DynamicDisabledModel } from '../ds-dynamic-form-ui/models/disabled/dynamic-disabled.model';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

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 +37,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 +58,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
Expand Up @@ -2,9 +2,11 @@ import { FormFieldModel } from '../models/form-field.model';
import { DropdownFieldParser } from './dropdown-field-parser';
import { DynamicScrollableDropdownModel } from '../ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.model';
import { ParserOptions } from './parser-options';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

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

const submissionId = '1234';
const initFormValues = {};
Expand Down Expand Up @@ -37,13 +39,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 +54,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
Expand Up @@ -15,6 +15,7 @@ import {
import { isNotEmpty } from '../../../empty.util';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
import { ParserOptions } from './parser-options';
import { TranslateService } from '@ngx-translate/core';

export class DropdownFieldParser extends FieldParser {

Expand All @@ -23,8 +24,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
9 changes: 7 additions & 2 deletions src/app/shared/form/builder/parsers/field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { VocabularyOptions } from '../../../../core/submission/vocabularies/mode
import { ParserType } from './parser-type';
import { isNgbDateStruct } from '../../../date.util';
import { SubmissionScopeType } from '../../../../core/submission/submission-scope-type';
import { TranslateService } from '@ngx-translate/core';

export const SUBMISSION_ID: InjectionToken<string> = new InjectionToken<string>('submissionId');
export const CONFIG_DATA: InjectionToken<FormFieldModel> = new InjectionToken<FormFieldModel>('configData');
Expand All @@ -50,7 +51,8 @@ export abstract class FieldParser {
@Inject(SUBMISSION_ID) protected submissionId: string,
@Inject(CONFIG_DATA) protected configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) protected initFormValues: any,
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions,
protected translate: TranslateService
) {
}

Expand Down Expand Up @@ -395,11 +397,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
10 changes: 6 additions & 4 deletions src/app/shared/form/builder/parsers/list-field-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { ListFieldParser } from './list-field-parser';
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 { ParserOptions } from './parser-options';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

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

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -39,13 +41,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 +56,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 +69,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
Expand Up @@ -3,10 +3,12 @@ import { FormFieldMetadataValueObject } from '../models/form-field-metadata-valu
import { LookupFieldParser } from './lookup-field-parser';
import { DynamicLookupModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup.model';
import { ParserOptions } from './parser-options';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

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

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -38,13 +40,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 +59,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
Expand Up @@ -3,10 +3,12 @@ import { FormFieldMetadataValueObject } from '../models/form-field-metadata-valu
import { LookupNameFieldParser } from './lookup-name-field-parser';
import { DynamicLookupNameModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model';
import { ParserOptions } from './parser-options';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

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

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -38,13 +40,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 +59,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
10 changes: 6 additions & 4 deletions src/app/shared/form/builder/parsers/name-field-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { NameFieldParser } from './name-field-parser';
import { DynamicConcatModel } from '../ds-dynamic-form-ui/models/ds-dynamic-concat.model';
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
import { ParserOptions } from './parser-options';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';

describe('NameFieldParser test suite', () => {
let field1: FormFieldModel;
let field2: FormFieldModel;
let field3: FormFieldModel;
let initFormValues: any = {};
let translateService = getMockTranslateService();

const submissionId = '1234';
const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -71,21 +73,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 +100,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

0 comments on commit 971c076

Please sign in to comment.