Skip to content

Commit

Permalink
Merge pull request #37 from OpenDataServices/2023-09-22
Browse files Browse the repository at this point in the history
When codelist is used on an array, the enum is set for items in that array
  • Loading branch information
odscjames authored Sep 22, 2023
2 parents 79358dc + 034e8e3 commit 220eb58
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.5.0] - 2023-09-22

### 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
3 changes: 3 additions & 0 deletions docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ And the resulting output would be:
You can pass a base directory that will be searched for codelists. If not passed, the current working directory is searched

This will work for situations where one value can be selected and situations with an array where multiple values can be selected.
Set `type:"array"` with the codelist field, and start defining the `"items"` key and the tool will put the enum options on the array items for you.

Finally, if the `openCodelist` variable exists and that is set to true, nothing will be done. This means a codelist is
"Open" (ie - allows the user to add any values they want) as opposed to "Closed" (ie - the user can only add the values
in the codelist).
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="compiletojsonschema",
version="0.4.0",
version="0.5.0",
author="Open Data Services",
author_email="[email protected]",
url="https://github.com/OpenDataServices/compile-to-json-schema",
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 220eb58

Please sign in to comment.