Yup validation schema for nested object

2k views Asked by At

Hello I have this json object:

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
}

This is the current validation schema I tried to do:

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)
                        })
                    )

            })
        })
});

Can't seem to make it work. I'll be very happy if I could make this work.

The validations I need are: Project names are required (can't be null) Task names are required (can't be null) The hours of the timesheet days are 0-24, numbers.

That's all I need.

0

There are 0 answers