Hapi: Validation - Allow other unused fields

Created on 8 Jan 2014  Â·  10Comments  Â·  Source: hapijs/hapi

Here I set a few validation rules:

exports.post = {
  validate : {
    payload : {
      title : Hapi.types.String().required(),
      description : Hapi.types.String().required(),
      name : Hapi.types.String().required(),
      url : Hapi.types.String(),
      thumbnailUrl : Hapi.types.String()
    }
  },
  handler : function (request) {
    ...
  }
};

Currently I can either allow everything or just the fields in the validation. What if I don't mind what additional fields are passed in as long as the fields I want are supplied?

If anything other than these values gets passed by the client, the validation will fail. Is there a way to ask HAPI to ignore additional parameters in payload?

support

Most helpful comment

Know this is an old issue but I came across it when trying to solve the same thing. For future reference, I accomplished it by setting the route's validation.options.allowUnknown to true. For example:

exports.post = {
  validate : {
    options: {
      allowUnknown: true
    },
    payload : {
      title : Hapi.types.String().required(),
      description : Hapi.types.String().required()
    }
  },
  handler : function (request) {
    ...
  }
};

All 10 comments

I'm not sure which version of Hapi/Joi you are using. See these docs for v2 of Joi :

https://github.com/spumko/joi#validatevalue-schema-options

allowUnknown - when true, allows object to contain unknown keys which are ignored. Defaults to false.

Yeah just saw that, how would I set that through HAPI route validation?

Look at validation under the Server Options
This object will be passed to Joi when validating. On top of the allowUnknown option you have the stripUnknown which removes the other fields completely.

Thanks,

Doesn't seem to do it for me

var serverOptions = { 
  cors: {
    origin: ['*'],
    headers : ['Content-Type', 'appid', 'token']
  },
  validation : {
    allowUnknown : true
  }
};

var server = Hapi.createServer('localhost', config.get('port'), serverOptions);

Using Hapi 1.20.0. Am I doing anything wrong?

Hum, I think this only applies to hapi 2.0.0
For hapi
Try to use the code from the master branch maybe?
Otherwise you may have to enable joi v2 and hope that the validation options are working the same way within hapi.

Yeah did that broke all other validations. I think I will pass on this. I'll keep open and see if @hueniverse has anything to add.

Thanks for your help.

You should be able to handle enabling joi 2 but if not, wait a week for 2.0.

Know this is an old issue but I came across it when trying to solve the same thing. For future reference, I accomplished it by setting the route's validation.options.allowUnknown to true. For example:

exports.post = {
  validate : {
    options: {
      allowUnknown: true
    },
    payload : {
      title : Hapi.types.String().required(),
      description : Hapi.types.String().required()
    }
  },
  handler : function (request) {
    ...
  }
};

Is there any way to set this just for one particular validation type. For example, I want to allow unknown properties in my headers, but I want to strip them in my payload. Seems if I set the validation.options.stripUnknown property to true it starts to break things related to headers.

Here is the schema I tried, with no luck:

validate: {
  headers: Joi.object({
    'authorization': Joi.string().required()
  }).unknown(),
  payload: {
    folderName: Joi.string().required(),
    parent: Joi.number()
  },
  options: {
    stripUnknown: true
  }
}

Don't use the validation options at the validate level. Use the joi options to set it per source.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arb picture arb  Â·  4Comments

mateeyow picture mateeyow  Â·  5Comments

AdriVanHoudt picture AdriVanHoudt  Â·  5Comments

leore picture leore  Â·  4Comments

leore picture leore  Â·  3Comments