Striping unknown keys when validating with Joi

元气小坏坏 提交于 2019-12-23 16:52:50

问题


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?


回答1:


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




回答2:


const joi = require('joi');

joi.validate(object, schema, {stripUnknown:true}, callback);


来源:https://stackoverflow.com/questions/31679374/striping-unknown-keys-when-validating-with-joi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!