5.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a0dcc730cb92a616f86f0c4 | Challenge 300: Schema Validator Part 6 | 29 | challenge-300 |
--description--
Given an object (JavaScript) or dictionary (Python), determine if it matches the following schema:
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 listedRolesvalues. - The question mark (
?) after supporter means that the field is optional, but is the specified type if it exists. UserProfile[]denotes an array ofUserProfileobjects. 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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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--
def is_valid_schema(obj):
return obj
--solutions--
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"])