Skip to content

Commit

Permalink
fix: avoid using superstruct interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Aug 20, 2019
1 parent b0f124b commit aa95ab9
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@ const { struct, superstruct } = require('superstruct')
const { optional, list } = struct

// Define custom types
const s = superstruct()
const transport = s.union([
s.interface({
createListener: 'function',
dial: 'function'
}),
'function'
])
const s = superstruct({
types: {
transport: value => {
if (value.length === 0) return 'ERROR_EMPTY'
value.forEach(i => {
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
})
return true
},
protector: value => {
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
return true
}
}
})

const modulesSchema = s({
connEncryption: optional(list([s('object|function')])),
// this is hacky to simulate optional because interface doesnt work correctly with it
// change to optional when fixed upstream
connProtector: s.union(['undefined', s.interface({ protect: 'function' })]),
connProtector: s('undefined|protector'),
contentRouting: optional(list(['object'])),
dht: optional(s('null|function|object')),
pubsub: optional(s('null|function|object')),
peerDiscovery: optional(list([s('object|function')])),
peerRouting: optional(list(['object'])),
streamMuxer: optional(list([s('object|function')])),
transport: s.intersection([[transport], s.interface({
length (v) {
return v > 0 ? true : 'ERROR_EMPTY'
}
})])
transport: 'transport'
})

const configSchema = s({
Expand Down

0 comments on commit aa95ab9

Please sign in to comment.