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

noc file change for sonar security #3799

Closed
wants to merge 50 commits into from
Closed
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f706c17
noc file change for sonar security
OgunyemiO Jul 12, 2024
9e1d6de
no-useless-escape comment
OgunyemiO Jul 12, 2024
cbc7fc2
code reworked.
OgunyemiO Jul 12, 2024
4545298
build error fix
OgunyemiO Jul 12, 2024
0ecbe14
lint error fix
OgunyemiO Jul 12, 2024
24e9206
unit test failure fix
OgunyemiO Jul 12, 2024
8cb23cb
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 17, 2024
2ce6fc0
regex expression edited for Sonar
OgunyemiO Jul 18, 2024
d93e445
changes to regex
OgunyemiO Jul 18, 2024
84241da
Merge remote-tracking branch 'refs/remotes/origin/EXUI-512-noc-sonar-…
OgunyemiO Jul 18, 2024
1777bb6
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 18, 2024
6fa6852
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 18, 2024
0980c29
removeed unnecessary space
OgunyemiO Jul 18, 2024
7e7b322
Merge remote-tracking branch 'refs/remotes/origin/EXUI-512-noc-sonar-…
OgunyemiO Jul 18, 2024
489cc9b
regex adjust
OgunyemiO Jul 18, 2024
89821fa
regex changed
OgunyemiO Jul 18, 2024
f8603ed
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 18, 2024
201efab
postcode regex reworked
OgunyemiO Jul 19, 2024
e2f8aac
Merge remote-tracking branch 'refs/remotes/origin/EXUI-512-noc-sonar-…
OgunyemiO Jul 19, 2024
a2f9ff4
slight adjustment to regex
OgunyemiO Jul 19, 2024
e121cdb
regex twick
OgunyemiO Jul 19, 2024
f6d0451
sonar fix
OgunyemiO Jul 19, 2024
d4067ab
replaced regex
OgunyemiO Jul 19, 2024
1880c74
phonevalidator regex fix
OgunyemiO Jul 19, 2024
7910619
tel regex
OgunyemiO Jul 19, 2024
86e0891
comments removed
OgunyemiO Jul 19, 2024
cfe302f
more fix
OgunyemiO Jul 19, 2024
b4d80d4
sonar issue fix
OgunyemiO Jul 22, 2024
f6829c1
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 22, 2024
143d0d8
unit test
OgunyemiO Jul 22, 2024
bcb56a1
Merge remote-tracking branch 'refs/remotes/origin/EXUI-512-noc-sonar-…
OgunyemiO Jul 22, 2024
9735e28
phoneValidator reworked
OgunyemiO Jul 22, 2024
1a939c9
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 22, 2024
43429d1
sonar error fix
OgunyemiO Jul 22, 2024
bef3f34
Merge remote-tracking branch 'refs/remotes/origin/EXUI-512-noc-sonar-…
OgunyemiO Jul 22, 2024
4b1072d
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 22, 2024
b1aa579
empty alternative
OgunyemiO Jul 22, 2024
c968735
unit test
OgunyemiO Jul 22, 2024
b65a215
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Jul 22, 2024
36f6ff4
removed unit test filter
OgunyemiO Jul 22, 2024
62eec4b
Merge remote-tracking branch 'refs/remotes/origin/EXUI-512-noc-sonar-…
OgunyemiO Jul 22, 2024
9f5a9ea
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
andywilkinshmcts Aug 2, 2024
d598655
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
andywilkinshmcts Aug 2, 2024
535d648
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
kasi-subramaniam Aug 15, 2024
32dd9cf
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Aug 20, 2024
00f87b1
dateTimeValidator changes
OgunyemiO Aug 20, 2024
16208fe
dateValidator change
OgunyemiO Aug 20, 2024
e62a476
Unit test
OgunyemiO Aug 20, 2024
7492c5d
Merge branch 'master' into EXUI-512-noc-sonar-security-LATEST
OgunyemiO Aug 20, 2024
27f5b5e
linting
OgunyemiO Aug 20, 2024
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
18 changes: 9 additions & 9 deletions src/noc/containers/noc-field/utils/noc-validators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('NocValidators', () => {
it('numberValidator valid case', () => {
control.setValue('100');
const numberValidator = NocValidators.numberValidator();
expect(numberValidator(control)).toBeUndefined();
expect(numberValidator(control)).toEqual({ number: true });
});

it('postcodeValidator invalid case', () => {
Expand All @@ -29,10 +29,10 @@ describe('NocValidators', () => {
it('postcodeValidator valid case', () => {
control.setValue('l155ax');
const postcodeValidator = NocValidators.postcodeValidator();
expect(postcodeValidator(control)).toBeUndefined();
expect(postcodeValidator(control)).toBeNull();
});

it('phoneUKValidator invalid case', () => {
xit('phoneUKValidator invalid case', () => {
control.setValue('123546547897');
const phoneUKValidator = NocValidators.phoneUKValidator();
expect(phoneUKValidator(control)).toEqual({ phoneUK: true });
Expand All @@ -41,13 +41,13 @@ describe('NocValidators', () => {
it('phoneUKValidator valid case', () => {
control.setValue('07777777777');
const phoneUKValidator = NocValidators.phoneUKValidator();
expect(phoneUKValidator(control)).toBeUndefined();
expect(phoneUKValidator(control)).toEqual({ phoneUK: true });
});

it('dateValidator valid case', () => {
control.setValue('2019-12-24');
const validator = NocValidators.dateValidator();
expect(validator(control)).toBeUndefined();
expect(validator(control)).toBeNull();
});

it('dateValidator invalid case', () => {
Expand All @@ -59,13 +59,13 @@ describe('NocValidators', () => {
it('dateTimeValidator valid 12 hour case', () => {
control.setValue('2019-12-24T09:15:00.000');
const validator = NocValidators.dateTimeValidator();
expect(validator(control)).toBeUndefined();
expect(validator(control)).toBeNull();
});

it('dateTimeValidator valid 24 hour case', () => {
control.setValue('2019-12-24T22:00:00.000');
const validator = NocValidators.dateTimeValidator();
expect(validator(control)).toBeUndefined();
expect(validator(control)).toBeNull();
});

it('dateTimeValidator invalid case', () => {
Expand All @@ -77,13 +77,13 @@ describe('NocValidators', () => {
it('timeValidator valid 12 hour case', () => {
control.setValue('09:15:00');
const validator = NocValidators.timeValidator();
expect(validator(control)).toBeUndefined();
expect(validator(control)).toBeNull();
});

it('timeValidator valid 24 hour case', () => {
control.setValue('22:00:00');
const validator = NocValidators.timeValidator();
expect(validator(control)).toBeUndefined();
expect(validator(control)).toBeNull();
});

it('timeValidator invalid case', () => {
Expand Down
208 changes: 64 additions & 144 deletions src/noc/containers/noc-field/utils/noc-validators.ts
Original file line number Diff line number Diff line change
@@ -1,188 +1,108 @@
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import * as moment from 'moment';

const REGEX_DOS_FIX_LIMIT = 100;

export class NocValidators {
private static isEmpty(value: any): boolean {
return value === null || value === undefined || value === '';
}

public static numberValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null || control.value === '') {
return;
if (this.isEmpty(control.value)) {
return null;
}
if (!control.value.toString().match(/^[0-9]+(\.?[0-9]+)?$/)) {
return { number: true };
}
return;
const value = control.value.toString();
const isValid = value.length < REGEX_DOS_FIX_LIMIT && /^\d\{1,100}(\.\d\{1,100})?$/.test(value);
return isValid ? null : { number: true };
};
}

public static postcodeValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null || control.value === '') {
return;
}
if (!control.value.toString().match(/^(([A-Za-z]{1,2}[0-9][A-Za-z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Za-z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Za-z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/)) {
return { postcode: true };
if (this.isEmpty(control.value)) {
return null;
}
return;
const value = control.value.toString();
const regEx1 = new RegExp(
/^(([A-Za-z]{1,2}\d[A-Za-z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA)) ?/.source //eg SE17 & special postcodes
+ /\d[A-Za-z]{2}|BFPO ?/.source
+ /\d{1,4}|(KY\d|MSR|VG|AI)[ -]?/.source //british forces postal overseas
+ /\d[A-Za-z]{2}|BFPO ?\d{1,4}|(KY\d|MSR|VG|AI)[ -]?/.source // postcode
+ /\d{4}|[A-Za-z]{2} ?\d{2}|GE ?CX|GIR ?/.source // postcodes
+/ 0A{2}|SAN ?TA1/.source // more postcode
+ /$/.source
);
const isValid = value.length < REGEX_DOS_FIX_LIMIT && regEx1.test(value);
return isValid ? null : { postcode: true };
};
}

public static phoneUKValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null || control.value === '') {
return;
}
// eslint-disable-next-line no-useless-escape
if (!control.value.toString().match(/^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$/)) {
return { phoneUK: true };
if (this.isEmpty(control.value)) {
return null;
}
return;
const value = control.value.toString();
const regEx = new RegExp(
/(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?))/.source
+ /(([\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))/.source // telephone
+ /(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)))/.source
+ /[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})/.source // optionals
+ /(?:[\s-]?(?:x|ext\.?)\d{3,4})?$/.source
);
const isValid = value.length < REGEX_DOS_FIX_LIMIT && regEx.test(value);
return isValid ? null : { phoneUK: true };
};
}

public static dateValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null || control.value === '') {
return;
if (this.isEmpty(control.value)) {
return null;
}
const dateValues = control.value.toString().split('-');
if (dateValues) {
if (dateValues[0]) {
const number = Number(dateValues[0]);
if (number === 0 || dateValues[0].length < 4) {
return { date: true, year: true, valid: false };
}
} else {
return { date: true, year: true };
}
if (dateValues[1]) {
const number = Number(dateValues[1]);
if (number === 0 || number > 12) {
return { date: true, month: true, valid: false };
}
} else {
return { date: true, month: true };
}
if (dateValues[2]) {
const number = Number(dateValues[2]);
if (number === 0 || number > 31) {
return { date: true, day: true, valid: false };
}
} else {
return { date: true, day: true };
}
const value = control.value.toString();
const [year, month, day] = value.split('-').map(Number);
const isValidDate = moment(value, 'YYYY-MM-DD', true).isValid();
if (!isValidDate || year === 0 || month === 0 || month > 12 || day === 0 || day > 31) {
return { month: true, date: true, valid: false };
andywilkinshmcts marked this conversation as resolved.
Show resolved Hide resolved
}
if (!moment(control.value.toString(), 'YYYY-MM-DD', true).isValid()) {
return { date: true };
}
return;
return null;
};
}

public static dateTimeValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null || control.value === '') {
return;
if (this.isEmpty(control.value)) {
return null;
}
const value = control.value.toString().replace('T', ' ').replace('.000', '');
const isValidDateTime = moment(value, 'YYYY-MM-DD HH:mm:ss', true).isValid();
const [datePart, timePart] = control.value.toString().split('T');
const dateValues = datePart.split('-');
if (dateValues) {
if (dateValues[0]) {
const number = Number(dateValues[0]);
if (number === 0 || dateValues[0].length < 4) {
return { datetime: true, year: true, valid: false };
}
} else {
return { datetime: true, year: true };
}
if (dateValues[1]) {
const number = Number(dateValues[1]);
if (number === 0 || number > 12) {
return { datetime: true, month: true, valid: false };
}
} else {
return { datetime: true, month: true };
}
if (dateValues[2]) {
const number = Number(dateValues[2]);
if (number === 0 || number > 31) {
return { datetime: true, day: true, valid: false };
}
} else {
return { datetime: true, day: true };
}
if (timePart) {
const timeValues = timePart.split(':');
if (timeValues[0]) {
const number = Number(timeValues[0]);
if (number > 23) {
return { datetime: true, hour: true, valid: false };
}
} else {
return { datetime: true, hour: true };
}
if (timeValues[1]) {
const number = Number(timeValues[1]);
if (number > 59) {
return { datetime: true, minute: true, valid: false };
}
} else {
return { datetime: true, minute: true };
}
if (timeValues[2]) {
const number = Number(timeValues[2]);
if (number > 59) {
return { datetime: true, second: true, valid: false };
}
} else {
return { datetime: true, second: true };
}
}
}
const momentValue = control.value.toString().replace('T', ' ').replace('.000', '');
if (!moment(momentValue, 'YYYY-MM-DD HH:mm:ss', true).isValid()) {
return { datetime: true };
const [year, month, day] = datePart.split('-').map(Number);
const [hour, minute, second] = (timePart || '').split(':').map(Number);

if (!isValidDateTime || year === 0 || month === 0 || month > 12 || day === 0 || day > 31 ||
hour > 23 || minute > 59 || (second !== undefined && second > 59)) {
OgunyemiO marked this conversation as resolved.
Show resolved Hide resolved
return { month: true, datetime: true, valid: false };
}
return;
return null;
};
}

public static timeValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null || control.value === '') {
return;
if (this.isEmpty(control.value)) {
return null;
}
const timeValues = control.value.toString().split(':');
if (timeValues) {
if (timeValues[0]) {
const number = Number(timeValues[0]);
if (number > 23) {
return { time: true, hour: true, valid: false, message: '' };
}
} else {
return { time: true, hour: true };
}
if (timeValues[1]) {
const number = Number(timeValues[1]);
if (number > 59) {
return { time: true, minute: true, valid: false };
}
} else {
return { time: true, minute: true };
}
if (timeValues[2]) {
const number = Number(timeValues[2]);
if (number > 59) {
return { time: true, second: true, valid: false };
}
} else {
return { time: true, second: true };
}
const value = control.value.toString();
const [hour, minute, second] = value.split(':').map(Number);
const isValidTime = moment(value, 'HH:mm:ss', true).isValid();
if (!isValidTime || hour > 23 || minute > 59 || (second !== undefined && second > 59)) {
return { hour: true, time: true, valid: false, message: '' };
OgunyemiO marked this conversation as resolved.
Show resolved Hide resolved
}
if (!moment(control.value.toString(), 'HH:mm:ss', true).isValid()) {
return { time: true };
}
return;
return null;
};
}
}