Skip to content

Instantly share code, notes, and snippets.

@aymanosman
Last active August 3, 2016 10:11
Show Gist options
  • Save aymanosman/1ebde70a8a1e4908c9577efc756b7588 to your computer and use it in GitHub Desktop.
Save aymanosman/1ebde70a8a1e4908c9577efc756b7588 to your computer and use it in GitHub Desktop.
Validate ObjectIds with Joi
m = require('mongojs')
J = require('joi')
x = J.string().hex().length(24)
i = m.ObjectId().toString()
// '57a1c20202ccc41cd8d84834'
x.validate(i)
// { error: null, value: '57a1c20202ccc41cd8d84834' }
x.validate("123").error.message
// '"value" length must be 24 characters long'
x.validate("x").error.message
// '"value" must only contain hexadecimal characters'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment