Skip to content

Commit

Permalink
When codelist is used on an array, the enum is set for items in that …
Browse files Browse the repository at this point in the history
  • Loading branch information
odscjames committed Sep 22, 2023
1 parent 79358dc commit 5baf141
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

- Process anyOf and allOf and well as oneOf https://github.com/OpenDataServices/compile-to-json-schema/issues/28
- When codelist is used on an array, the enum is set for items in that array

### Removed

Expand Down
7 changes: 6 additions & 1 deletion compiletojsonschema/compiletojsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ def __process(self, source):
if len(row) > 0 and row[0]:
values.append(row[0])
if values:
out["enum"] = values
if out.get("type") == "array" and isinstance(
out.get("items"), dict
):
out["items"]["enum"] = values
else:
out["enum"] = values
else:
raise CodeListNotFoundException(
"Can not find codelist: " + source["codelist"]
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/codelists/schema-closed-codelist-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"properties": {
"pet": {
"title": "Pet",
"codelist": "pets.csv",
"openCodelist": false,
"type": "array",
"items": {
"type": [
"string"
]
}
}
}
}
24 changes: 24 additions & 0 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,27 @@ def test_closed_codelist():
out = ctjs.get()

assert out["properties"]["pet"]["enum"] == ["Dog", "Cat", "Parrot"]


def test_closed_codelist_array():

input_filename = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"fixtures",
"codelists",
"schema-closed-codelist-array.json",
)

codelist_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"fixtures",
"codelists",
)

ctjs = CompileToJsonSchema(
input_filename=input_filename, codelist_base_directory=codelist_dir
)
out = ctjs.get()

assert not out["properties"]["pet"].get("enum")
assert out["properties"]["pet"]["items"]["enum"] == ["Dog", "Cat", "Parrot"]

0 comments on commit 5baf141

Please sign in to comment.