score:1
Accepted answer
you can do like this way:
*I've done an example where at least one registrationNumber in array should be filled for your vehicles schema:
vehicles: Yup.array(
Yup.object({
registrationNumber: Yup.string(),
make: Yup.string().required("make Required"),
}).test(
"registrationNumber test",
// The error message that should appears if test failed
"at least one registrationNumber should be filled",
// Function that does the custom validation to this array
validateAgainstPrevious
)
)
The function below its just an example. You can do your own logic here.
function validateAgainstPrevious() {
// In this case, parent is the entire array
const { parent } = this;
// filtered array vechicles that doens't have registrationNumber
const filteredArray = parent.filter((e) => !e.registrationNumber);
// If length of vehicles that doesn't have registrationNumber is equals to vehicles array length then return false;
if (filteredArray.length === parent.length) return false;
return true;
}
UPDATED
For the multiple error text issue, you can do a workaround:
Instead you pass TextField component to Field, like this:
<Field
component={TextField}
fullWidth={true}
label="Registration Number"
name={`vehicles[${index}].registrationNumber`}
/>
you can do this:
<Field
// component={TextField}
fullWidth={true}
label="Registration Number"
name={`vehicles[${index}].registrationNumber`}
render={() => (
<TextField
error={Boolean(errors.vehicles)}
helperText= {
errors.vehicles && getVehiclesErrors(errors.vehicles)
}
/>
)}
/>
And created this function:
const getVehiclesErrors = (errors) => {
return Array.isArray(errors)
? errors.filter((email, i, arr) => arr.indexOf(email) === i)
: errors;
};
score:2
Instead Yup.object({}) use Yup.object().shape({ ... all the properties comes here})
Source: stackoverflow.com
Related Query
- How to validate individual element of an array of JSON objects using Yup
- How to validate with Yup that in array of objects at least one of object keys has true value?
- How to validate array of objects using reactjs
- How to process JSON Array of objects using Key values in JavaScript
- How to delete array element from JSON array of objects by ID
- How to push into an array of object using the spread operator to a specific element
- Yup validate array of objects contains at most one object where property = value
- How to get first error while using yup to validate object schema
- How to update an object in an array of Objects using setState
- How to connect each element of array individually by using react redux
- How to save big array of objects in react native using react-native-db-model
- Unable to figure out how to highlight or select individual objects in InstancedMesh and display custom HTML tooltip using react three fiber
- How to iterate through an array of objects from json in React.js
- How can I send a file within an array of objects using formData.append()
- React how to conditionally edit or add object in array of objects using setState
- Unable to access element from array of JSON objects
- How to update the array of objects using onChange handler in React JS?
- How to extract element from JSON array and put into another array
- Fetching the input value from textarea in a loop from an array of objects using reactjs. How to do so?
- Yup validation of first and last element in array of objects
- Validating formik field containing array of objects using Yup
- How to map array of objects which i need to groupby date property in JSON format with react JS
- How to use spread operator to delete an object from array of objects using spread operator using react?
- How to add more objects to Array Object state using setState?
- How to iterate through array of objects and execute a request using react?
- How to set state of an Array of objects using useState?
- How to Sort Dates, Numbers, Strings from the array of nested JSON objects in Javascript
- How to print only first element from JSON Array in ReactJs?
- How to display a message if the array of objects is empty using react?
- How to get the name of the array in json data using ReactJs?
More Query from same tag
- Memory usage is getting higher and higer while I am scrolling Flatlist and memory is not released when I stop scrolling (React Native)
- Stop re-rendering components when route changes. Especially components outside Routes
- Passing keys to children in React.js
- remove imported styles when component unmounts
- Express multer is returning undefined on req.file
- React img tag add class on load
- React - How to make react-icon rotate 180 degrees onClick?
- WebpackError: TypeError: Cannot read property 'map' of undefined during Gatsby build
- How to stop auto re-render on components by using useEffect hook?
- Show/hide a message if date time match with API response
- useFetch custom hook not working properly
- Create calculator in React from preexisting template
- React Plotly JS apply dark (plotly_dark) theme
- How to create a new user along with their profile images in Sanity?
- When does React re-render after a setState call made inside an event handler
- Why should we use next js configuration file in React project? What's the main purpose using it?
- React: Passing child state to parent state
- How do I write conditional code for added data to existing Fetch API call in React?
- React js truncate text with ellipsis in the middle
- useCustomHook being called on every render - is something wrong with this
- how to test Routes in react using jest and react testing library
- Bitbucket pipeline env variables
- React-router - How to pass data between pages in React?
- Stuck with webpack HMR configuration
- React Axios not rendering after setState
- Invalid hook call - useContext - React Hooks
- React/Redux - how to triggle customer event by action
- Ref array of dropdown different components by id
- Setting props and testing nested component
- How to push array inside a object with arrays in React js?