Skip to content

Commit

Permalink
feat: support enum of objects (#1) (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-schoonjans authored Mar 5, 2024
1 parent cfd9a0e commit 46684c6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions jsf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def __parse_definition(self, name: str, path: str, schema: Dict[str, Any]) -> Al
enum_list = schema["enum"]
assert len(enum_list) > 0, "Enum List is Empty"
assert all(
isinstance(item, (int, float, str, type(None))) for item in enum_list
), "Enum Type is not null, int, float or string"
isinstance(item, (int, float, str, dict, type(None))) for item in enum_list
), "Enum Type is not null, int, float, string or dict"
return JSFEnum.from_dict(
{
"name": name,
Expand Down
2 changes: 1 addition & 1 deletion jsf/schema_types/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class JSFEnum(BaseSchema):
enum: Optional[List[Union[str, int, float, None]]] = []
enum: Optional[List[Union[str, int, float, dict, None]]] = []
model_config = ConfigDict()

def generate(self, context: Dict[str, Any]) -> Optional[Union[str, int, float]]:
Expand Down
13 changes: 13 additions & 0 deletions jsf/tests/data/object-enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "object",
"enum": [
{
"code": "1",
"value": "CHILD"
},
{
"code": "2",
"value": "ADULT"
}
]
}
12 changes: 12 additions & 0 deletions jsf/tests/test_default_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def test_fake_string_enum(TestData):
assert all(p.generate() in ["Street", "Avenue", "Boulevard"] for _ in range(100))


def test_fake_object_enum(TestData):
with open(TestData / "object-enum.json") as file:
schema = json.load(file)
p = JSF(schema)

assert isinstance(p.generate(), dict)
assert all(
p.generate() in [{"code": "1", "value": "CHILD"}, {"code": "2", "value": "ADULT"}]
for _ in range(100)
)


def test_fake_int(TestData):
with open(TestData / "integer.json") as file:
schema = json.load(file)
Expand Down

0 comments on commit 46684c6

Please sign in to comment.