3.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a0dcc730cb92a616f86f0c1 | Challenge 297: Schema Validator Part 3 | 28 | challenge-297 |
--description--
Given an object (JavaScript) or dictionary (Python), determine if it matches the following schema:
Roles = "user" | "creator" | "moderator" | "staff" | "admin"
{
username: string,
posts: number,
verified: boolean,
role: Roles
}
- The pipe (
|) symbol means "or".rolemust be one of the listedRolesvalues. - Extra keys are allowed
--hints--
isValidSchema({ username: "henry", posts: 0, verified: true, role: "staff" }) should return true.
assert.isTrue(isValidSchema({ username: "henry", posts: 0, verified: true, role: "staff" }));
isValidSchema({ username: "sara", posts: 45, verified: false, role: "creator", followers: 70 }) should return true.
assert.isTrue(isValidSchema({ username: "sara", posts: 45, verified: false, role: "creator", followers: 70 }));
isValidSchema({ username: "penelope", posts: 20, verified: true, role: "admin" }) should return true.
assert.isTrue(isValidSchema({ username: "penelope", posts: 20, verified: true, role: "admin" }));
isValidSchema({ username: "kevin", posts: 0, verified: false, role: "user" }) should return true.
assert.isTrue(isValidSchema({ username: "kevin", posts: 0, verified: false, role: "user" }));
isValidSchema({ username: "george", posts: 15, verified: true, role: "moderator" }) should return true.
assert.isTrue(isValidSchema({ username: "george", posts: 15, verified: true, role: "moderator" }));
isValidSchema({ username: "david", posts: 0, verified: false, role: "guest" }) should return false.
assert.isFalse(isValidSchema({ username: "david", posts: 0, verified: false, role: "guest" }));
isValidSchema({ username: "wendy", posts: 10, verified: true }) should return false.
assert.isFalse(isValidSchema({ username: "wendy", posts: 10, verified: true }));
isValidSchema({ username: "fabian", posts: 1, verified: true, role: true }) should return false.
assert.isFalse(isValidSchema({ username: "fabian", posts: 1, verified: true, role: true }));
isValidSchema({ username: 8, posts: 1, verified: true, role: "user" }) should return false.
assert.isFalse(isValidSchema({ username: 8, posts: 1, verified: true, role: "user" }));
isValidSchema({ username: "penny", posts: "10", verified: true, role: "staff" }) should return false.
assert.isFalse(isValidSchema({ username: "penny", posts: "10", verified: true, role: "staff" }));
isValidSchema({ username: "john", posts: "1", verified: "true", role: "admin" }) should return false.
assert.isFalse(isValidSchema({ username: "john", posts: "1", verified: "true", role: "admin" }));
--seed--
--seed-contents--
function isValidSchema(obj) {
return obj;
}
--solutions--
function isValidSchema(obj) {
const roles = ["user", "creator", "moderator", "staff", "admin"];
return (
typeof obj.username === 'string' &&
typeof obj.posts === 'number' &&
typeof obj.verified === 'boolean' &&
roles.includes(obj.role)
);
}