From 7b2520bba3e4c37d33628da507971f952d0060b5 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 12 Apr 2023 01:36:44 +0300 Subject: [PATCH] feat: extend allowed_values option of JSONSchema for Property(ArrayType) --- singer_sdk/typing.py | 14 +++++++++++++- tests/core/test_jsonschema_helpers.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/singer_sdk/typing.py b/singer_sdk/typing.py index 3fe572ffa..45c9073f9 100644 --- a/singer_sdk/typing.py +++ b/singer_sdk/typing.py @@ -10,6 +10,12 @@ Property("id", IntegerType, required=True), Property("foo_or_bar", StringType, allowed_values=["foo", "bar"]), + Property( + "permissions", + ArrayType(th.StringType), + allowed_values=["create", "delete", "insert", "update"], + examples=["insert", "update"], + ), Property("ratio", NumberType, examples=[0.25, 0.75, 1.0]), Property("days_active", IntegerType), Property("updated_on", DateTimeType), @@ -68,6 +74,7 @@ JSONSCHEMA_ANNOTATION_WRITEONLY, append_type, get_datelike_property_type, + is_array_type, ) if TYPE_CHECKING: @@ -477,7 +484,12 @@ def to_dict(self) -> dict: }, ) if self.allowed_values: - type_dict.update({"enum": self.allowed_values}) + type_enum = {"enum": self.allowed_values} + + if is_array_type(self.type_dict): + type_dict["items"].update(type_enum) + else: + type_dict.update(type_enum) if self.examples: type_dict.update({"examples": self.examples}) return {self.name: type_dict} diff --git a/tests/core/test_jsonschema_helpers.py b/tests/core/test_jsonschema_helpers.py index 6c262c412..a3b377cee 100644 --- a/tests/core/test_jsonschema_helpers.py +++ b/tests/core/test_jsonschema_helpers.py @@ -434,6 +434,25 @@ def test_inbuilt_type(json_type: JSONTypeHelper, expected_json_schema: dict): }, {is_integer_type}, ), + ( + Property( + "my_prop10", + ArrayType(StringType), + allowed_values=["create", "delete", "insert", "update"], + examples=["insert", "update"], + ), + { + "my_prop10": { + "type": ["array", "null"], + "items": { + "type": ["string"], + "enum": ["create", "delete", "insert", "update"], + }, + "examples": ["insert", "update"], + }, + }, + {is_array_type, is_string_array_type}, + ), ], ) def test_property_creation(