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
107 lines
2.5 KiB
Markdown
107 lines
2.5 KiB
Markdown
---
|
|
id: 6a0dcc730cb92a616f86f0c0
|
|
title: "Challenge 296: Schema Validator Part 2"
|
|
challengeType: 29
|
|
dashedName: challenge-296
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an object (JavaScript) or dictionary (Python), determine if it matches the following schema:
|
|
|
|
```json
|
|
{
|
|
username: string,
|
|
posts: number,
|
|
verified: boolean
|
|
}
|
|
```
|
|
|
|
- Extra keys are allowed
|
|
|
|
# --hints--
|
|
|
|
`is_valid_schema({"username": "alice", "posts": 10, "verified": False})` should return `True`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": "alice", "posts": 10, "verified": False}), True)`)
|
|
}})
|
|
```
|
|
|
|
`is_valid_schema({"username": "carol", "posts": 15, "verified": True, "followers": 25})` should return `True`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": "carol", "posts": 15, "verified": True, "followers": 25}), True)`)
|
|
}})
|
|
```
|
|
|
|
`is_valid_schema({"username": "frank", "posts": "21", "verified": True})` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": "frank", "posts": "21", "verified": True}), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_valid_schema({"username": "sam", "posts": 17, "verified": "false"})` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": "sam", "posts": 17, "verified": "false"}), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_valid_schema({"username": "bill", "verified": True})` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": "bill", "verified": True}), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_valid_schema({"username": "fred", "verified": True})` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": "fred", "verified": True}), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_valid_schema({"username": 5, "posts": 10, "verified": True})` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_valid_schema({"username": 5, "posts": 10, "verified": True}), False)`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def is_valid_schema(obj):
|
|
|
|
return obj
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def is_valid_schema(obj):
|
|
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)
|
|
)
|
|
```
|