从模式验证内部数组

时间:2020-08-10 00:46:04

标签: node.js schema joi

您好,我正在尝试使用joi验证下一个架构:

{
    "user": {
    "firstName": "Pablo",
    "lastName": "Neruda",
    "gender": "M",
    "age": 24,
    "userName": "test3",
    "password": "test2",
    "address": {
        "streetAddress": "126",
        "city": "San Jone",
        "state": "CA",
        "postalCode": "394221"
    },
    "phoneNumbers": [
        { "type": "home", "number": "7383627627" }
    ]
  }
}

我正在使用下一个代码:

function validateUser(user) {
  const JoiSchema = Joi.object({
    userName: Joi.string().min(5).max(30).required(),
    password: Joi.string().min(5).max(30).required(),
    firstName: Joi.string().min(5).max(30).required(),
    lastName: Joi.string().min(5).max(30).required(),
    gender: Joi.string().min(1).max(1).required(),
    age: Joi.number().integer().required(),
    email: Joi.string().email().min(5).max(50).optional(),
    address: Joi.object({
      streetAddress: Joi.string().required(),
      city: Joi.string().required(),
      state: Joi.string().required(),
      postalCode: Joi.string().required()
    })
  }).options({ abortEarly: false });

  return JoiSchema.validate(user);
}

我怀疑什么是验证phoneNumbers数组的最佳方法。

谢谢!

0 个答案:

没有答案