diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e494b7..c9b03b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/compiletojsonschema/compiletojsonschema.py b/compiletojsonschema/compiletojsonschema.py index b03d6d1..a670635 100644 --- a/compiletojsonschema/compiletojsonschema.py +++ b/compiletojsonschema/compiletojsonschema.py @@ -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"] diff --git a/docs/features.rst b/docs/features.rst index b4cbcc3..580618b 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -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). diff --git a/setup.py b/setup.py index 119bb3b..27b59e7 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name="compiletojsonschema", - version="0.4.0", + version="0.5.0", author="Open Data Services", author_email="code@opendataservices.coop", url="https://github.com/OpenDataServices/compile-to-json-schema", diff --git a/tests/fixtures/codelists/schema-closed-codelist-array.json b/tests/fixtures/codelists/schema-closed-codelist-array.json new file mode 100644 index 0000000..a8e4320 --- /dev/null +++ b/tests/fixtures/codelists/schema-closed-codelist-array.json @@ -0,0 +1,15 @@ +{ + "properties": { + "pet": { + "title": "Pet", + "codelist": "pets.csv", + "openCodelist": false, + "type": "array", + "items": { + "type": [ + "string" + ] + } + } + } +} diff --git a/tests/test_codelist.py b/tests/test_codelist.py index 2c2ae38..8a6278a 100644 --- a/tests/test_codelist.py +++ b/tests/test_codelist.py @@ -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"]