--- id: 6a0dcc730cb92a616f86f0c3 title: "Challenge 299: Schema Validator Part 5" challengeType: 29 dashedName: challenge-299 --- # --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, badges: string[] } ``` - 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. - The brackets `[]` after `string` means that `badges` should be an array of strings (or empty). - Extra keys are allowed # --hints-- `is_valid_schema({"username": "gill", "posts": 12, "verified": False, "role": "creator", "supporter": False, "badges": ["early-adopter", "popular"]})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "gill", "posts": 12, "verified": False, "role": "creator", "supporter": False, "badges": ["early-adopter", "popular"]}), True)`) }}) ``` `is_valid_schema({"username": "tonya", "posts": 299, "verified": True, "role": "moderator", "supporter": True, "badges": ["streak-master", "veteran"], "followers": 1233})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "tonya", "posts": 299, "verified": True, "role": "moderator", "supporter": True, "badges": ["streak-master", "veteran"], "followers": 1233}), True)`) }}) ``` `is_valid_schema({"username": "zara", "posts": 0, "verified": False, "role": "user", "supporter": False, "badges": []})` should return `True`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "zara", "posts": 0, "verified": False, "role": "user", "supporter": False, "badges": []}), True)`) }}) ``` `is_valid_schema({"username": "nicole", "posts": 65, "verified": True, "role": "admin", "supporter": False, "badges": ["first-post", 18]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "nicole", "posts": 65, "verified": True, "role": "admin", "supporter": False, "badges": ["first-post", 18]}), False)`) }}) ``` `is_valid_schema({"username": "tim", "posts": 25, "verified": True, "role": "staff", "supporter": False})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "tim", "posts": 25, "verified": True, "role": "staff", "supporter": False}), False)`) }}) ``` `is_valid_schema({"username": "charlie", "posts": 0, "verified": False, "role": "user", "supporter": "no", "badges": ["first-post", "anniversary"]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "charlie", "posts": 0, "verified": False, "role": "user", "supporter": "no", "badges": ["first-post", "anniversary"]}), False)`) }}) ``` `is_valid_schema({"username": "wanda", "posts": 15, "verified": True, "role": "friend", "supporter": True, "badges": ["popular"]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "wanda", "posts": 15, "verified": True, "role": "friend", "supporter": True, "badges": ["popular"]}), False)`) }}) ``` `is_valid_schema({"username": "guy", "posts": 5, "verified": "false", "role": "staff", "supporter": True, "badges": ["helper"]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "guy", "posts": 5, "verified": "false", "role": "staff", "supporter": True, "badges": ["helper"]}), False)`) }}) ``` `is_valid_schema({"username": "carrie", "verified": True, "role": "moderator", "supporter": True, "badges": ["helper", "sharer"]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": "carrie", "verified": True, "role": "moderator", "supporter": True, "badges": ["helper", "sharer"]}), False)`) }}) ``` `is_valid_schema({"username": True, "posts": 75, "verified": True, "role": "creator", "supporter": True, "badges": ["veteran"]})` should return `False`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertIs(is_valid_schema({"username": True, "posts": 75, "verified": True, "role": "creator", "supporter": True, "badges": ["veteran"]}), 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)) and isinstance(obj.get("badges"), list) and all(isinstance(b, str) for b in obj["badges"]) ) ```