-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
33 lines (29 loc) · 881 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const Multiaddr = require('multiaddr')
const mafmt = require('mafmt')
module.exports = {
name: 'multiaddr',
language: {
invalid: '{{message}}',
fmt: 'must be in {{fmt}} format'
},
pre (value, state, options) {
if (value == null) {
return this.createError('multiaddr.invalid', { v: value, message: 'addr must be a string, Buffer, or another Multiaddr' }, state, options)
}
let ma
try {
ma = Multiaddr(value)
} catch (err) {
return this.createError('multiaddr.invalid', { v: value, message: err.message }, state, options)
}
return options.convert ? ma : value
},
rules: Object.keys(mafmt).map(fmt => ({
name: fmt,
validate (params, value, state, options) {
return mafmt[fmt].matches(value)
? value
: this.createError(`multiaddr.fmt`, { v: value, fmt }, state, options)
}
}))
}