Skip to content

Commit

Permalink
feat: extend allowed_values option of JSONSchema for Property(ArrayType)
Browse files Browse the repository at this point in the history
  • Loading branch information
TyShkan committed Apr 11, 2023
1 parent 21d0a14 commit 7b2520b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -68,6 +74,7 @@
JSONSCHEMA_ANNOTATION_WRITEONLY,
append_type,
get_datelike_property_type,
is_array_type,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -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}
Expand Down
19 changes: 19 additions & 0 deletions tests/core/test_jsonschema_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7b2520b

Please sign in to comment.