// 1. Define a function for validation to return undefined or an error object
import Joi from 'joi';
function validate(params = {}) {
const result = Joi.validate(params, {
name: Joi.string().required(),
sex: Joi.string().uppercase().valid('boy'),
is_student: Joi.boolean(),
});
// Always return the first error object
return result.error && result.error.details && result.error.details[0];
}
// 2. Call and Validate
const validationError = validate(params);
if (validationError) {
// Error handling right here
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.