-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Validation): add domain validator (#1366)
- Loading branch information
1 parent
ed2bfd9
commit 9ade135
Showing
8 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
terminus-ui/validators/src/validators/domain/domain.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |