3.0.0 • Published 7 years ago

joi-extension-ipfs v3.0.0

Weekly downloads
18
License
ISC
Repository
github
Last release
7 years ago

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