Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
5.2. Check an object against a set of rules
Easy
Topics
This question is in
5. Objects and property controlA 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
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 }:errorsis a new array of the keys whose rule failed, in the order ofrules;validistruewhenerrorsis empty.
Requirements
- Every rule runs exactly once, with the value, the key and the object. A truthy result passes; a falsy result fails.
errorslists the failing keys in the order of the rules' keys;validistrueonly when nothing failed. No rules means valid.- The value is the object's own property. A missing key, and a property the object only inherits (from its prototype, or
toStringfromObject.prototype), give the ruleundefined. - A rule that throws counts as failed, and the rules after it still run.
- Keys of the object that have no rule are ignored. Only the rules object's own enumerable keys are rules.
- If any rule is not a function, it throws a
TypeErrorbefore running any rule. - It throws a
TypeErrorwhenobjectis not an object (null,undefined, a number, a string, a boolean). Arrays are objects, checked by index. - The object is not changed, and every call returns a new
errorsarray.
Examples
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"] }checkRules({}, { email: (value) => value !== undefined }); // { valid: false, errors: ["email"] }
checkRules({ a: 1 }, { a: () => { throw new Error("oops"); } }); // { valid: false, errors: ["a"] }checkRules({ a: 1 }, { a: "required" }); // TypeError: the rule is not a function
checkRules(null, rules); // TypeErrorEnvironment
- Plain JavaScript in a browser page; the tests call the global
checkRulesfromcheck-rules.js. - Every built-in is allowed. Nested rules (a rule object for a nested object) are out of scope.
Hints
- Which check tells an own property from an inherited one?
- 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…