11// import update from 'immutability-helper';
22import forOwn from 'lodash/forOwn' ;
33import mapValues from 'lodash/mapValues' ;
4+ import flatten from 'lodash/flatten' ;
5+ import difference from 'lodash/difference' ;
46import rules from './rules' ;
57
6- const validationResult = ( schema , uiSchema , value ) => {
8+ const isSubset = ( source , target ) => ! difference ( flatten ( source ) , flatten ( target ) ) . length ;
9+ const validationResult = ( schema , uiSchema , value , customValidations ) => {
710 const rv = [ ] ;
811 forOwn ( rules , ( rule , ruleId ) => {
912 const result = rule ( schema , uiSchema , value ) ;
@@ -14,20 +17,39 @@ const validationResult = (schema, uiSchema, value) => {
1417 } ) ;
1518 }
1619 } ) ;
20+ if (
21+ uiSchema
22+ && uiSchema [ 'ui:validations' ]
23+ && customValidations
24+ && isSubset (
25+ Object . keys ( uiSchema [ 'ui:validations' ] ) ,
26+ Object . keys ( customValidations ) ,
27+ )
28+ ) {
29+ forOwn ( customValidations , ( rule , ruleId ) => {
30+ const result = rule ( schema , uiSchema , value ) ;
31+ if ( result ) {
32+ rv . push ( {
33+ rule : ruleId ,
34+ ...result ,
35+ } ) ;
36+ }
37+ } ) ;
38+ }
1739 return rv ;
1840} ;
1941
20- const getFieldSpec = ( schema , uiSchema , value ) => {
42+ const getFieldSpec = ( schema , uiSchema , value , customValidations ) => {
2143 if ( value === null ) {
2244 return [ ] ;
2345 }
2446 if ( typeof value === 'number' || typeof value === 'string' ) {
25- return validationResult ( schema , uiSchema , value ) ;
47+ return validationResult ( schema , uiSchema , value , customValidations ) ;
2648 }
27- return mapValues ( schema . properties , ( s , p ) => getFieldSpec ( s , uiSchema [ p ] , value [ p ] ) ) ;
49+ return mapValues ( schema . properties , ( s , p ) => getFieldSpec ( s , uiSchema [ p ] , value [ p ] , customValidations ) ) ;
2850} ;
2951
30- export default ( schema , uiSchema , data ) => {
31- const spec = getFieldSpec ( schema , uiSchema , data ) ;
52+ export default ( schema , uiSchema , data , customValidations ) => {
53+ const spec = getFieldSpec ( schema , uiSchema , data , customValidations ) ;
3254 return { ...spec } ;
3355} ;
0 commit comments