--- id: 6a0dcc730cb92a616f86f0c2 title: "Challenge 298: Schema Validator Part 4" challengeType: 29 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-- `is_valid_schema({"username": "vivian", "posts": 1, "verified": False, "role": "user", "supporter": True})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "vivian", "posts": 1, "verified": False, "role": "user", "supporter": True}), True)`) }}) ``` `is_valid_schema({"username": "rudolph", "posts": 15, "verified": True, "role": "creator"})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "rudolph", "posts": 15, "verified": True, "role": "creator"}), True)`) }}) ``` `is_valid_schema({"username": "hernandez", "posts": 35, "verified": True, "role": "moderator", "supporter": False, "followers": 55})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "hernandez", "posts": 35, "verified": True, "role": "moderator", "supporter": False, "followers": 55}), True)`) }}) ``` `is_valid_schema({"username": "julia", "posts": 50, "verified": True, "role": "admin", "supporter": "true"})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "julia", "posts": 50, "verified": True, "role": "admin", "supporter": "true"}), False)`) }}) ``` `is_valid_schema({"username": "bernard", "posts": 0, "verified": True, "role": "friend", "supporter": True})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "bernard", "posts": 0, "verified": True, "role": "friend", "supporter": True}), False)`) }}) ``` `is_valid_schema({"username": "felix", "posts": 40, "verified": "yes", "role": "staff", "supporter": False})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "felix", "posts": 40, "verified": "yes", "role": "staff", "supporter": False}), False)`) }}) ``` `is_valid_schema({"username": "jimmy", "posts": True, "verified": False, "role": "creator", "supporter": True})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "jimmy", "posts": True, "verified": False, "role": "creator", "supporter": True}), False)`) }}) ``` `is_valid_schema({"username": True, "posts": 30, "verified": True, "role": "moderator", "supporter": False})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": True, "posts": 30, "verified": True, "role": "moderator", "supporter": False}), 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 and ("supporter" not in obj or isinstance(obj["supporter"], bool)) ) ```