Skip to curriculum
Jump to the editor

5.2. Check an object against a set of rules

Easy
Topics
10–20 min

A sign-up form sends { name, email, age }; a settings file has a dozen keys; an API returns a user. Before using any of them, code checks each field: is the name a string, is the age at least 18, is the email there at all. Writing those checks as one rule per key keeps them readable, and a small function can run them all and report which keys failed.

Write checkRules(object, rules) in check-rules.js. rules maps each key to a rule function. checkRules runs every rule and returns { valid, errors }.

Signature

JAVASCRIPT
function checkRules(object, rules) // returns { valid, errors }
  • object: the object to check.
  • rules: an object whose own enumerable keys are the keys to check; each value is a rule, rule(value, key, object).
  • Returns { valid, errors }: errors is a new array of the keys whose rule failed, in the order of rules; valid is true when errors is empty.

Requirements

  1. Every rule runs exactly once, with the value, the key and the object. A truthy result passes; a falsy result fails.
  2. errors lists the failing keys in the order of the rules' keys; valid is true only when nothing failed. No rules means valid.
  3. The value is the object's own property. A missing key, and a property the object only inherits (from its prototype, or toString from Object.prototype), give the rule undefined.
  4. A rule that throws counts as failed, and the rules after it still run.
  5. Keys of the object that have no rule are ignored. Only the rules object's own enumerable keys are rules.
  6. If any rule is not a function, it throws a TypeError before running any rule.
  7. It throws a TypeError when object is not an object (null, undefined, a number, a string, a boolean). Arrays are objects, checked by index.
  8. The object is not changed, and every call returns a new errors array.

Examples

JAVASCRIPT
const rules = {
  name: (value) => typeof value === "string",
  age: (value) => typeof value === "number" && value >= 18,
};

checkRules({ name: "Asha", age: 30 }, rules); // { valid: true, errors: [] }
checkRules({ name: 7, age: 12 }, rules);      // { valid: false, errors: ["name", "age"] }
JAVASCRIPT
checkRules({}, { email: (value) => value !== undefined });        // { valid: false, errors: ["email"] }
checkRules({ a: 1 }, { a: () => { throw new Error("oops"); } });  // { valid: false, errors: ["a"] }
JAVASCRIPT
checkRules({ a: 1 }, { a: "required" });  // TypeError: the rule is not a function
checkRules(null, rules);                  // TypeError

Environment

  • Plain JavaScript in a browser page; the tests call the global checkRules from check-rules.js.
  • Every built-in is allowed. Nested rules (a rule object for a nested object) are out of scope.

Hints

  1. Which check tells an own property from an inherited one?
  2. To throw before any rule runs, what do you have to do before the loop that runs them?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…