Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping unknown keys when validating with Joi

I'm using Joi to validate a JavaScript object in the server. The schema is like the following:

var schema = Joi.object().keys({
    displayName: Joi.string().required(),
    email: Joi.string().email(),
    enabled: Joi.boolean().default(false, "Default as disabled")
}).unknown(false);

The schema above will report an error if there is an unknown key in the object, which is expected, but what I want is to strip all the unknown silently, without an error. Is it possible to be done?

like image 516
Rafael Maiolla Avatar asked Jul 28 '15 14:07

Rafael Maiolla


1 Answers

You need to use the stripUnknown option if you want to strip the unknown keys from the objects that you are validating.

cf options on https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback

like image 68
Jerome WAGNER Avatar answered Sep 30 '22 15:09

Jerome WAGNER