File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ import ProxyValidator from '../src/index' ;
2
+
3
+ const validators = {
4
+ // Define a set of rules to apply for each prop, each rule is formed by a key and a value.
5
+ name : {
6
+ // The key corresponds to a validator function.
7
+ isLength : {
8
+ // The value can be either an object, containing options and an errorMessage...
9
+ options : {
10
+ min : 6
11
+ } ,
12
+ errorMessage : 'Minimum length 6 characters.'
13
+ } ,
14
+ // ...or a boolean, in which case the message will be the default one.
15
+ isUppercase : true
16
+ } ,
17
+ mobile : {
18
+ isMobilePhone : {
19
+ options : 'en-GB' ,
20
+ errorMessage : 'Invalid mobile number.'
21
+ } ,
22
+ isAlphanumeric : true
23
+ }
24
+ } ;
25
+
26
+ const sanitizers = {
27
+ // As with the validation, the sanitizing schema is formed by a a key/value pair.
28
+ name : {
29
+ // The key corresponds with the sanitizing function
30
+ trim : true // and the value can be either boolean (use defaults)
31
+ } ,
32
+ email : {
33
+ normalizeEmail : {
34
+ options : { // ...or a config options object.
35
+ all_lowercase : true
36
+ }
37
+ }
38
+ }
39
+ } ;
40
+
41
+ const ContactValidator = ProxyValidator ( validators , sanitizers ) ;
42
+
43
+ // Creates a proxy that will enforce the validator rules.
44
+ const withValidationContact = ContactValidator ( ) ;
45
+
46
+ function proxyTestDrive ( name ) {
47
+ try {
48
+ withValidationContact . name = name ;
49
+ console . log ( 'success!' , withValidationContact ) ;
50
+ } catch ( e ) {
51
+ console . error ( 'Whoops!' , e . message ) ;
52
+ }
53
+ }
54
+ proxyTestDrive ( 'Mike' ) ;
55
+ //
56
+ proxyTestDrive ( ' MICHAEL' ) ;
57
+ // success! { name: 'MICHAEL' };
You can’t perform that action at this time.
0 commit comments