Skip to main content
Version: 0.2.0

Raw-payload schemas

The bundled raw-payload schemas (sportsdataverse/schemas/raw/*.yaml) and the validator that checks a captured payload against one before it is stored.

Release utilities​

load_raw_schema​

load_raw_schema(name: 'str') -> 'dict[str, Any]'

Load a raw-payload schema by name.

Parameters

ParameterTypeDefaultDescription
namestrA key of RAW_SCHEMAS, e.g. "espn_summary".

Returns

The parsed schema: {name, description, additional_properties, required, optional}.

Raises

ExceptionDescription
KeyErrorIf name is not a declared payload family.
ImportErrorIf PyYAML is not installed.
TypeErrorIf the schema file does not parse to a mapping.

Example

from sportsdataverse.schemas import load_raw_schema

schema = load_raw_schema("espn_summary")
print(sorted(schema["required"]))

# Inspect what a family tolerates

print(schema["additional_properties"]) # True -- unknown keys allowed

validate_payload​

validate_payload(name: 'str', payload: 'Any') -> 'list[str]'

Check a payload against its declared shape.

Parameters

ParameterTypeDefaultDescription
namestrA key of RAW_SCHEMAS.
payloadAnyThe parsed JSON payload.

Returns

A list of human-readable problems. An empty list means the payload matches: every required key is present with a declared type, and every present optional key has a declared type. Unknown keys are ignored by design.

Raises

ExceptionDescription
KeyErrorIf name is not a declared payload family.

Example

import json
from sportsdataverse.schemas import validate_payload

payload = json.loads(open("wbb/json/final/401811123.json").read())
problems = validate_payload("espn_summary", payload)
print(problems) # [] when the payload matches

# Fail a scrape on a bad capture

if validate_payload("espn_summary", payload):
raise AssertionError("refusing to persist a non-conforming payload")

# Scan an archive for damage

import glob

paths = glob.glob("wbb/json/final/*.json")
bad = [p for p in paths if validate_payload("espn_summary", json.loads(open(p).read()))]