score:0
You can return a custom error object using Error constructor like this:
var schema = Joi.object().keys({
firstName: Joi.string().min(4).max(8).required().error(new
Error('error message here for first name')),
lastName: Joi.string().min(5).max(1).required().error(new
Error('error message here for last name'))
});
Joi.validate(req.body, schema, function(err, value) {
if (err) {
console.log(err.message)
return catched(err.message);
}
});
score:0
The easiest way in my opinion would be this.
const Joi = require("@hapi/joi");
export const categorySchema = Joi.object({
mobile: Joi.string().trim().regex(/^[6-9]\d{9}$/).required().messages({
"string.base": `"" should be a type of string`,
"string.empty": `"" must contain value`,
"string.pattern.base": `"" must be 10 digit number`,
"any.required": `"" is a required field`
}),
password: Joi.string().trim().required().messages({
"string.base": `"" should be a type of 'text'`,
"string.pattern.base": `"" must be 10 digit number`,
"any.required": `"" is a required field`
}),
}).required();
Source: stackoverflow.com
Related Query
- How to make custom error message using Joi?
- How to display custom error message from Node/Express in React/Redux?
- How to create custom error messages using AJV?
- How do I make this custom generated table editable? Made it using Ag-grid react
- Joi validation display custom error message
- react-hook-form Set error message for custom validation when using useController
- how can I pass success or error message using react-toastify when using axios method
- How to get error message in terminal when using Redux React
- How to make a react custom component animated using react-spring
- How to have a custom internal server error using react next? (500 internal server error)
- How to clear custom message created by error boundary when I navigate to other router pages
- How to show error message using react-hook-form in react
- How to fix jslint error 'Don't make functions within a loop.' while using find function inside loop
- How to display a success or error message after submitting the form? Using react
- How to display error message correctly from custom hook validation?
- How can I make error validation work in React Hook Form when using template literal in register function?
- How to make a message appear once email is submitted using React and CSS
- How to get the relevant error message when using React-hook-forms in a loop ,
- How do I make error message dissapear when user move to another page in React?
- How to add custom error message with joi?
- How to show error message using PhoneInput from react-phone-input-2 package?
- How can I make use of Error boundaries in functional React components?
- How to make a dropdown menu open below the Appbar using Material-UI?
- How to make the whole Card component clickable in Material UI using React JS?
- How to make sure a React state using useState() hook has been updated?
- How to use absolute path to import custom scss, when using react + webpack?
- How to integrate Error Boundary in Components routed using react-router
- How do I make components in React Native without using JSX?
- Swiper React | How to create custom navigation/pagination components using React refs?
- How to fetch response body using fetch in case of HTTP error 422?
More Query from same tag
- react-router Link changes url but Route component doesn't render
- best way to implement render props in react
- Cannot append to formData object on file upload in React
- React with Webpack: PropTypes appearing in final bundle
- Refactoring a React PureComponent to a hooks based functional component
- ReactJs update parent after ComponentDidMount from child class
- type 'string | null' is not assignable to parameter of type 'SetStateAction<string>'.Type 'null' is not assignable to type 'SetStateAction<string>'
- React BrowserRouter works when navigating on localhost. Refreshing a not-home-page when deployed throws 404 not found
- useEffect EventListener problem REACT HOOKS
- How to organize useState and useQuery - Apollo/Graphql
- res.sendFile() shows my html, but doesn't render react components
- Material UI - Facing an issue that drop down options are coming below the modal window footer
- How to extract data from API call
- useContext causing blank screen
- Can't make requests in mern stack heroku hosted application, in production version
- Drawer in material-ui v1.0 doesn't work with typescript
- Ant Design - Make Menu Uncollapsable/Unfoldable
- Why is React making my SVG shape color look weird?
- how to dynamically parse Json data using reactjs
- How to count number of occurrences of distinct values from an array of objects in Javascript?
- How to get value text to follow slider input?
- History goBack not working at function component
- Proxy error: Could not proxy request /api/register from localhost:3000 to http://localhost:8000/ (ECONNREFUSED)
- Authentication issue - previous user data is shown
- TypeError: eleves.map is not a function React/Spring-boot cannot display @ManyToOne relation
- how to hide show different components in one component ionic 2
- How to detect keydown anywhere on page in a React app?
- Testing subsequent function call on an Async function from React function component
- React Dnd out of position after scroll
- Passing child component class as props to parent component in React