import Immutable from "immutable" ; import jsonschema from "jsonschema" ; import jsonSchemaDefaults from "json-schema-defaults" ; import Promise from "../utils/promise" ; import error from "../utils/error" ; import mergeDefaults from "../utils/mergeDefaults" ; param { Book } param { Plugin } return { Book } function validatePluginConfig ( book , plugin ) { let config = book . getConfig ( ) ; const packageInfos = plugin . getPackage ( ) ; const configKey = [ "pluginsConfig" , plugin . getName ( ) ] . join ( "." ) ; let pluginConfig = config . getValue ( configKey , { } ) . toJS ( ) ; const schema = ( packageInfos . get ( "gitbook" ) || Immutable . Map ( ) ) . toJS ( ) ; if ( ! schema ) return book ; schema . id = `/${ configKey }` ; schema . type = "object" ; const v = new jsonschema . Validator ( ) ; const result = v . validate ( pluginConfig , schema , { propertyName : configKey , } ) ; if ( result . errors . length > 0 ) { throw new error . ConfigurationError ( new Error ( result . errors [ 0 ] . stack ) ) ; } const defaults = jsonSchemaDefaults ( schema ) ; pluginConfig = mergeDefaults ( pluginConfig , defaults ) ; config = config . setValue ( configKey , pluginConfig ) ; return book . set ( "config" , config ) ; } param { Book } param { OrderedMap < String return { Promise < Book > } function validateConfig ( book , plugins ) { return Promise . reduce ( plugins , ( newBook , plugin ) => { return validatePluginConfig ( newBook , plugin ) ; } , book ) ; } export default validateConfig ;