使用 Array.Reduce 简化 JavaScript 对象验证
手动验证
创建验证框架
创建可重复使用的验证函数
如果没有一个好的框架,JavaScript 中的对象验证可能会非常繁琐。在本文中,我们将使用这种Array.reduce
方法,让对象验证变得轻松有趣!
手动验证
假设我们有一个user
对象,需要满足几个条件才能使其有效。以下是它们必须满足的属性和条件:
支柱 | 标准 |
---|---|
姓名 | 超过 2 个字符 |
密码 | 超过 8 个字符 |
确认密码 | 匹配密码 |
年龄 | 18岁或以上 |
如果我们采取某种手动方法进行验证,我们可能会写如下内容:
const user = {
name: "Bob",
password: "kaBob123",
confirmPassword: "kaBob123",
age: 19,
};
const errors = [];
if (user.name.length < 2) {
errors.push("User's name is too short");
}
if (user.password.length < 8) {
errors.push("User's password is too short");
}
if (user.password !== user.confirmPassword) {
errors.push("Password and confirmation do not match");
}
if (user.age < 18) {
errors.push("User must be at least 18 years old");
}
const isValid = errors.length === 0;
我们的errors
数组将会填充任何验证错误,并且如果数组的长度大于 0,我们的isValid
变量将会是false
。
创建验证框架
虽然这对于少量验证来说没问题,但我倾向于在数组中组织更大的规则集,并使用该reduce
方法来确定是否存在错误:
// Validation rules
const rules = [
{
test: (user) => user.name.length > 2,
message: "User's name is too short",
},
{
test: (user) => user.password.length >= 8,
message: "User's password is too short",
},
{
test: (user) => user.password === user.confirmPassword,
message: "Password and confirmation do not match",
},
{
test: (user) => user.age >= 18,
message: "User must be at least 18 years old",
},
];
// Test object against rules
const errors = rules.reduce((errs, rule) => {
const result = rule.test(user);
if (result === false) {
errs.push(rule.message);
}
return errs;
}, []);
const isValid = errors.length === 0;
现在,我们有一个一致的界面,并且可以添加规则,只需向我们的数组添加额外的对象!
创建可重复使用的验证函数
为了扩展验证器的实用性,我们可以创建一个函数,该函数接受一个对象和一组规则作为参数,并返回错误和验证状态。让我们来创建这个函数。
const validate = (obj, rules) => {
const errors = rules.reduce((errs, rule) => {
const result = rule.test(obj);
if (result === false) {
errs.push(rule.message);
}
return errs;
}, []);
return {
errors,
isValid: errors.length === 0
}
}
现在,我们可以在任何需要验证对象的地方使用这个函数了!让我们尝试之前的例子,并使用一个不太有效的用户对象:
// Invalid user object
const user = {
name: "Bob",
password: "kaBob123",
confirmPassword: "kaBob12",
age: 17,
};
// Validation rules
const rules = [
{
test: (user) => user.name.length > 2,
message: "User's name is too short",
},
{
test: (user) => user.password.length >= 8,
message: "User's password is too short",
},
{
test: (user) => user.password === user.confirmPassword,
message: "Password and confirmation do not match",
},
{
test: (user) => user.age >= 18,
message: "User must be at least 18 years old",
},
];
// Validation function
const validate = (obj, rules) => {
const errors = rules.reduce((errs, rule) => {
const result = rule.test(obj);
if (result === false) {
errs.push(rule.message);
}
return errs;
}, []);
return {
errors,
isValid: errors.length === 0,
};
};
// Testing our object
const result = validate(user, rules);
// {
// errors:
// [ 'Password and confirmation do not match',
// 'User must be at least 18 years old' ],
// isValid: false
// }
我希望您喜欢这次探索,它Array.reduce
使我们的对象验证更加一致和有趣。
编码愉快!
鏂囩珷鏉ユ簮锛�https://dev.to/nas5w/using-array-reduce-to-streamline-your-javascript-object-validations-2pc