第一个
valid
js
// object validation rules
const schema = {
first: {
required: true,
},
last: {
required: true,
},
}
// universal validation function
const validate = (schema, values) => {
for (field in schema) {
if (schema[field].required) {
if (!values[field]) {
return false
}
}
}
return true
}
console.log(validate(schema, { first: 'Bruce' })) // false
console.log(validate(schema, { first: 'Bruce', last: 'Wayne' })) // true
// object validation rules
const schema = {
first: {
required: true,
},
last: {
required: true,
},
}
// universal validation function
const validate = (schema, values) => {
for (field in schema) {
if (schema[field].required) {
if (!values[field]) {
return false
}
}
}
return true
}
console.log(validate(schema, { first: 'Bruce' })) // false
console.log(validate(schema, { first: 'Bruce', last: 'Wayne' })) // true