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

fix(url) Add localhost & fix IP validation #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 31 additions & 12 deletions src/url/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
import { FormControl, ValidatorFn } from '@angular/forms';

import { url } from './';
import { url } from './validator';

describe('Url', () => {
const VALIDATOR_ERROR = {url: true};
let control: FormControl;
let validator: ValidatorFn;

beforeEach(() => {
validator = url;
});

it('"http://www.test.com" should equal to "null"', () => {
control = new FormControl('http://www.test.com');
expect(validator(control)).toBeNull()
});
const validTestCases = [
"http://test.com",
"http://www.test.com",
"https://www.test.com",
"http://localhost",
"http://localhost:4200",
"http://192.168.0.1:8000",
"http://172.0.0.1",
"http://my.domain.com:9000/auth/sso/acs",
];

it('"https://www.test.com" should equal to "null"', () => {
control = new FormControl('https://www.test.com');
expect(validator(control)).toBeNull()
});
const invalidTestCases = [
"23a",
"test.com",
"1234://my.domain.com",
"http://my.domain.com/text with space",
];

it('"23a" should equal to "{url: true}"', () => {
control = new FormControl('23a');
expect(validator(control)).toEqual({url: true});

validTestCases.forEach(validURL => {
it(`"${validURL}" URL should equal to "null"`, async () => {
control = new FormControl(validURL);
expect(validator(control)).toBeNull();
});
});

invalidTestCases.forEach(invalidURL => {
it(`"${invalidURL}" should equal to "${VALIDATOR_ERROR}"`, async () => {
control = new FormControl(invalidURL);
expect(validator(control)).toEqual(VALIDATOR_ERROR);
});
})
});
23 changes: 17 additions & 6 deletions src/url/validator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { AbstractControl, Validators, ValidatorFn } from '@angular/forms';

import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { isPresent } from '../util/lang';

export const url: ValidatorFn = (control: AbstractControl): {[key: string]: boolean} => {
if (isPresent(Validators.required(control))) return null;
const PROTOCOL: RegExp = /(?:(?:(?:https?|ftp):)?\/\/)/;
const OPTIONAL_SUB_DOMAIN: RegExp = /(?:\S+(?::\S*)?@)?/;
const IPV4: RegExp = /(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})|(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4])))/;
const DOMAIN: RegExp = /(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?/
const LOCALHOST: RegExp = /localhost/;
const DOMAINS = new RegExp(`${OPTIONAL_SUB_DOMAIN.source}(?:${IPV4.source}|${DOMAIN.source}|${LOCALHOST.source})`);
const OPTIONAL_PORT: RegExp = /(?::\d{2,5})?/;
const OPTIONAL_PATH: RegExp = /(?:[/?#]\S*)?/;

export const url: ValidatorFn = (control: AbstractControl): ValidationErrors => {
if (isPresent(Validators.required(control))) {
return null;
}

let v: string = control.value;
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(v) ? null : {'url': true};
const url: string = control.value;
const URL_RE: RegExp = new RegExp(`^${PROTOCOL.source}${DOMAINS.source}${OPTIONAL_PORT.source}${OPTIONAL_PATH.source}$`, 'i');
return URL_RE.test(url) ? null : { 'url': true };
};