Skip to content

Commit

Permalink
feat(Validation): add domain validator (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
atlwendy authored and benjamincharity committed Feb 19, 2019
1 parent ed2bfd9 commit 9ade135
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ for each feature.
| Name | Purpose | Status |
|------------------|---------------------------------------------------------------|:------------------:|
| `creditCard` | A credit card number must be valid | :white_check_mark: |
| `domain` | A domain must be valid | :white_check_mark: |
| `email` | An email address must be valid | :white_check_mark: |
| `equalToControl` | A control's value must be equal to another control's value | :white_check_mark: |
| `greaterThan` | A number must be greater than another value | :white_check_mark: |
Expand Down
29 changes: 29 additions & 0 deletions demo/app/components/validation/validation.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,35 @@ <h3 tsCardTitle>Url</h3>
></ts-input>
</ts-card>

<ts-card tsVerticalSpacing>
<h3 tsCardTitle>Domain</h3>

<div fxLayout="row" [fxLayoutGap]="flexGap">
<pre fxFlex>
// Valid domains:
foo.com
foo.com/blah_blah_(wikipedia)
www.example.com/foo/?bar=baz&inga=42&quux
userid:password@example.com:8080/
⌘.ws
oo.bar/baz
foo.bar/?q=Test%20URL-encoded%20stuff
223.255.255.254
</pre>

<pre fxFlex>
// Invalid domains:
http://
http://foo.bar?q=Spaces should be encoded
rdar://1234
1.1.1.1.1
.www.foo.bar./
shouldfail.com
</pre>
</div>

<ts-input [formControl]="myForm.get('domain')" label="Domain"></ts-input>
</ts-card>

<ts-card tsVerticalSpacing>
<h3 tsCardTitle tsVerticalSpacing>
Expand Down
4 changes: 4 additions & 0 deletions demo/app/components/validation/validation.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class ValidationComponent implements OnInit {
null,
[this.validatorsService.url()],
],
domain: [
null,
[this.validatorsService.domain()],
],
compare1: [
null,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class TsValidationMessagesService {
notUnique: `${validatorValue.actual} has already been selected.`,
noResults: `No results found.`,
url: `'${validatorValue.actual}' must be a valid URL.`,
domain: `'${validatorValue.actual}' must be a valid domain`,
equalToControl: `'${validatorValue.actual}' must be equal to '${validatorValue.compareValue}'`,
lowercase: `Must contain at least ${validatorValue.lowercase} lowercase letters`,
uppercase: `Must contain at least ${validatorValue.uppercase} uppercase letters`,
Expand Down
1 change: 1 addition & 0 deletions terminus-ui/validators/src/validators.service.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class MyComponent {
|------------------|---------------------------------------------------------------|
| `creditCard` | A credit card number must be valid |
| `email` | An email address must be valid |
| `domain` | A domain must be valid |
| `equalToControl` | A control's value must be equal to another control's value |
| `greaterThan` | A number must be greater than another value |
| `inCollection` | A value must be found in a collection |
Expand Down
2 changes: 2 additions & 0 deletions terminus-ui/validators/src/validators.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';

import { creditCardValidator } from './validators/creditCard/creditCard';
import { domainValidator } from './validators/domain/domain';
import { emailValidator } from './validators/email/email';
import { equalToControlValidator } from './validators/equalToControl/equalToControl';
import { greaterThanValidator } from './validators/greaterThan/greaterThan';
Expand All @@ -22,6 +23,7 @@ import { urlValidator } from './validators/url/url';
@Injectable()
export class TsValidatorsService {
creditCard = creditCardValidator;
domain = domainValidator;
email = emailValidator;
equalToControl = equalToControlValidator;
greaterThan = greaterThanValidator;
Expand Down
64 changes: 64 additions & 0 deletions terminus-ui/validators/src/validators/domain/domain.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// tslint:disable: no-non-null-assertion
import {
AbstractControl,
FormControl,
ValidatorFn,
} from '@angular/forms';

import { domainValidator } from './domain';


describe(`domainValidator`, function() {
let validatorFn: ValidatorFn;
let nullControl: AbstractControl;

beforeEach(() => {
validatorFn = domainValidator();
nullControl = new FormControl(null);
});


describe(`if the control doesn't exist`, () => {

test(`should return null`, () => {
expect(validatorFn(nullControl)).toEqual(null);
});

});


describe(`if the control has no value`, () => {

test(`should return null`, () => {
expect(validatorFn({} as any)).toEqual(null);
});

});


describe(`if the domain is valid`, () => {

test(`should return null`, () => {
const control = new FormControl('foo.com/blah_blah');

expect(validatorFn(control)).toEqual(null);
});

});


describe(`if the domain is NOT valid`, () => {

test(`should return the invalid response`, () => {
const control = new FormControl(' shouldfail.com');
const expected = {
valid: false,
actual: ' shouldfail.com',
};

expect(validatorFn(control)!.domain).toEqual(expected);
});

});

});
31 changes: 31 additions & 0 deletions terminus-ui/validators/src/validators/domain/domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
AbstractControl,
ValidationErrors,
ValidatorFn,
} from '@angular/forms';

import { urlOptionalProtocolRegex } from '@terminus/ngx-tools/regex';


/**
* Return a validator function to verify that a domain is valid
*
* @return The validator function
*/
export function domainValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
// Allow optional controls by not validating empty values
if (!control || !control.value) {
return null;
}

const invalidResponse: ValidationErrors = {
domain: {
valid: false,
actual: control.value,
},
};

return urlOptionalProtocolRegex.test(control.value) ? null : invalidResponse;
};
}

0 comments on commit 9ade135

Please sign in to comment.