Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

149 lines
4.2 KiB
Markdown

---
id: 6a0dcc730cb92a616f86f0c1
title: "Challenge 297: Schema Validator Part 3"
challengeType: 29
dashedName: challenge-297
---
# --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
}
```
- The pipe (`|`) symbol means "or". `role` must be one of the listed `Roles` values.
- Extra keys are allowed
# --hints--
`is_valid_schema({"username": "henry", "posts": 0, "verified": True, "role": "staff"})` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "henry", "posts": 0, "verified": True, "role": "staff"}), True)`)
}})
```
`is_valid_schema({"username": "sara", "posts": 45, "verified": False, "role": "creator", "followers": 70})` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "sara", "posts": 45, "verified": False, "role": "creator", "followers": 70}), True)`)
}})
```
`is_valid_schema({"username": "penelope", "posts": 20, "verified": True, "role": "admin"})` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "penelope", "posts": 20, "verified": True, "role": "admin"}), True)`)
}})
```
`is_valid_schema({"username": "kevin", "posts": 0, "verified": False, "role": "user"})` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "kevin", "posts": 0, "verified": False, "role": "user"}), True)`)
}})
```
`is_valid_schema({"username": "george", "posts": 15, "verified": True, "role": "moderator"})` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "george", "posts": 15, "verified": True, "role": "moderator"}), True)`)
}})
```
`is_valid_schema({"username": "david", "posts": 0, "verified": False, "role": "guest"})` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "david", "posts": 0, "verified": False, "role": "guest"}), False)`)
}})
```
`is_valid_schema({"username": "wendy", "posts": 10, "verified": True})` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "wendy", "posts": 10, "verified": True}), False)`)
}})
```
`is_valid_schema({"username": "fabian", "posts": 1, "verified": True, "role": True})` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "fabian", "posts": 1, "verified": True, "role": True}), False)`)
}})
```
`is_valid_schema({"username": 8, "posts": 1, "verified": True, "role": "user"})` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": 8, "posts": 1, "verified": True, "role": "user"}), False)`)
}})
```
`is_valid_schema({"username": "penny", "posts": "10", "verified": True, "role": "staff"})` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "penny", "posts": "10", "verified": True, "role": "staff"}), False)`)
}})
```
`is_valid_schema({"username": "john", "posts": "1", "verified": "true", "role": "admin"})` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "john", "posts": "1", "verified": "true", "role": "admin"}), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_valid_schema(obj):
return obj
```
# --solutions--
```py
def is_valid_schema(obj):
roles = ["user", "creator", "moderator", "staff", "admin"]
return (
isinstance(obj.get("username"), str) and
isinstance(obj.get("posts"), int) and not isinstance(obj.get("posts"), bool) and
isinstance(obj.get("verified"), bool) and
obj.get("role") in roles
)
```