Objective: Implement a simple form validation library that allows validating form fields based on custom rules.
Instructions:
validateForm
that takes an object representing form data and an object representing validation rules.Example Usage:
const formData = {
username: 'user123',
password: 'pass',
email: 'user@example.com',
};
const validationRules = {
username: { required: true, minLength: 5 },
password: { required: true, minLength: 8 },
email: { required: true, email: true },
};
const result = validateForm(formData, validationRules);
console.log(result);
// Output: { valid: false, errors: { password: 'Password must be at least 8 characters.' } }
Requirements:
Bonus: