-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,7 @@ npm i constraint-validator --save | |
|
||
## Basic usage | ||
```javascript | ||
import { | ||
Form, | ||
NotBlank, | ||
Email, | ||
Length | ||
} from 'constraint-validator'; | ||
import { Form, NotBlank, Email, Length } from 'constraint-validator'; | ||
|
||
const form = new Form(); | ||
|
||
|
@@ -57,6 +52,66 @@ In case of form data is not valid the ```errors``` object contains properties (r | |
] | ||
} | ||
``` | ||
## Data transformers | ||
Data transformers are used to translate the data for a field into a other format and back. The data transformers | ||
act as middleware and will be executed in the same order as they were applied. | ||
|
||
There are 2 types of data transformers: | ||
- **transformer** - executes before validation process | ||
- **reverseTransformers** - executes after validation process | ||
|
||
#### Form data transformers | ||
```javascript | ||
import { Form, NotBlank, Email } from 'constraint-validator'; | ||
|
||
const form = new Form(); | ||
|
||
form | ||
.add('email', [ | ||
new NotBlank(), | ||
new Email(), | ||
]) | ||
// next transformers will be applied to the form data | ||
.addTransformer(data => { | ||
data.email += '@example.com' | ||
|
||
return data; | ||
}) | ||
.addReverseTransformer(data => { | ||
data.email = data.email.replace(/@example.com/, '@example.me'); | ||
|
||
return data; | ||
}); | ||
|
||
form.validate({email: 'email'}); | ||
|
||
console.log(form.getData()); | ||
// Output: | ||
// {"email": "[email protected]"} | ||
``` | ||
|
||
#### Field data transformers | ||
```javascript | ||
import { Form, NotBlank, Email } from 'constraint-validator'; | ||
|
||
const form = new Form(); | ||
|
||
form | ||
.add('email', [ | ||
new NotBlank(), | ||
new Email(), | ||
]) | ||
.get('email') | ||
// next transformers will be applied to the 'email' field only | ||
.addTransformer(value => value + '@example.com') | ||
.addReverseTransformer(value => value.replace(/@example.com/, '@example.me')); | ||
|
||
form.validate({email: 'email'}); | ||
|
||
console.log(form.getData()); | ||
// Output: | ||
// {"email": "[email protected]"} | ||
``` | ||
|
||
|
||
## Documentation | ||
|