@@ -72,7 +72,7 @@ const checkCondition = (condition, fieldName) => {
7272 }
7373} ;
7474
75- const checkValidators = ( validate , fieldName , validatorTypes ) => {
75+ const checkValidators = ( validate , fieldName , validatorTypes , validatorMapper = { } ) => {
7676 if ( validate === undefined ) {
7777 return ;
7878 }
@@ -107,6 +107,10 @@ const checkValidators = (validate, fieldName, validatorTypes) => {
107107 Received "${ validator . type } ", expected one of: [${ validatorTypes } ].
108108 ` ) ;
109109 }
110+
111+ if ( validatorMapper . hasOwnProperty ( validator . type ) ) {
112+ validatorMapper [ validator . type ] ( validator , fieldName ) ;
113+ }
110114 }
111115 } ) ;
112116} ;
@@ -127,7 +131,7 @@ const checkDataType = (type, fieldName) => {
127131 }
128132} ;
129133
130- const checkActions = ( actions , name , actionTypes ) => {
134+ const checkActions = ( actions , name , actionTypes , actionsValidator = { } ) => {
131135 Object . keys ( actions ) . forEach ( ( prop ) => {
132136 if ( ! Array . isArray ( actions [ prop ] ) ) {
133137 throw new DefaultSchemaError ( `
@@ -152,13 +156,17 @@ const checkActions = (actions, name, actionTypes) => {
152156 Use one of them or define new action in the mapper.
153157 ` ) ;
154158 }
159+
160+ if ( actionsValidator . hasOwnProperty ( actions [ prop ] [ 0 ] ) ) {
161+ actionsValidator [ actions [ prop ] [ 0 ] ] ( actions [ prop ] , name ) ;
162+ }
155163 } ) ;
156164} ;
157165
158- const iterateOverFields = ( fields , componentMapper , validatorTypes , actionTypes , parent = { } ) => {
166+ const iterateOverFields = ( fields , componentMapper , validatorTypes , actionTypes , schemaValidatorMapper , parent = { } ) => {
159167 fields . forEach ( ( field ) => {
160168 if ( Array . isArray ( field ) ) {
161- return iterateOverFields ( field , componentMapper , validatorTypes ) ;
169+ return iterateOverFields ( field , componentMapper , validatorTypes , actionTypes , schemaValidatorMapper ) ;
162170 }
163171
164172 if ( parent . component !== componentTypes . WIZARD ) {
@@ -191,30 +199,34 @@ const iterateOverFields = (fields, componentMapper, validatorTypes, actionTypes,
191199 }
192200
193201 if ( field . hasOwnProperty ( 'validate' ) ) {
194- checkValidators ( field . validate , field . name , validatorTypes ) ;
202+ checkValidators ( field . validate , field . name , validatorTypes , schemaValidatorMapper . validators ) ;
195203 }
196204
197205 if ( field . hasOwnProperty ( 'dataType' ) ) {
198206 checkDataType ( field . dataType , field . name ) ;
199207 }
200208
201209 if ( field . hasOwnProperty ( 'fields' ) ) {
202- iterateOverFields ( field . fields , componentMapper , validatorTypes , actionTypes , field ) ;
210+ iterateOverFields ( field . fields , componentMapper , validatorTypes , actionTypes , schemaValidatorMapper , field ) ;
203211 }
204212
205213 if ( field . hasOwnProperty ( 'actions' ) ) {
206- checkActions ( field . actions , field . name , actionTypes ) ;
214+ checkActions ( field . actions , field . name , actionTypes , schemaValidatorMapper . actions ) ;
215+ }
216+
217+ if ( schemaValidatorMapper . components && schemaValidatorMapper . components . hasOwnProperty ( field . component ) ) {
218+ schemaValidatorMapper . components [ field . component ] ( field ) ;
207219 }
208220 } ) ;
209221} ;
210222
211- const defaultSchemaValidator = ( schema , componentMapper , validatorTypes = [ ] , actionTypes = [ ] ) => {
223+ const defaultSchemaValidator = ( schema , componentMapper , validatorTypes = [ ] , actionTypes = [ ] , schemaValidatorMapper = { } ) => {
212224 if ( Array . isArray ( schema ) || typeof schema !== 'object' ) {
213225 throw new DefaultSchemaError ( `Form Schema must be an object, received ${ Array . isArray ( schema ) ? 'array' : typeof schema } !` ) ;
214226 }
215227
216228 checkFieldsArray ( schema , 'schema' ) ;
217- iterateOverFields ( schema . fields , componentMapper , validatorTypes , actionTypes ) ;
229+ iterateOverFields ( schema . fields , componentMapper , validatorTypes , actionTypes , schemaValidatorMapper ) ;
218230} ;
219231
220232export default defaultSchemaValidator ;
0 commit comments