嵌套对象的Yup验证架构

时间:2020-10-04 19:14:18

标签: javascript yup

你好,我有这个json对象:

timesheet{
  "id": 0,
  "from": "2020-10-04",
  "to": "2020-10-04",
  "statusType": "OPEN",
  "activities": [
    {
      "id": 0,
      "project": {
        "id": 0,
        "name": "string"
      },
      "task": {
        "id": 0,
        "name": "string"
      },
      "timesheetDays": [
        {
          "id": 0,
          "date": "2020-10-04T19:08:33.542Z",
          "hours": 0
        }
      ]
    }
  ],
  "total": 0
}

这是我尝试执行的当前验证方案:

const schema = Yup.object({
    timesheet:
        Yup.object({
            activities: Yup.array().of({
                project: Yup.object({
                    name: Yup.string().required("Project must be selected!"),
                }),
                task: Yup.object({
                    name: Yup.string().required("Task must be selected!"),
                }),
                timesheetDays: Yup.array()
                    .of(
                        Yup.object({
                            hours: Yup.number().positive().min(0).max(24)
                        })
                    )

            })
        })
});

似乎无法使其正常工作。如果我能完成这项工作,我将非常高兴。

我需要的验证是: 项目名称为必填项(不能为null) 任务名称为必填项(不能为null) 时间表工作日的小时数是0-24(数字)。

这就是我所需要的。

0 个答案:

没有答案