--- id: 6a0dcc730cb92a616f86f0c4 title: "Challenge 300: Schema Validator Part 6" challengeType: 29 dashedName: challenge-300 --- # --description-- Given an object (JavaScript) or dictionary (Python), determine if it matches the following schema: ```json Roles = "user" | "creator" | "moderator" | "staff" | "admin" UserProfile = { username: string, posts: number, verified: boolean, role: Roles, supporter?: boolean, badges: string[] } { users: UserProfile[] } ``` - 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. - `UserProfile[]` denotes an array of `UserProfile` objects. An empty array is valid. - Extra keys are allowed # --hints-- `is_valid_schema({"users": [{"username": "ron", "posts": 14, "verified": True, "role": "creator", "badges": ["early-adopter"]}, {"username": "cher", "posts": 25, "verified": True, "role": "moderator", "supporter": True, "followers": 20, "badges": ["helper"]}]})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "ron", "posts": 14, "verified": True, "role": "creator", "badges": ["early-adopter"]}, {"username": "cher", "posts": 25, "verified": True, "role": "moderator", "supporter": True, "followers": 20, "badges": ["helper"]}]}), True)`) }}) ``` `is_valid_schema({"users": []})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": []}), True)`) }}) ``` `is_valid_schema({"users": {"username": "anne", "posts": 0, "verified": False, "role": "user", "supporter": False, "badges": []}})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": {"username": "anne", "posts": 0, "verified": False, "role": "user", "supporter": False, "badges": []}}), False)`) }}) ``` `is_valid_schema({"users": [{"username": "tony", "posts": 10, "verified": True, "role": "creator", "supporter": True, "badges": ["liked", 6]}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "tony", "posts": 10, "verified": True, "role": "creator", "supporter": True, "badges": ["liked", 6]}]}), False)`) }}) ``` `is_valid_schema({"users": [{"username": "ursula", "posts": 3, "verified": False, "role": "user", "supporter": "false", "badges": ["comeback"]}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "ursula", "posts": 3, "verified": False, "role": "user", "supporter": "false", "badges": ["comeback"]}]}), False)`) }}) ``` `is_valid_schema({"users": [{"username": "benny", "posts": 55, "verified": True, "role": "superstar", "supporter": True, "badges": ["veteran"]}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "benny", "posts": 55, "verified": True, "role": "superstar", "supporter": True, "badges": ["veteran"]}]}), False)`) }}) ``` `is_valid_schema({"users": [{"username": "chase", "posts": 1, "verified": "yes", "role": "staff", "supporter": False, "badges": ["superstar"]}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "chase", "posts": 1, "verified": "yes", "role": "staff", "supporter": False, "badges": ["superstar"]}]}), False)`) }}) ``` `is_valid_schema({"users": [{"username": "carla", "posts": "10", "verified": False, "role": "user", "supporter": False, "badges": ["newbie"]}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "carla", "posts": "10", "verified": False, "role": "user", "supporter": False, "badges": ["newbie"]}]}), False)`) }}) ``` `is_valid_schema({"users": [{"posts": 4, "verified": False, "role": "admin", "supporter": False, "badges": ["superuser", "veteran"]}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"posts": 4, "verified": False, "role": "admin", "supporter": False, "badges": ["superuser", "veteran"]}]}), False)`) }}) ``` `is_valid_schema({"users": [{"username": "harold", "posts": 80, "verified": True, "role": "creator", "supporter": True, "badges": ["liked", "hero"]}, {"username": "kim", "posts": 11, "verified": False, "role": "admin", "supporter": True, "badges": ["first"]}, {}]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"users": [{"username": "harold", "posts": 80, "verified": True, "role": "creator", "supporter": True, "badges": ["liked", "hero"]}, {"username": "kim", "posts": 11, "verified": False, "role": "admin", "supporter": True, "badges": ["first"]}, {}]}), 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"] def is_valid_user(user): return ( isinstance(user.get("username"), str) and isinstance(user.get("posts"), int) and not isinstance(user.get("posts"), bool) and isinstance(user.get("verified"), bool) and user.get("role") in roles and ("supporter" not in user or isinstance(user["supporter"], bool)) and isinstance(user.get("badges"), list) and all(isinstance(b, str) for b in user["badges"]) ) return isinstance(obj.get("users"), list) and all(is_valid_user(u) for u in obj["users"]) ```