22
33const Joi = require ( 'joi' )
44
5- const schema = Joi . object ( {
5+ const ModuleSchema = Joi . alternatives ( ) . try ( Joi . func ( ) , Joi . object ( ) )
6+
7+ const OptionsSchema = Joi . object ( {
68 // TODO: create proper validators for the generics
79 connectionManager : Joi . object ( ) ,
810 peerInfo : Joi . object ( ) . required ( ) ,
911 peerBook : Joi . object ( ) ,
1012 modules : Joi . object ( ) . keys ( {
11- transport : Joi . array ( ) . items (
12- Joi . alternatives ( ) . try (
13- Joi . func ( ) ,
14- Joi . object ( )
15- )
16- ) . min ( 1 ) . required ( ) ,
17- streamMuxer : Joi . array ( ) . items (
18- Joi . alternatives ( ) . try (
19- Joi . func ( ) ,
20- Joi . object ( )
21- )
22- ) . allow ( null ) ,
23- connEncryption : Joi . array ( ) . items (
24- Joi . alternatives ( ) . try (
25- Joi . func ( ) ,
26- Joi . object ( )
27- )
28- ) . allow ( null ) ,
29- peerDiscovery : Joi . array ( ) . items (
30- Joi . alternatives ( ) . try (
31- Joi . func ( ) ,
32- Joi . object ( )
33- )
34- ) . allow ( null ) ,
35- dht : Joi . alternatives ( ) . try (
36- Joi . func ( ) ,
37- Joi . object ( )
38- ) . allow ( null )
13+ transport : Joi . array ( ) . items ( ModuleSchema ) . min ( 1 ) . required ( ) ,
14+ streamMuxer : Joi . array ( ) . items ( ModuleSchema ) . allow ( null ) ,
15+ connEncryption : Joi . array ( ) . items ( ModuleSchema ) . allow ( null ) ,
16+ peerDiscovery : Joi . array ( ) . items ( ModuleSchema ) . allow ( null ) ,
17+ dht : ModuleSchema . allow ( null )
3918 } ) . required ( ) ,
4019 config : Joi . object ( ) . keys ( {
4120 peerDiscovery : Joi . object ( ) . allow ( null ) ,
@@ -57,27 +36,12 @@ const schema = Joi.object({
5736} )
5837
5938module . exports . validate = ( options ) => {
60- let newSchema = schema
61- // Throw an intial error early for required props
62- let config = Joi . attempt ( options , newSchema )
63-
64- // Ensure discoveries are properly configured
65- if ( config . modules . peerDiscovery ) {
66- config . modules . peerDiscovery . forEach ( ( discovery ) => {
67- // If it's a function, validate we have configs for it
68- if ( typeof discovery === 'function' ) {
69- Joi . reach ( schema , 'config.peerDiscovery' ) . keys ( {
70- [ discovery . tag ] : Joi . object ( ) . required ( )
71- } )
72- }
73- } )
74- }
39+ options = Joi . attempt ( options , OptionsSchema )
7540
7641 // Ensure dht is correct
77- if ( config . config . EXPERIMENTAL && config . config . EXPERIMENTAL . dht ) {
78- newSchema = newSchema . requiredKeys ( ' modules.dht' )
42+ if ( options . config . EXPERIMENTAL . dht ) {
43+ Joi . assert ( options . modules . dht , ModuleSchema . required ( ) )
7944 }
8045
81- // Finish validation and return the updated config
82- return Joi . attempt ( config , newSchema )
46+ return options
8347}
0 commit comments