Joi: How to get header and pass it as a userID

Created on 23 Aug 2017  路  3Comments  路  Source: sideway/joi

Hi, Can we get header(authorization) and insert it as a userId?

Here is my code

const ratingSchema = Joi.object().keys({
      ratings: Joi.number().precision(2),
      comment_added: Joi.date().timestamp('javascript').default(() => Date(new Date()), 'creation date'),
      commentId: Joi.number().integer().default(new Date().getTime().toString())    
});`


const schemas = {        
    createReviews: {
             headers: {
             authorization: Joi.string().required()
             },
            params: {
                Id: Joi.string().regex(/^[A-Fa-f0-9]{24}$/).required()
            },
            body: {
                comment_added: Joi.date().timestamp('javascript').default(() => Date(new Date()), 'creation date'),
                commentId: Joi.number().integer().default(new Date().getTime().toString()),
                userId:  ? (Here I want to take authorisation and it should auto insert. Can you please tell How can I do it?)
           }   
   }
};`

What did you expect ?

in userId I want to take authorization key and insert it as a userId. Please tell me How can I do it

support

Most helpful comment

.default(Joi.ref('$headers.authorization')) might work, or something similar.

All 3 comments

Assuming you use hapi you use the onPostAuth hook for that, I don't think Joi can do what you want it to do

can you please give an example using above code using hapi

.default(Joi.ref('$headers.authorization')) might work, or something similar.

Was this page helpful?
0 / 5 - 0 ratings