Skip to content

Latest commit

 

History

History
60 lines (38 loc) · 2.19 KB

README.md

File metadata and controls

60 lines (38 loc) · 2.19 KB

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