Skip to content

Commit 220eb58

Browse files
author
James (ODSC)
authored
Merge pull request #37 from OpenDataServices/2023-09-22
When codelist is used on an array, the enum is set for items in that array
2 parents 79358dc + 034e8e3 commit 220eb58

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.5.0] - 2023-09-22
10+
911
### Added
1012

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

1316
### Removed
1417

compiletojsonschema/compiletojsonschema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ def __process(self, source):
9393
if len(row) > 0 and row[0]:
9494
values.append(row[0])
9595
if values:
96-
out["enum"] = values
96+
if out.get("type") == "array" and isinstance(
97+
out.get("items"), dict
98+
):
99+
out["items"]["enum"] = values
100+
else:
101+
out["enum"] = values
97102
else:
98103
raise CodeListNotFoundException(
99104
"Can not find codelist: " + source["codelist"]

docs/features.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ And the resulting output would be:
139139
140140
You can pass a base directory that will be searched for codelists. If not passed, the current working directory is searched
141141

142+
This will work for situations where one value can be selected and situations with an array where multiple values can be selected.
143+
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.
144+
142145
Finally, if the `openCodelist` variable exists and that is set to true, nothing will be done. This means a codelist is
143146
"Open" (ie - allows the user to add any values they want) as opposed to "Closed" (ie - the user can only add the values
144147
in the codelist).

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name="compiletojsonschema",
7-
version="0.4.0",
7+
version="0.5.0",
88
author="Open Data Services",
99
author_email="[email protected]",
1010
url="https://github.com/OpenDataServices/compile-to-json-schema",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"properties": {
3+
"pet": {
4+
"title": "Pet",
5+
"codelist": "pets.csv",
6+
"openCodelist": false,
7+
"type": "array",
8+
"items": {
9+
"type": [
10+
"string"
11+
]
12+
}
13+
}
14+
}
15+
}

tests/test_codelist.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,27 @@ def test_closed_codelist():
9696
out = ctjs.get()
9797

9898
assert out["properties"]["pet"]["enum"] == ["Dog", "Cat", "Parrot"]
99+
100+
101+
def test_closed_codelist_array():
102+
103+
input_filename = os.path.join(
104+
os.path.dirname(os.path.realpath(__file__)),
105+
"fixtures",
106+
"codelists",
107+
"schema-closed-codelist-array.json",
108+
)
109+
110+
codelist_dir = os.path.join(
111+
os.path.dirname(os.path.realpath(__file__)),
112+
"fixtures",
113+
"codelists",
114+
)
115+
116+
ctjs = CompileToJsonSchema(
117+
input_filename=input_filename, codelist_base_directory=codelist_dir
118+
)
119+
out = ctjs.get()
120+
121+
assert not out["properties"]["pet"].get("enum")
122+
assert out["properties"]["pet"]["items"]["enum"] == ["Dog", "Cat", "Parrot"]

0 commit comments

Comments
 (0)