Skip to content

Instantly share code, notes, and snippets.

@dgolant
Created November 7, 2017 15:27
Show Gist options
  • Save dgolant/ca3c4c66f529f3c15d7cee47dd0418f1 to your computer and use it in GitHub Desktop.
Save dgolant/ca3c4c66f529f3c15d7cee47dd0418f1 to your computer and use it in GitHub Desktop.
app.post(‘/user/links’, Celebrate({ 
body: Joi.object().keys({ 
important_value: Joi.string().required(), // look, type enforcement!
 data1: Joi.number().integer(),
 data2: Joi.string().default(‘admin’) // hey, and defaults!
 
}),
 query: { 
token: Joi.string().token().required() // you can use one object to 
  // validate body, query, 
  // and params all at once
 
}), (req, res) => { 
/* Here we do whatever the route is 
actually supposed to do, 
because Celebrate will automatically res.status(400).send() 
an informative message 
if the validation fails 
 */
 }); 
app.use(Celebrate.errors());   // taken and modified from the Celebrate docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment