Skip to content

Commit 101cf1b

Browse files
authored
Generate ISO country schemas using .jq templates (#67)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 83eba33 commit 101cf1b

5 files changed

Lines changed: 109 additions & 222 deletions

File tree

Makefile

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ GENERATED = \
8585
schemas/xbrl/utr/volume-item-type-normative.json \
8686
schemas/xbrl/utr/volume-item-type.json \
8787
schemas/xbrl/utr/volume-per-monetary-item-type-normative.json \
88-
schemas/xbrl/utr/volume-per-monetary-item-type.json
88+
schemas/xbrl/utr/volume-per-monetary-item-type.json \
89+
schemas/iso/country/2020/alpha-2.json \
90+
schemas/iso/country/2020/alpha-3.json \
91+
schemas/iso/country/2020/numeric.json
8992

9093
# TODO: Make `jsonschema fmt` automatically detect test files
9194
all: common test
@@ -113,25 +116,37 @@ lint: common
113116
test:
114117
$(JSONSCHEMA) test ./test
115118

116-
build/iso/currency/list-%.json: scripts/xml2json.py vendor/data/iso/currency/list-%.xml
119+
build/iso/currency/list-%.json: \
120+
scripts/xml2json.py \
121+
vendor/data/iso/currency/list-%.xml
117122
$(PYTHON) $< $(word 2,$^) $@
118-
schemas/iso/currency/2015/historical/alpha-code.json: build/iso/currency/list-three.json templates/iso/currency/2015/historical/alpha-code.jq
123+
schemas/iso/currency/2015/historical/alpha-code.json: \
124+
build/iso/currency/list-three.json \
125+
templates/iso/currency/2015/historical/alpha-code.jq
119126
$(MKDIRP) $(dir $@)
120127
$(JQ) --from-file $(word 2,$^) $< > $@
121128
$(JSONSCHEMA) fmt $@
122-
schemas/iso/currency/2015/historical/alpha-currency.json: build/iso/currency/list-three.json templates/iso/currency/2015/historical/alpha-currency.jq
129+
schemas/iso/currency/2015/historical/alpha-currency.json: \
130+
build/iso/currency/list-three.json \
131+
templates/iso/currency/2015/historical/alpha-currency.jq
123132
$(MKDIRP) $(dir $@)
124133
$(JQ) --from-file $(word 2,$^) $< > $@
125134
$(JSONSCHEMA) fmt $@
126-
schemas/iso/currency/2015/historical/numeric-code.json: build/iso/currency/list-three.json templates/iso/currency/2015/historical/numeric-code.jq
135+
schemas/iso/currency/2015/historical/numeric-code.json: \
136+
build/iso/currency/list-three.json \
137+
templates/iso/currency/2015/historical/numeric-code.jq
127138
$(MKDIRP) $(dir $@)
128139
$(JQ) --from-file $(word 2,$^) $< > $@
129140
$(JSONSCHEMA) fmt $@
130-
schemas/iso/currency/2015/historical/numeric-currency.json: build/iso/currency/list-three.json templates/iso/currency/2015/historical/numeric-currency.jq
141+
schemas/iso/currency/2015/historical/numeric-currency.json: \
142+
build/iso/currency/list-three.json \
143+
templates/iso/currency/2015/historical/numeric-currency.jq
131144
$(MKDIRP) $(dir $@)
132145
$(JQ) --from-file $(word 2,$^) $< > $@
133146
$(JSONSCHEMA) fmt $@
134-
schemas/iso/currency/2015/%.json: build/iso/currency/list-one.json templates/iso/currency/2015/%.jq
147+
schemas/iso/currency/2015/%.json: \
148+
build/iso/currency/list-one.json \
149+
templates/iso/currency/2015/%.jq
135150
$(MKDIRP) $(dir $@)
136151
$(JQ) --from-file $(word 2,$^) $< > $@
137152
$(JSONSCHEMA) fmt $@
@@ -144,10 +159,12 @@ schemas/xbrl/utr/%.json: build/xbrl/utr/utr.json templates/xbrl/utr/%.jq
144159
$(JQ) --from-file $(word 2,$^) $< > $@
145160
$(JSONSCHEMA) fmt $@
146161

147-
generate-iso-language: generate/iso/language/main.py
148-
$(PYTHON) $<
149-
generate-iso-country: generate/iso/country/main.py
150-
$(PYTHON) $<
162+
schemas/iso/country/2020/%.json: \
163+
vendor/iso3166/all/all.json \
164+
templates/iso/country/2020/%.jq
165+
$(MKDIRP) $(dir $@)
166+
$(JQ) --from-file $(word 2,$^) $< > $@
167+
$(JSONSCHEMA) fmt $@
151168

152169

153170
# TODO: Add a `jsonschema pkg` command instead

generate/iso/country/main.py

Lines changed: 0 additions & 211 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "ISO 3166-1:2020 Alpha-2 Country Code",
4+
"description": "A two-letter country code from ISO 3166-1",
5+
"examples": (
6+
map(select(.["alpha-2"] != null and .["alpha-2"] != ""))
7+
| sort_by(.["alpha-2"])
8+
| .[0:4]
9+
| map(.["alpha-2"])
10+
),
11+
"x-license": "https://github.com/sourcemeta/std/blob/main/LICENSE",
12+
"x-links": ["https://www.iso.org/iso-3166-country-codes.html"],
13+
"anyOf": (
14+
map(select(.["alpha-2"] != null and .["alpha-2"] != ""))
15+
| sort_by(.["alpha-2"])
16+
| map({
17+
"title": .name,
18+
"x-alpha-3": .["alpha-3"],
19+
"x-numeric": (.["country-code"] | tonumber)
20+
} +
21+
(if .region != null and .region != "" then {"x-region": .region} else {} end) +
22+
(if .["sub-region"] != null and .["sub-region"] != "" then {"x-sub-region": .["sub-region"]} else {} end) +
23+
{
24+
"const": .["alpha-2"]
25+
})
26+
)
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "ISO 3166-1:2020 Alpha-3 Country Code",
4+
"description": "A three-letter country code from ISO 3166-1",
5+
"examples": (
6+
map(select(.["alpha-3"] != null and .["alpha-3"] != ""))
7+
| sort_by(.["alpha-3"])
8+
| .[0:4]
9+
| map(.["alpha-3"])
10+
),
11+
"x-license": "https://github.com/sourcemeta/std/blob/main/LICENSE",
12+
"x-links": ["https://www.iso.org/iso-3166-country-codes.html"],
13+
"anyOf": (
14+
map(select(.["alpha-3"] != null and .["alpha-3"] != ""))
15+
| sort_by(.["alpha-3"])
16+
| map({
17+
"title": .name,
18+
"x-alpha-2": .["alpha-2"],
19+
"x-numeric": (.["country-code"] | tonumber)
20+
} +
21+
(if .region != null and .region != "" then {"x-region": .region} else {} end) +
22+
(if .["sub-region"] != null and .["sub-region"] != "" then {"x-sub-region": .["sub-region"]} else {} end) +
23+
{
24+
"const": .["alpha-3"]
25+
})
26+
)
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "ISO 3166-1:2020 Numeric Country Code",
4+
"description": "A three-digit numeric country code from ISO 3166-1",
5+
"examples": (
6+
map(select(.["country-code"] != null and .["country-code"] != ""))
7+
| sort_by(.["country-code"])
8+
| .[0:4]
9+
| map(.["country-code"] | tonumber)
10+
),
11+
"x-license": "https://github.com/sourcemeta/std/blob/main/LICENSE",
12+
"x-links": ["https://www.iso.org/iso-3166-country-codes.html"],
13+
"anyOf": (
14+
map(select(.["country-code"] != null and .["country-code"] != ""))
15+
| sort_by(.["country-code"])
16+
| map({
17+
"title": .name,
18+
"x-alpha-2": .["alpha-2"],
19+
"x-alpha-3": .["alpha-3"]
20+
} +
21+
(if .region != null and .region != "" then {"x-region": .region} else {} end) +
22+
(if .["sub-region"] != null and .["sub-region"] != "" then {"x-sub-region": .["sub-region"]} else {} end) +
23+
{
24+
"const": (.["country-code"] | tonumber)
25+
})
26+
)
27+
}

0 commit comments

Comments
 (0)