Files
galaxy/lib/galaxy_test/base/json_schema_utils.py
T
Nicola Soranzo 7f89e0eab9 Code cleanups from pyupgrade
Found by running:

```
make pyupgrade
ruff check --fix .
ruff check --preview --unsafe-fixes --select=F401 --fix .
make format
make update-client-api-schema
```

The bulk of the changes are replacements of typing deprecated aliases
such as `Dict` and `List` with the standard library classes. These
aliases became redundant in Python 3.9 when the corresponding
pre-existing classes were enhanced to support `[]`, see
https://docs.python.org/3/library/typing.html#deprecated-aliases

Also:
- Update the `pyupgrade` Makefile target to use `--py38-plus` for the
  packages needed by Pulsar, including the new galaxy-tool-util-models
  package.
2025-07-31 11:17:22 +01:00

33 lines
1.0 KiB
Python

from typing import (
Any,
)
import jsonschema
from galaxy.util import requests
from galaxy_test.base import api_asserts
schema_store: dict[str, Any] = {}
class JsonSchemaValidator:
@staticmethod
def validate_using_schema_url(instance: dict, schema_url: str):
schema = schema_store.get(schema_url, None)
if not schema:
response = requests.get(schema_url)
api_asserts.assert_status_code_is_ok(response)
schema = response.json()
schema_store[schema_url] = schema
JsonSchemaValidator.validate(instance, schema)
@staticmethod
def validate(instance: dict, schema: dict):
try:
schema_version = schema.get("$id", "Unknown schema version")
jsonschema.validate(instance=instance, schema=schema)
except jsonschema.exceptions.ValidationError as err:
raise AssertionError(
f"The instance does not validate against the schema: {schema_version}.\nReasons:\n{err}"
)