dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
103 lines
3.1 KiB
Markdown
103 lines
3.1 KiB
Markdown
---
|
|
id: 6a0dcc730cb92a616f86f0c2
|
|
title: "Challenge 298: Schema Validator Part 4"
|
|
challengeType: 28
|
|
dashedName: challenge-298
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an object (JavaScript) or dictionary (Python), determine if it matches the following schema:
|
|
|
|
```json
|
|
Roles = "user" | "creator" | "moderator" | "staff" | "admin"
|
|
|
|
{
|
|
username: string,
|
|
posts: number,
|
|
verified: boolean,
|
|
role: Roles,
|
|
supporter?: boolean
|
|
}
|
|
```
|
|
|
|
- The pipe (`|`) symbol means "or". `role` must be one of the listed `Roles` values.
|
|
- The question mark (`?`) after `supporter` means that the field is optional, but is the specified type if it exists.
|
|
- Extra keys are allowed
|
|
|
|
# --hints--
|
|
|
|
`isValidSchema({ username: "vivian", posts: 1, verified: false, role: "user", supporter: true })` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isValidSchema({ username: "vivian", posts: 1, verified: false, role: "user", supporter: true }));
|
|
```
|
|
|
|
`isValidSchema({ username: "rudolph", posts: 15, verified: true, role: "creator" })` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isValidSchema({ username: "rudolph", posts: 15, verified: true, role: "creator" }));
|
|
```
|
|
|
|
`isValidSchema({ username: "hernandez", posts: 35, verified: true, role: "moderator", supporter: false, followers: 55 })` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isValidSchema({ username: "hernandez", posts: 35, verified: true, role: "moderator", supporter: false, followers: 55 }));
|
|
```
|
|
|
|
`isValidSchema({ username: "julia", posts: 50, verified: true, role: "admin", supporter: "true" })` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidSchema({ username: "julia", posts: 50, verified: true, role: "admin", supporter: "true" }));
|
|
```
|
|
|
|
`isValidSchema({ username: "bernard", posts: 0, verified: true, role: "friend", supporter: true })` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidSchema({ username: "bernard", posts: 0, verified: true, role: "friend", supporter: true }));
|
|
```
|
|
|
|
`isValidSchema({ username: "felix", posts: 40, verified: "yes", role: "staff", supporter: false })` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidSchema({ username: "felix", posts: 40, verified: "yes", role: "staff", supporter: false }));
|
|
```
|
|
|
|
`isValidSchema({ username: "jimmy", posts: true, verified: false, role: "creator", supporter: true })` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidSchema({ username: "jimmy", posts: true, verified: false, role: "creator", supporter: true }));
|
|
```
|
|
|
|
`isValidSchema({ username: true, posts: 30, verified: true, role: "moderator", supporter: false })` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isValidSchema({ username: true, posts: 30, verified: true, role: "moderator", supporter: false }));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function isValidSchema(obj) {
|
|
|
|
return obj;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
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) &&
|
|
(obj.supporter === undefined || typeof obj.supporter === 'boolean')
|
|
);
|
|
}
|
|
```
|