hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Validate only one header

Open perfaram opened this issue 11 years ago • 13 comments

User Story

As a developer, I would like to check the value of a specific header, without having to handle them all. So that Hapi doesn't raise errors because of unhandled or ignored headers.

Example

When I create a route, with the following attribute :

config: {
    validate: {
        headers: {
            'accept-version': Joi.number().valid(1.0)
        }
    }
},

Hapi/Joi sends back the following response :

{
  "statusCode" : 400,
  "message" : "host is not allowed. content-type is not allowed. connection is not allowed. accept is not allowed. user-agent is not allowed. accept-language is not allowed. accept-encoding is not allowed",
  "error" : "Bad Request",
  "validation" : {
    "source" : "headers",
    "keys" : [
      "host",
      "content-type",
      "connection",
      "accept",
      "user-agent",
      "accept-language",
      "accept-encoding"
    ]
  }
}

I would not to have to handle any possible HTTP header.

Request

Is there already such a thing in Hapi ? If not, wouldn't it be a good thing ?

perfaram avatar Feb 13 '15 07:02 perfaram

You probably want

config: {
    validate: {
        headers: Joi.object({
            'accept-version': Joi.number().valid(1.0).required()
        }).options({ allowUnknown: true })
    }
},

Marsup avatar Feb 13 '15 08:02 Marsup

Yup, exactly. Thanks ! Couldn't we also write this in the docs ?

perfaram avatar Feb 13 '15 08:02 perfaram

It's all in the docs, hapi accepts Joi objects as validation, Joi objects can do that, so hapi can. Anyway try to find a place where you'd want it to go in the docs and send a pull request for that.

Marsup avatar Feb 13 '15 08:02 Marsup

Didn't see this in Joi docs, but I must have missed it. Anyway, thanks !

perfaram avatar Feb 13 '15 08:02 perfaram

There's no example with this specific property but it's mentioned in the validate part. If you have a good idea to expose it in the docs we'll obviously take it.

Marsup avatar Feb 13 '15 08:02 Marsup

No, no idea, sorry

perfaram avatar Feb 13 '15 08:02 perfaram

thanks @Marsup for the tip :+1:

adityasaxena avatar Sep 22 '15 20:09 adityasaxena

How do you extend the headers set by the security: true option, I don't want to mess the rest of the validation up but I want to add x-csrf-token to the list of validations.

davemackintosh avatar Nov 05 '15 14:11 davemackintosh

+1 for the tip, though it's counter intuitive.

For query/params/payload, we can address specific keys:

validate: {
  payload: {
    username: Joi.string().required()
  },
  params: {
    id: Joi.number().required()
  },
  query: {
    search: Joi.string().optional()
  }
}

But for headers, we can't do this:

validate: {
  headers: {
    example: Joi.string().required()
  }
}

But in fact have to do this:

validate: {
  headers: Joi.object({
    'example': Joi.string().required()
  }).options({ allowUnknown: true })
}

adamkdean avatar Jan 21 '17 16:01 adamkdean

@adamkdean It's not working

ericching-heracles avatar Jul 01 '19 12:07 ericching-heracles

Sorry @ericching-heracles this was quite a while ago, I don't recall the specifics anymore

adamkdean avatar Jul 01 '19 13:07 adamkdean

@adamkdean It's ok, I was miss using it.

ericching-heracles avatar Jul 01 '19 13:07 ericching-heracles

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

lock[bot] avatar Jan 09 '20 09:01 lock[bot]