Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a0dcc730cb92a616f86f0c2 Challenge 298: Schema Validator Part 4 29 challenge-298

--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,
  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.

({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.

({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.

({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.

({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.

({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.

({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.

({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.

({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--

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 and
        ("supporter" not in obj or isinstance(obj["supporter"], bool))
    )