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

4.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a0dcc730cb92a616f86f0c1 Challenge 297: Schema Validator Part 3 29 challenge-297

--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
}
  • The pipe (|) symbol means "or". role must be one of the listed Roles values.
  • Extra keys are allowed

--hints--

is_valid_schema({"username": "henry", "posts": 0, "verified": True, "role": "staff"}) should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "henry", "posts": 0, "verified": True, "role": "staff"}), True)`)
}})

is_valid_schema({"username": "sara", "posts": 45, "verified": False, "role": "creator", "followers": 70}) should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "sara", "posts": 45, "verified": False, "role": "creator", "followers": 70}), True)`)
}})

is_valid_schema({"username": "penelope", "posts": 20, "verified": True, "role": "admin"}) should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "penelope", "posts": 20, "verified": True, "role": "admin"}), True)`)
}})

is_valid_schema({"username": "kevin", "posts": 0, "verified": False, "role": "user"}) should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "kevin", "posts": 0, "verified": False, "role": "user"}), True)`)
}})

is_valid_schema({"username": "george", "posts": 15, "verified": True, "role": "moderator"}) should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "george", "posts": 15, "verified": True, "role": "moderator"}), True)`)
}})

is_valid_schema({"username": "david", "posts": 0, "verified": False, "role": "guest"}) should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "david", "posts": 0, "verified": False, "role": "guest"}), False)`)
}})

is_valid_schema({"username": "wendy", "posts": 10, "verified": True}) should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "wendy", "posts": 10, "verified": True}), False)`)
}})

is_valid_schema({"username": "fabian", "posts": 1, "verified": True, "role": True}) should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "fabian", "posts": 1, "verified": True, "role": True}), False)`)
}})

is_valid_schema({"username": 8, "posts": 1, "verified": True, "role": "user"}) should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": 8, "posts": 1, "verified": True, "role": "user"}), False)`)
}})

is_valid_schema({"username": "penny", "posts": "10", "verified": True, "role": "staff"}) should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "penny", "posts": "10", "verified": True, "role": "staff"}), False)`)
}})

is_valid_schema({"username": "john", "posts": "1", "verified": "true", "role": "admin"}) should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_schema({"username": "john", "posts": "1", "verified": "true", "role": "admin"}), 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
    )