使用joi验证库删除未知的对象属性

时间:2019-02-18 17:10:47

标签: joi

我正在尝试使用joi验证库,并在docs中发现您可以使用stripUnknow选项从对象和数组中剥离未知属性。但这对我不起作用,这是我正在测试的示例代码

const schema = Joi.object({
  firstName: Joi.string()
    .trim()
    .min(3)
    .required(),
  lastName: Joi.string()
    .trim()
    .min(3)
    .required()
});

let data = {
  firstName: "some name",
  test: "test value"
};

let result = Joi.validate(data, schema, {
  stripUnknown: true
});
console.log(util.inspect(result, {depth: 3}));

我希望从Joi返回的值不包含“ test”,而是包含。

{ error: 
   { [ValidationError: child "lastName" fails because ["lastName" is required]]
     isJoi: true,
     name: 'ValidationError',
     details: 
      [ { message: '"lastName" is required',
          path: [Object],
          type: 'any.required',
          context: [Object] } ],
     _object: { firstName: 'some name', test: 'test value' },
     annotate: [Function] },
  value: { firstName: 'some name', test: 'test value' },
  then: [Function: then],
  catch: [Function: catch] } 

我也检查了此answer,但它不能按回答工作。

0 个答案:

没有答案