3

我在 Joi 验证中遇到了一个问题(或者我认为是一个问题)。如果它作为请求正文的一部分传递,我正在尝试为不存在的键分配一个值。

例如:

parameters: Joi.object().keys({
  keyA: Joi.string().allow('').allow(null).default(null),
  keyB: Joi.object().keys({
    b1: Joi.string(),
    b2: Joi.string(),
    b3: Joi.object().keys({
      b3_1: Joi.string(),
      b3_2: Joi.string(),
      b3_3: Joi.string()
    })
  }).default({}),
  keyC: Joi.object().keys({
    c1: Joi.number(),
    c2: Joi.number(),
    c3: Joi.boolean(),
    c4: Joi.boolean()
  }).default({}),
  keyD: Joi.object().keys({
    d1: Joi.number(),
    d2: Joi.number()
  }).default({}),
  keyE: Joi.object().keys({
    e1: Joi.number()
  }).default({}) 
}).allow(null)

所以具体来说,如果我要通过:

{
  keyA: "foo",
  keyD: {
    d1: 21.9,
    d2: 21.1
  },
  keyE: {
    e1: 42
  }
}

我会得到这个作为回报

{
  keyA: "foo",
  keyB: {},
  keyC: {},
  keyD: {
    d1: 21.9,
    d2: 21.1
  },
  keyE: {
    e1: 42
  }
}

用 :eyes: 在空的物体上。Joi.default() 方法缺少什么?我是否过度扩展了 Joi 的用途?

4

2 回答 2

2

I'll start by pointing out that the schema in your question isn't valid JavaScript, you've closed a few too many brackets before declaring the rule for keyC. I'll assume this is simply a formatting error with the question and your actual schema is currently valid.

Secondly, there's nothing wrong with how you've declared your defaults.. it works just fine. I'll assume it's the way you're validating the schema which is the problem.

Try running this. I've mimicked the validation method in the docs for default().

const schema = Joi.object().keys({
    keyA: Joi.string().allow('').allow(null).default(null),
    keyB: Joi.object().keys({
        b1: Joi.string(),
        b2: Joi.string(),
        b3: Joi.object().keys({
            b3_1: Joi.string(),
            b3_2: Joi.string(),
            b3_3: Joi.string()
        })
    }).default({}),
    keyC: Joi.object().keys({
        c1: Joi.number(),
        c2: Joi.number(),
        c3: Joi.boolean(),
        c4: Joi.boolean()
    }).default({}),
    keyD: Joi.object().keys({
        d1: Joi.number(),
        d2: Joi.number()
    }).default({}),
    keyE: Joi.object().keys({
        e1: Joi.number()
    }).default({})
}).allow(null);

Joi.validate({
    keyA: "foo",
    keyD: {
        d1: 21.9,
        d2: 21.1
    },
    keyE: {
        e1: 42
    }
}, schema, (err, value) =>
{
    if (err)
        throw err;

    console.log(value);
});

I get this in the console:

{
    keyA :'foo',
    keyD: {
        d1: 21.9,
        d2: 21.1
    },
    keyE: {
        e1: 42
    },
    keyB: {},
    keyC: {}
}

The keys are unlikely to look ordered like your expected output, but that shouldn't matter as object keys are not ordered anyway.

于 2017-03-30T07:50:55.917 回答
-1

你可以设置默认值,你应该试试这个:

parameters: Joi.object().keys({
  keyA: Joi.string().allow(null).default(null),
  keyB: Joi.object().keys({
    b1: Joi.string().default("abc"),
      b2: Joi.string().default("abc"),
      b3: Joi.object().keys({
        b3_1: Joi.string().default("abc"),
        b3_2: Joi.string().default("abc"),
        b3_3: Joi.string().default("abc")
      })
    })
  }).default({}),
  keyC: Joi.object().keys({
    c1: Joi.number().default(0),
    c2: Joi.number().default(0),
    c3: Joi.boolean().default(0),
    c4: Joi.boolean().default(0)
  }).default({}),
  keyD: Joi.object().keys({
    d1: Joi.number().default(0),
    d2: Joi.number().default(0)
  }).default({}),
  keyE: Joi.object().keys({
    e1: Joi.number().default(0)
  }).default({}) 
}).allow(null)
于 2017-03-30T06:52:20.380 回答