Skip to content

Commit 592093c

Browse files
committed
When codelist is used on an array, the enum is set for items in that array
Needed for Open-Telecoms-Data/lib-cove-ofds#81
1 parent 79358dc commit 592093c

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
### Added
1010

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

1314
### Removed
1415

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).
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)