Hapijs joi, just confirm one field and allow any field

I want to check one field and allow other fields without checking; for example, to check: field "firstname". In my code, when I comment on the "payload", hapi allows me to write any field, when I uncomment the "payload" hapijs, I don’t allow me to write any field, but I just want to check the "firstname" example as a "string" and let the rest of the field allow. I plan that the variable fields correspond to the database configuration, so I'm going to just check out some fixed fields and allow other variable fields to be saved that are managed in the interface and not in the backend

config: { validate: { /* payload: { firstname: Joi.string(), lastname: Joi.string() ...anothers fields... }*/ } } 

UPDATED: thanks to Robert C. Bell, I adapted the solution to add 'validate':

  config: { validate: { options: { allowUnknown: true }, payload: { firstname: Joi.string() } } } 
+5
source share
2 answers

Perhaps you are looking for the .unknown() method :

object.unknown([allow])

Overrides the processing of unknown keys only for the area of ​​the current object (does not apply to child elements), where:

  • allow - if false , unknown keys are not allowed, otherwise unknown keys are ignored.

js const schema = Joi.object({ a: Joi.any() }).unknown();

+2
source
  config: { validate: { payload: Joi.object({ 'firstname': Joi.string(), }).options({ allowUnknown: true }) } } 

Instead of adding validation fields to validation, check the payload directly using the Joi object. Taking the value allowUnknown true, using this, it checks only those fields that are mentioned in the Joi object.

0
source

All Articles