diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 013c64297..ea855a513 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,12 @@ Change Log ---------- +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 424704e8f..c7f564026 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.8.6" +version = "8.9.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