数组等对象的验证规则

Validation rule for object like array

我一直有问题。 我想要做的是为我的数据添加一些验证规则。 数据的例子写在下面。

const data = [
    { name: 'Benz', class: 'Car', grade: 'A', isDefault: true },
    { name: 'BMW', class: 'Car', grade: 'B', isDefault: true },
    { name: 'Ford', class: 'Car', grade: 'C', isDefault: true },
    { name: 'Hyundai', class: 'Car', grade: 'C', isDefault: true },
    { name: 'Kia', class: 'Car', grade: 'C', isDefault: true },
    { name: 'Mustang', class: 'Car', grade: 'D', isDefault: true },
    { name: 'Zeep', class: 'Car', grade: 'E', isDefault: true },
    { name: 'Andy', class: 'Boat', grade: 'A', isDefault: true },
    { name: 'Tommy', class: 'Boat', grade: 'B', isDefault: true },
    { name: 'Judy', class: 'Boat', grade: 'C', isDefault: true },
    { name: 'John', class: 'Boat', grade: 'D', isDefault: true },
    { name: 'James', class: 'Boat', grade: 'E', isDefault: false },
    { name: 'Romio', class: 'Boat', grade: 'F', isDefault: false },
    { name: 'Michael', class: 'Boat', grade: 'F', isDefault: false },
    { name: 'Ron', class: 'Boat', grade: 'F', isDefault: false },
]

[ 验证规则 ]

如果'class'和'grande'相同,则必须有一个默认值(isDefault = true),但默认值不能超过1

也许这条规则很难理解。 比方说,现代的情况。 现代可以默认值class'Car'和等级'C'。但是在class'Car'&&等级'C'中还有另外一个默认值'Ford'和'Kia'。 因此,验证结果为失败。

假设 class 'Boat' && 等级 'F' 案例。 没有分配这种情况的默认值。 因此,验证结果为失败。

提前致谢。

你的意思是这样的? (class 第一个字母 == grape => isDefault:true ; class 第一个字母 != grape => isDefault:false

const data = [{ name: 'Benz', class: 'Car', grade: 'A', isDefault: true },{ name: 'BMW', class: 'Car', grade: 'B', isDefault: true },{ name: 'Ford', class: 'Car', grade: 'C', isDefault: true },{ name: 'Hyundai', class: 'Car', grade: 'C', isDefault: true },{ name: 'Kia', class: 'Car', grade: 'C', isDefault: true },{ name: 'Mustang', class: 'Car', grade: 'D', isDefault: true },{ name: 'Zeep', class: 'Car', grade: 'E', isDefault: true },{ name: 'Andy', class: 'Boat', grade: 'A', isDefault: true },{ name: 'Tommy', class: 'Boat', grade: 'B', isDefault: true },{ name: 'Judy', class: 'Boat', grade: 'C', isDefault: true },{ name: 'John', class: 'Boat', grade: 'D', isDefault: true },{ name: 'James', class: 'Boat', grade: 'E', isDefault: false }, { name: 'Romio', class: 'Boat', grade: 'F', isDefault: false },{ name: 'Michael', class: 'Boat', grade: 'F', isDefault: false },{ name: 'Ron', class: 'Boat', grade: 'F', isDefault: false }]
for(let elem of data) {elem.isDefault=(elem.class[0]==elem.grade)}
console.log(data)

以下是实现目标的一种可能方法。

代码段

// method to validate given class-grade combination
const myValidate = (pClass, pGrade, arr) => {
  // first determine if any data matches the class-grade combination
  const filterData = arr.filter(
    obj => (
      obj.class === pClass &&
      obj.grade === pGrade
    )
  );
  // if yes (ie, class-grade combination exists),
  if (filterData.length > 0) {
    // count how many "isDefault" with value "true" exists
    const defaultTrueCount = filterData.filter(({ isDefault }) => !!isDefault).length;
    // if exactly 1 "isDefault" with value "true", then test "pass"-ed
    // else, "fail"-ed
    if (defaultTrueCount === 1) return 'Pass'
    else return 'Fail';
  } else {
    // no data matched - so case is neither pass nor fail
    return `class: ${pClass} and grade: ${pGrade} combination not found in data`;
  }
};

const data = [
    { name: 'Benz', class: 'Car', grade: 'A', isDefault: true },
    { name: 'BMW', class: 'Car', grade: 'B', isDefault: true },
    { name: 'Ford', class: 'Car', grade: 'C', isDefault: true },
    { name: 'Hyundai', class: 'Car', grade: 'C', isDefault: true },
    { name: 'Kia', class: 'Car', grade: 'C', isDefault: true },
    { name: 'Mustang', class: 'Car', grade: 'D', isDefault: true },
    { name: 'Zeep', class: 'Car', grade: 'E', isDefault: true },
    { name: 'Andy', class: 'Boat', grade: 'A', isDefault: true },
    { name: 'Tommy', class: 'Boat', grade: 'B', isDefault: true },
    { name: 'Judy', class: 'Boat', grade: 'C', isDefault: true },
    { name: 'John', class: 'Boat', grade: 'D', isDefault: true },
    { name: 'James', class: 'Boat', grade: 'E', isDefault: false },
    { name: 'Romio', class: 'Boat', grade: 'F', isDefault: false },
    { name: 'Michael', class: 'Boat', grade: 'F', isDefault: false },
    { name: 'Ron', class: 'Boat', grade: 'F', isDefault: false },
];

console.log('validate class: Car, grade: C | result: ', myValidate('Car', 'C', data));
console.log('validate class: Boat, grade: F | result: ', myValidate('Boat', 'F', data));
console.log('validate class: Car, grade: A | result: ', myValidate('Car', 'A', data));
.as-console-wrapper { max-height: 100% !important; top: 0 }

说明

在上面的代码片段中添加了内联评论。