Skip to content

Commit

Permalink
fix(dynamic-forms): removed regex for name (#1155)
Browse files Browse the repository at this point in the history
* fix(dynamic-forms): removed regex for name

* fix(dynamic-forms): modified regex to allow japanese/chinese chars

* fix(dynamic-forms): got rid of spaces
  • Loading branch information
JulieMarie authored and emoralesb05 committed May 25, 2018
1 parent 7e217df commit ddf8a6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
38 changes: 25 additions & 13 deletions src/platform/dynamic-forms/services/dynamic-forms.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,49 @@ describe('Service: TdDynamicFormsService', () => {
});
}));

it('expect to validate element names correctly',
async(inject([TdDynamicFormsService], (service: TdDynamicFormsService) => {
it('expect to validate element names correctly',
async(inject([TdDynamicFormsService], (service: TdDynamicFormsService) => {
try {
service.validateDynamicElementName('normal-name');
} catch (e) {
expect(e).toBeFalsy('name should be validated correctly');
}

try {
service.validateDynamicElementName('normal-name');
service.validateDynamicElementName('normal_22_name');
} catch (e) {
expect(e).toBeFalsy('name should be validated correctly');
expect(e).toBeFalsy('name should be validated correctly');
}

try {
service.validateDynamicElementName('normal_22_name');
service.validateDynamicElementName('normal_22-name_22');
} catch (e) {
expect(e).toBeFalsy('name should be validated correctly');
expect(e).toBeFalsy('name should be validated correctly');
}

try {
service.validateDynamicElementName('normal_22-name_22');
service.validateDynamicElementName('王先生');
} catch (e) {
expect(e).toBeFalsy('name should be validated correctly');
expect(e).toBeFalsy('王先生 (chinese) name should be validated correctly');
}

try {
service.validateDynamicElementName('2normal_22-name_22');
expect(false).toBeTruthy('2normal_22-name_22 name should not be validated correctly');
service.validateDynamicElementName('日本語');
} catch (e) {
expect(e).toBeFalsy('日本語 (japanese) name should be validated correctly');
}

try {
service.validateDynamicElementName('2normal_22-name_22');
expect(false).toBeTruthy('2normal_22-name_22 name should not be validated correctly');
} catch (e) { /* */ }

try {
service.validateDynamicElementName('normal@22-name_22');
expect(false).toBeTruthy('normal@22-name_22 name should not be validated correctly');
service.validateDynamicElementName('normal@22-name_22');
expect(false).toBeTruthy('normal@22-name_22 name should not be validated correctly');
} catch (e) { /* */ }

}),
}),
));

it('expect to return components depending on type | element',
Expand Down
4 changes: 2 additions & 2 deletions src/platform/dynamic-forms/services/dynamic-forms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface ITdDynamicElementConfig {
validators?: ITdDynamicElementValidator[];
}

export const DYNAMIC_ELEMENT_NAME_REGEX: RegExp = /^[a-zA-Z]+[a-zA-Z0-9-_]*$/;
export const DYNAMIC_ELEMENT_NAME_REGEX: RegExp = /^[^0-9][^\@]*$/;

@Injectable()
export class TdDynamicFormsService {
Expand All @@ -60,7 +60,7 @@ export class TdDynamicFormsService {
*/
validateDynamicElementName(name: string): void {
if (!DYNAMIC_ELEMENT_NAME_REGEX.test(name)) {
throw new Error(`Dynamic element name: "${name}" is not valid.`);
throw new Error('Dynamic element name: "${name}" is not valid.');
}
}

Expand Down

0 comments on commit ddf8a6b

Please sign in to comment.