Last active
August 3, 2016 10:11
-
-
Save aymanosman/1ebde70a8a1e4908c9577efc756b7588 to your computer and use it in GitHub Desktop.
Validate ObjectIds with Joi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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