4.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a0dcc730cb92a616f86f0c1 | Challenge 297: Schema Validator Part 3 | 29 | 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--
is_valid_schema({"username": "henry", "posts": 0, "verified": True, "role": "staff"}) should return True.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "john", "posts": "1", "verified": "true", "role": "admin"}), False)`)
}})
--seed--
--seed-contents--
def is_valid_schema(obj):
return obj
--solutions--
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
)