Skip to content

Commit 4d0703f

Browse files
authored
fix(ontology): fix regex pattern in ontology form (DSP-1139) (#483)
* fix(ontology): fix regex pattern in ontology form (DSP-1139) * fix(ontology): fix regex pattern for onto name * test(ontology): test onto name regex pattern
1 parent d916b4b commit 4d0703f

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎src/app/project/ontology/ontology-form/ontology-form.component.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!-- auto complete list to select resource classes -->
33
<div class="form-content">
44
<mat-form-field class="large-field ontology-name">
5-
<input matInput class="ontology-name" [maxlength]="nameMaxLength" [formControl]="ontologyForm.controls['name']"
5+
<input matInput class="ontology-name" autocomplete="off" [maxlength]="nameMaxLength" [formControl]="ontologyForm.controls['name']"
66
[placeholder]="'Set a unique name *'">
77
<mat-icon matSuffix [matTooltip]="ontologyNameInfo" matTooltipPosition="above" matTooltipClass="multi-line-tooltip">help</mat-icon>
88
<mat-hint *ngIf="formErrors.name" class="medium-field">

‎src/app/project/ontology/ontology-form/ontology-form.component.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,50 @@ describe('OntologyFormComponent', () => {
126126

127127
});
128128

129+
it('should test form validity with not allowed names', () => {
130+
const form = ontologyFormComponent.ontologyForm;
131+
expect(form.valid).toBeFalsy();
132+
133+
const nameInput = form.controls.name;
134+
135+
nameInput.setValue('v2onto');
136+
expect(form.valid).toBeFalsy();
137+
138+
nameInput.setValue('my-onto');
139+
expect(form.valid).toBeFalsy();
140+
141+
nameInput.setValue('2ndOnto');
142+
expect(form.valid).toBeFalsy();
143+
144+
nameInput.setValue('-notAllowed');
145+
expect(form.valid).toBeFalsy();
146+
147+
nameInput.setValue('_notAllowed');
148+
expect(form.valid).toBeFalsy();
149+
150+
nameInput.setValue('not-allowed');
151+
expect(form.valid).toBeFalsy();
152+
153+
nameInput.setValue('not_allowed');
154+
expect(form.valid).toBeFalsy();
155+
156+
nameInput.setValue('not.allowed');
157+
expect(form.valid).toBeFalsy();
158+
159+
nameInput.setValue('no$or€');
160+
expect(form.valid).toBeFalsy();
161+
162+
nameInput.setValue('no spaces');
163+
expect(form.valid).toBeFalsy();
164+
165+
nameInput.setValue('ältereOnto');
166+
expect(form.valid).toBeFalsy();
167+
168+
nameInput.setValue('bestOnto');
169+
expect(form.valid).toBeTruthy();
170+
171+
});
172+
129173
it('should test form validity without label', () => {
130174
const form = ontologyFormComponent.ontologyForm;
131175
expect(form.valid).toBeFalsy();

‎src/app/project/ontology/ontology-form/ontology-form.component.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ export class OntologyFormComponent implements OnInit {
5151

5252
lastModificationDate: string;
5353

54-
nameRegex = /^(?![vV][0-9]|[0-9]|[\u00C0-\u017F]).[a-zA-Z0-9]+\S*$/;
54+
// regex to check ontology name: shouldn't start with a number or with 'v' followed by a number, spaces or special characters are not allowed
55+
nameRegex = /^(?![vV]+[0-9])+^([a-zA-Z])[a-zA-Z0-9]*$/;
5556

57+
// ontology name must not contain one of the following words
5658
forbiddenNames: string[] = [
5759
'knora',
5860
'salsah',

0 commit comments

Comments
 (0)