import * as Promise from 'bluebird' ; import { ISpec , ISchema } from '../specs' ; import { IValidatorConfig } from '../validator-config' ; import { loadSchema } from '../helpers/loader' ; export function extendAllAllOfs ( schema , config , spec ) { if ( ! schema . allOf || ! schema . allOf . length || schema . allOfResolved ) { return Promise . resolve ( schema ) ; } let parentPromises = [ ] ; schema . allOf . filter ( el => ! el . allOfResolved ) . forEach ( ( parentObject ) => { let parentPromise ; if ( parentObject . $ref ) { parentPromise = loadSchema ( parentObject , spec , config ) ; } else { parentPromise = Promise . resolve ( parentObject ) ; } parentPromise . then ( parentObject => extendAllAllOfs ( parentObject , config , spec ) ) . then ( parentObject => { return extend ( parentObject , schema ) ; } ) ; parentPromises . push ( parentPromise ) ; } ) ; return Promise . all ( parentPromises ) . then ( parents => { let result = parents . reduce ( ( a , b ) => { return extend ( b , a ) ; } , schema ) ; return result ; } ) ; } function extend ( from , to ) { if ( from . required ) { if ( to . required ) { to . required = < [ string ] > to . required . concat ( from . required . filter ( item => { return to . required . indexOf ( item ) === - 1 ; } ) ) ; } else { to . required = from . required ; } } for ( let propertyName in from . properties ) { if ( from . properties . hasOwnProperty ( propertyName ) ) { let property = from . properties [ propertyName ] ; if ( ! to . properties ) { to . properties = { } ; } if ( ! to . properties [ propertyName ] ) { to . properties [ propertyName ] = property ; } } } to . allOfResolved = true ; return to ; }