diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3fae95c04..9868b3e89 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,14 +6,19 @@ dcicutils Change Log ---------- - -8.8.6 -===== +8.10.0 +====== * Added merge capabilities to structured_data. (IN PROGRESS: 2025-05-25) * Added Question class to command_utils (factored out of smaht-submitr). +8.9.0 +===== + +* Add more schema parsing functions to `schema_utils`. + + 8.8.6 ===== diff --git a/dcicutils/schema_utils.py b/dcicutils/schema_utils.py index b5b1bf425..82aa6bb12 100644 --- a/dcicutils/schema_utils.py +++ b/dcicutils/schema_utils.py @@ -24,6 +24,7 @@ class JsonSchemaConstants: class EncodedSchemaConstants: + DESCRIPTION = "description" IDENTIFYING_PROPERTIES = "identifyingProperties" LINK_TO = "linkTo" MERGE_REF = "$merge" @@ -187,6 +188,21 @@ def get_one_of_formats(schema: Dict[str, Any]) -> List[str]: ] +def is_link(property_schema: Dict[str, Any]) -> bool: + """Is property schema a link?""" + return bool(property_schema.get(SchemaConstants.LINK_TO)) + + +def get_enum(property_schema: Dict[str, Any]) -> List[str]: + """Return the enum of a property schema.""" + return property_schema.get(SchemaConstants.ENUM, []) + + +def get_description(schema: Dict[str, Any]) -> str: + """Return the description of a schema.""" + return schema.get(SchemaConstants.DESCRIPTION, "") + + class Schema: def __init__(self, schema: dict, type: Optional[str] = None) -> None: diff --git a/pyproject.toml b/pyproject.toml index 304f47444..1a63902a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.8.6.1b14" +version = "8.9.0.1b1" # TODO: To become 8.10.0 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT" diff --git a/test/test_schema_utils.py b/test/test_schema_utils.py index 6b948b8ed..8a928ee3b 100644 --- a/test/test_schema_utils.py +++ b/test/test_schema_utils.py @@ -1,4 +1,4 @@ -from typing import Any, Dict +from typing import Any, Dict, List import pytest from dcicutils import schema_utils @@ -41,7 +41,16 @@ } FORMAT = "email" PATTERN = "some_regex" -STRING_SCHEMA = {"type": "string", "format": FORMAT, "pattern": PATTERN} +ENUM = ["foo", "bar"] +DESCRIPTION = "foo" +STRING_SCHEMA = { + "type": "string", + "format": FORMAT, + "pattern": PATTERN, + "linkTo": "foo", + "enum": ENUM, + "description": DESCRIPTION, +} ARRAY_SCHEMA = {"type": "array", "items": [STRING_SCHEMA]} OBJECT_SCHEMA = {"type": "object", "properties": {"foo": STRING_SCHEMA}} NUMBER_SCHEMA = {"type": "number"} @@ -320,3 +329,37 @@ def test_get_conditional_formats( schema: Dict[str, Any], expected: Dict[str, Any] ) -> None: assert schema_utils.get_conditional_formats(schema) == expected + + +@pytest.mark.parametrize( + "schema,expected", + [ + ({}, False), + (STRING_SCHEMA, True), + (FORMAT_SCHEMA, False), + ], +) +def test_is_link(schema: Dict[str, Any], expected: bool) -> None: + assert schema_utils.is_link(schema) == expected + + +@pytest.mark.parametrize( + "schema,expected", + [ + ({}, []), + (STRING_SCHEMA, ENUM), + ], +) +def test_get_enum(schema: Dict[str, Any], expected: List[str]) -> None: + assert schema_utils.get_enum(schema) == expected + + +@pytest.mark.parametrize( + "schema,expected", + [ + ({}, ""), + (STRING_SCHEMA, DESCRIPTION), + ], +) +def test_get_description(schema: Dict[str, Any], expected: str) -> None: + assert schema_utils.get_description(schema) == expected