joi-extension-ipfs

3.0.0 • Public • Published

joi-extension-ipfs Build Status dependencies Status

Joi validators for IPFS.

Example

const Joi = require('joi').extend(require('joi-extension-ipfs'))
const Assert = require('assert')
 
const schema = Joi.ipfs().path()
 
let result = schema.validate('/ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9')
Assert.ifError(result.error)
console.log(result.value) // /ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9

Strict mode

By default, joi-extension-ipfs will validate an IPFS path if it looks like it might be one. Pass strict: true to enable strict path validation.

const Joi = require('joi').extend(require('joi-extension-ipfs'))
const Assert = require('assert')
 
const strictSchema = Joi.ipfs().path({ strict: true })
const schema = Joi.ipfs().path()
 
// Multihash only
 
let result = strictSchema.validate('QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9')
Assert.ifError(result.error) // throws
 
result = schema.validate('QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9')
console.log(result.value) // /ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9
 
// Missing leading forward slash
 
result = strictSchema.validate('ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9')
Assert.ifError(result.error) // throws
 
result = schema.validate('ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9')
console.log(result.value) // /ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9
 
// Missing protocol
 
result = strictSchema.validate('QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9/foo.jpg')
Assert.ifError(result.error) // throws
 
result = schema.validate('QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9/foo.jpg')
console.log(result.value) // /ipfs/QmWPgGoxjsSazq94f3dvysj17d4pbebqamfMmVXn2DqkG9/foo.jpg

Validate ONLY a multihash?

Take a look at joi-extension-multihash.


js-standard-style

Package Sidebar

Install

npm i joi-extension-ipfs

Weekly Downloads

0

Version

3.0.0

License

ISC

Last publish

Collaborators

  • alanshaw