11import _ from "lodash" ;
2- import {
3- ValidationPlugin ,
4- ValidationPluginConfig ,
5- ValidationPluginConstructor ,
6- ValidationPluginInterface ,
7- } from "../models/ValidatorInterface" ;
8-
9- /**
10- Declarative Validation Rules
11-
12- const plugins = {
13- dvr: dvr({
14- package: validatorjs,
15- extend: callback,
16- }),
17- };
18-
19- */
20- export class DVR implements ValidationPluginInterface {
21- promises = [ ] ;
22-
23- config = null ;
24-
25- state = null ;
26-
27- extend = null ;
28-
29- validator = null ;
2+ import FieldInterface from "src/models/FieldInterface" ;
3+ import FormInterface from "src/models/FormInterface" ;
4+ import { ValidationPlugin , ValidationPluginConfig , ValidationPluginConstructor , ValidationPluginInterface } from "src/models/ValidatorInterface" ;
5+
6+ export class DVR < TValidator = any > implements ValidationPluginInterface < TValidator > {
7+ promises : Promise < any > [ ] ;
8+ config : any ;
9+ state : any ;
10+ extend ?: ( args : { validator : TValidator ; form : FormInterface } ) => void ;
11+ validator : TValidator ;
12+ schema ?: any ;
3013
3114 constructor ( {
3215 config,
3316 state = null ,
3417 promises = [ ] ,
35- } : ValidationPluginConstructor ) {
18+ } : ValidationPluginConstructor < TValidator > ) {
3619 this . state = state ;
3720 this . promises = promises ;
3821 this . extend = config ?. extend ;
@@ -41,23 +24,21 @@ export class DVR implements ValidationPluginInterface {
4124 }
4225
4326 extendValidator ( ) {
44- // extend using "extend" callback
45- if ( typeof this . extend === 'function' ) {
27+ if ( typeof this . extend === "function" ) {
4628 this . extend ( {
4729 validator : this . validator ,
4830 form : this . state . form ,
4931 } ) ;
5032 }
5133 }
5234
53- validate ( field ) {
54- // get form fields data
35+ validate ( field : FieldInterface ) {
5536 const data = this . state . form . validatedValues ;
5637 this . validateFieldAsync ( field , data ) ;
5738 this . validateFieldSync ( field , data ) ;
5839 }
5940
60- makeLabels ( validation , field ) {
41+ makeLabels ( validation : any , field : FieldInterface ) {
6142 const labels = { [ field . path ] : field . label } ;
6243 _ . forIn ( validation . rules [ field . path ] , ( rule ) => {
6344 if (
@@ -85,34 +66,24 @@ export class DVR implements ValidationPluginInterface {
8566 validation . setAttributeNames ( labels ) ;
8667 }
8768
88- validateFieldSync ( field , data ) {
69+ validateFieldSync ( field : FieldInterface , data : any ) {
8970 const $rules = this . rules ( field . rules , "sync" ) ;
90- // exit if no rules found
9171 if ( _ . isEmpty ( $rules [ 0 ] ) ) return ;
92- // get field rules
9372 const rules = { [ field . path ] : $rules } ;
94- // create the validator instance
95- const validation = new this . validator ( data , rules ) ;
96- // set label into errors messages instead key
73+ const validation = new ( this . validator as any ) ( data , rules ) ;
9774 this . makeLabels ( validation , field ) ;
98- // check validation
9975 if ( validation . passes ( ) ) return ;
100- // the validation is failed, set the field error
10176 field . invalidate ( _ . head ( validation . errors . get ( field . path ) ) , false ) ;
10277 }
10378
104- validateFieldAsync ( field , data ) {
79+ validateFieldAsync ( field : FieldInterface , data : any ) {
10580 const $rules = this . rules ( field . rules , "async" ) ;
106- // exit if no rules found
10781 if ( _ . isEmpty ( $rules [ 0 ] ) ) return ;
108- // get field rules
10982 const rules = { [ field . path ] : $rules } ;
110- // create the validator instance
111- const validation = new this . validator ( data , rules ) ;
112- // set label into errors messages instead key
83+ const validation = new ( this . validator as any ) ( data , rules ) ;
11384 this . makeLabels ( validation , field ) ;
11485
115- const $p = new Promise ( ( resolve ) =>
86+ const $p = new Promise ( ( resolve : any ) =>
11687 validation . checkAsync (
11788 ( ) => this . handleAsyncPasses ( field , resolve ) ,
11889 ( ) => this . handleAsyncFails ( field , validation , resolve )
@@ -122,36 +93,40 @@ export class DVR implements ValidationPluginInterface {
12293 this . promises . push ( $p ) ;
12394 }
12495
125- handleAsyncPasses ( field , resolve ) {
96+ handleAsyncPasses ( field : FieldInterface , resolve : ( ) => void ) {
12697 field . setValidationAsyncData ( true ) ;
12798 resolve ( ) ;
12899 }
129100
130- handleAsyncFails ( field , validation , resolve ) {
131- field . setValidationAsyncData ( false , _ . head ( validation . errors . get ( field . path ) ) ) ;
101+ handleAsyncFails ( field : FieldInterface , validation : any , resolve : ( ) => void ) {
102+ field . setValidationAsyncData (
103+ false ,
104+ _ . head ( validation . errors . get ( field . path ) )
105+ ) ;
132106 this . executeAsyncValidation ( field ) ;
133107 resolve ( ) ;
134108 }
135109
136- executeAsyncValidation ( field ) {
110+ executeAsyncValidation ( field : FieldInterface ) {
137111 if ( field . validationAsyncData . valid === false ) {
138112 field . invalidate ( field . validationAsyncData . message , false , true ) ;
139113 }
140114 }
141115
142- rules ( rules , type ) {
116+ rules ( rules : any , type : "sync" | "async" ) {
143117 const $rules = _ . isString ( rules ) ? _ . split ( rules , "|" ) : rules ;
144- // eslint-disable-next-line new-cap
145- const v = new this . validator ( ) ;
118+ const v = new ( this . validator as any ) ( ) ;
146119 return _ . filter ( $rules , ( $rule ) =>
147120 type === "async"
148- ? v . getRule ( _ . split ( $rule , ":" ) [ 0 ] ) . async
149- : ! v . getRule ( _ . split ( $rule , ":" ) [ 0 ] ) . async
121+ ? v . getRule ( _ . split ( $rule , ":" ) [ 0 ] ) ? .async
122+ : ! v . getRule ( _ . split ( $rule , ":" ) [ 0 ] ) ? .async
150123 ) ;
151124 }
152125}
153126
154- export default ( config ?: ValidationPluginConfig ) : ValidationPlugin => ( {
155- class : DVR ,
156- config,
157- } ) ;
127+ export default < TValidator = any > (
128+ config ?: ValidationPluginConfig < TValidator >
129+ ) : ValidationPlugin < TValidator > => ( {
130+ class : DVR < TValidator > ,
131+ config,
132+ } ) ;
0 commit comments