Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Yup validation for objects and object shape

const value = {
  books: [
    {
      info: {
        dateReleased: null,
        timeReleased: null,
      },
      reviewers: [
        {
          company: "",
          name: "",
        },
      ],
    },
  ],
};

const schema = yup.object().shape({
  books: yup.array(
    yup.object().shape({
      info: yup.object().shape({
        dateReleased: yup.date().required('Required'),                            
        timeReleased: yup.date().required('Required') 
      }),
      reviewer: yup.array(
        yup.object().shape({
          company: yup.string().required('Required'),                              
          name: yup.string().required('Required') 
        })
      )
    })
  ),
});

schema.validate(value).catch(err => {
  console.log(err.name); // ValidationError
  console.log(err.errors); // [books[0].info.timeReleased must be a `date` type, but the final value was: `Invalid Date`.]
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Yup #validation #objects #object #shape
ADD COMMENT
Topic
Name
8+8 =