@terminus/ui-validators
v3.0.0
Published
<h1>Validators</h1>
Downloads
6
Keywords
Readme
A collection of form validators.
Table of Contents
NOTE: This service is provided as a singleton by defining the providedIn
property as root
.
Usage
import {
FormBuilder,
FormGroup,
} from '@angular/forms';
import { TsValidatorsService } from '@terminus/ui-validators';
@Component({
...
})
export class MyComponent {
// Create a form
myForm: FormGroup = this.formBuilder.group({
email: [
null,
[
// Basic validator
this.validatorsService.email(),
],
],
greaterThan: [
null,
[
// Validator with a required argument
this.validatorsService.greaterThan(10),
],
],
});
constructor(
private formBuilder: FormBuilder,
private validatorsService: TsValidatorsService,
) {}
}
Available validators
| Validator | Purpose |
|------------------|---------------------------------------------------------------|
| 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 |
| isInRange
| A number must be between two numbers |
| lessThan
| A number must be less than another value |
| lowercase
| A value must contain a minimum amount of lowercase characters |
| maxDate
| A date must be before a maximum date |
| minDate
| A date must be after a minimum date |
| numbers
| A value must contain a minimum amount of numbers |
| password
| A password must meet certain requirements |
| uppercase
| A value must contain a minimum amount of uppercase characters |
| url
| A URL must be valid |
Mocking
A mocked version of the service is available for testing as TsValidatorsServiceMock
from @terminus/ui-validators/testing
.
The mocks consist of Jest mock functions. Based on the class value isValid
the validator mock will
return an error message or null
.