joi-multiaddr
v4.0.0
Published
Validate a multiaddr and common formats
Downloads
372
Maintainers
Readme
joi-multiaddr
Validate a multiaddr and common formats.
Install
npm install joi-multiaddr
Usage
const Joi = require('joi').extend(require('joi-multiaddr'))
/**
* Validate a String or Buffer is in multiaddr format:
*/
const schema = Joi.multiaddr()
const result = schema.validate('/ip4/0.0.7.6/tcp/1234')
console.log(result.error) // null
// Note, successful validation converts to a Multiaddr instance:
console.log(result.value) // <Multiaddr 04000007060604d2 - /ip4/0.0.7.6/tcp/1234>
/**
* Validate formats using mafmt (https://github.com/multiformats/js-mafmt):
*/
const dnsSchema = Joi.multiaddr().DNS()
const tcpSchema = Joi.multiaddr().TCP()
const ipfsSchema = Joi.multiaddr().IPFS()
/* etc. */
ipfsSchema.validate('/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4') // Success!
ipfsSchema.validate('/ip4/0.0.7.6/tcp/1234') // Failure :(
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw