Search
 
SCRIPT & CODE EXAMPLE
 

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`.]
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: Get physical path in javascript 
Javascript :: Solana SPL Token JavaScript library mint function 
Javascript :: useHistory: useNavigate if you install v6 or more than react-router-dom": ^6.2.1 
Javascript :: koa wildcard route 
Javascript :: binary conversion recursion in javascript 
Javascript :: typeorm sqlite Using async/await syntax 
Javascript :: typeorm caching queries limit 
Javascript :: react-native-gesture-handler-react-native-animated-2-tried-to-synchronously-c 
Javascript :: react stream chat 
Javascript :: Electron manage windows 
Javascript :: python range equivalent in javascript 
Javascript :: reactjs upload to firebase with custom id 
Javascript :: Using the Unshift() Method to Reverse an Array 
Javascript :: join () method to join all elements of the array into a string to reverse an string 
Javascript :: Spread syntax in ES6 
Javascript :: subject in angular service file 
Javascript :: Async restricted or not 
Javascript :: Nested Data Structures 
Javascript :: angular mat-calendar send to form 
Javascript :: reuse jquery angular 
Javascript :: make a circle in javascript 
Javascript :: js Set get elements by array 
Javascript :: ajax slick slidre 
Javascript :: uses of search engines jss3 
Javascript :: change the input feild name when the div contaoining that field is cloned using jquery 
Javascript :: POST http://localhost:3000/$(process.env.REACT_APP_API_URL)/auth/users/ 404 (Not Found) in react redux 
Javascript :: state changes when changing route useContext next 
Javascript :: jquery elements to json 
Javascript :: javascript convert to java 
Javascript :: print array elements in new line javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =