Skip to content
This repository has been archived by the owner on Mar 25, 2023. It is now read-only.

feat(validations): support letters of all existing languages as the 1st letter of entities #1514

Merged
merged 5 commits into from
Jan 10, 2019
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"tslib": "^1.9.0",
"uuid": "^3.1.0",
"web-animations-js": "2.3.1",
"xregexp": "^4.2.4",
"zone.js": "0.8.26"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,48 @@
<h3
*ngIf="title" class="mat-dialog-title"
>
{{ title | translate }}
</h3>
<h3 *ngIf="title" class="mat-dialog-title">{{ title | translate }}</h3>

<form
(ngSubmit)="submit()"
#form="ngForm"
novalidate
>
<form (ngSubmit)="submit()" #form="ngForm" novalidate>
<div class="mat-dialog-content">
<mat-radio-group
name="radio"
[ngModel]="mode"
(ngModelChange)="setMode($event)"
>
<mat-radio-button
*ngIf="enableAssign"
[value]="modes.assign"
>
<mat-radio-group name="radio" [ngModel]="mode" (ngModelChange)="setMode($event)">
<mat-radio-button *ngIf="enableAssign" [value]="modes.assign">
{{ assignLabel | translate }}
</mat-radio-button>

<mat-radio-button
*ngIf="enableCreate"
[value]="modes.create"
>
<mat-radio-button *ngIf="enableCreate" [value]="modes.create">
{{ createLabel | translate }}
</mat-radio-button>

<mat-radio-button
*ngIf="defaultValue && enableRemove"
[value]="modes.remove"
>
<div>{{ removeLabel | translate:{ value: defaultValue } }}</div>
<mat-radio-button *ngIf="defaultValue && enableRemove" [value]="modes.remove">
<div>{{ removeLabel | translate: { value: defaultValue } }}</div>
</mat-radio-button>
</mat-radio-group>

<mat-form-field
*ngIf="mode === modes.create"
class="full-width-input"
>
<mat-form-field *ngIf="mode === modes.create" class="full-width-input">
<input
matInput
type="text"
name="textValue"
pattern="^[a-zA-ZА-яЁё]{1}[^,]*$"
[pattern]="entityValidator"
[(ngModel)]="newValue"
[placeholder]="textFieldPlaceholder | translate"
[maxlength]="maxLength"
autofocus
required
>
/>
</mat-form-field>

<mat-form-field
class="full-width-input"
*ngIf="mode === modes.assign"
>
<mat-form-field class="full-width-input" *ngIf="mode === modes.assign">
<mat-select
name="selectValue"

[(ngModel)]="newValue"
[placeholder]="selectPlaceholder | translate"
>
<mat-option
*ngFor="let option of options"
[value]="option"
>
{{ option }}
</mat-option>
<mat-option *ngFor="let option of options" [value]="option"> {{ option }} </mat-option>
</mat-select>
</mat-form-field>
</div>

<div class="mat-dialog-actions">
<button
mat-button
color="primary"
type="button"
(click)="cancel()"
>
<button mat-button color="primary" type="button" (click)="cancel()">
{{ 'COMMON.CANCEL' | translate }}
</button>
<button
Expand All @@ -92,12 +54,7 @@
>
{{ 'COMMON.ASSIGN' | translate }}
</button>
<button
*ngIf="mode === modes.remove"
mat-button
color="primary"
type="submit"
>
<button *ngIf="mode === modes.remove" mat-button color="primary" type="submit">
{{ 'COMMON.REMOVE' | translate }}
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { MatInput } from '@angular/material';
import { Constants } from '../../constants';
const xRegExp = require('xregexp');

export enum Mode {
assign,
Expand Down Expand Up @@ -57,6 +59,7 @@ export class CreateUpdateDeleteDialogComponent implements OnInit {
public loading: boolean;
public newValue: string;

public entityValidator = xRegExp(Constants.entityValidator);
public modes = Mode;
// tslint:disable-next-line:variable-name
private _mode: Mode;
Expand Down
45 changes: 45 additions & 0 deletions src/app/shared/constants.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Constants } from './constants';
const xRegExp = require('xregexp');

describe('Entity Regex', () => {
const validEntityNames = ['name1', '漢語', 'name%$#', 'name surname', 'أَلِفأَلِف', '平仮名'];
const invalidEntityNames = [
'%name',
'$name',
'&name',
'[name',
']name',
'<name',
'>name',
'{name',
'}name',
'name ',
'!name',
'-name',
'(name',
')name',
'_name',
'?name',
'~name',
'^name',
'#name',
'@name',
`'name`,
'⏫name',
'⚫name',
];

it('should validate valid names', () => {
const urlRegex = xRegExp(Constants.entityValidator, 'i');
for (const validName of validEntityNames) {
expect(urlRegex.test(validName)).toBeTruthy();
}
});

it('should validate invalid names', () => {
const urlRegex = xRegExp(Constants.entityValidator, 'i');
for (const invalidName of invalidEntityNames) {
expect(urlRegex.test(invalidName)).toBeFalsy();
}
});
});
5 changes: 5 additions & 0 deletions src/app/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ export class Constants {
* Max length 255 for some entities fields
*/
public static readonly maxLength255 = 255;

/**
* The first symbol is a letter , further all symbols except comma
*/
public static readonly entityValidator = '^\\pL{1}[^,]*[\\S]$';
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

import { AffinityGroup, AffinityGroupType, emptyAffinityGroup } from '../../../shared/models';
import { FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { Constants } from '../../../shared';
const xRegExp = require('xregexp');

function isUniqName(affinityGroups: AffinityGroup[]): ValidatorFn {
return function(control: FormControl) {
Expand Down Expand Up @@ -96,8 +98,7 @@ export class AffinityGroupSelectorComponent implements OnInit, OnChanges {
name: this.formBuilder.control('', [
Validators.required,
Validators.maxLength(this.maxEntityNameLength),
// ^[^\\d_*&^%$#@!~-]{1}[^,]*$
Validators.pattern('^[A-Za-zА-Яа-я]{1}[^,]*$'),
Validators.pattern(xRegExp(Constants.entityValidator)),
isUniqName(this.affinityGroups),
]),
type: this.formBuilder.control(AffinityGroupType.antiAffinity, [Validators.required]),
Expand Down
Loading