Skip to content

Commit

Permalink
fiboa jsonschema and fiboa validate: Support geometryTypes for …
Browse files Browse the repository at this point in the history
…`geometry` data type #40
  • Loading branch information
m-mohr committed May 9, 2024
1 parent 1cc1759 commit 2473338
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Converter for EuroCrops France dataset
- `fiboa create-geojson`: Show conversion progress
- `fiboa jsonschema` and `fiboa validate`: Support `geometryTypes` for `geometry` data type in GeoJSON

### Changed

Expand Down
57 changes: 33 additions & 24 deletions fiboa_cli/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,45 @@ def convert_schema(prop_schema, datatypes, required = False):

datatype_schema = datatypes[prop_schema['type']].copy()

if required:
if '$ref' in datatype_schema:
if prop_schema['type'] == 'geometry':
geom_types = prop_schema.get('geometryTypes', [])
if isinstance(prop_schema.get('geometryTypes'), list):
datatype_schema = {
"allOf": [
datatype_schema,
{
"not": {
"type": "null"
}
}
"anyOf": [
{"$ref": f"https://geojson.org/schema/{type}.json"} for type in geom_types
]
}
del prop_schema['geometryTypes']

anyOf = datatype_schema.get('anyOf')
allOf = datatype_schema.get('allOf')
oneOf = datatype_schema.get('oneOf')
if required:
not_null_schema = { "not": { "type": "null" } }

if '$ref' in datatype_schema or isinstance(anyOf, list) or isinstance(oneOf, list):
datatype_schema = {
"allOf": [datatype_schema, not_null_schema]
}
elif (isinstance(allOf, list)):
allOf.append(not_null_schema)

else:
if isinstance(datatype_schema.get('type'), str):
datatype_schema['type'] = [datatype_schema['type'], "null"]
elif '$ref' in datatype_schema:
schema_type = datatype_schema.get('type')
null_schema = { "type": "null" }

if '$ref' in datatype_schema:
datatype_schema = {
"allOf": [
datatype_schema,
{
"type": "null"
}
]
"allOf": [datatype_schema, null_schema]
}
elif (isinstance(datatype_schema.get('type'), list)):
datatype_schema.get('type', []).append("null")
elif (isinstance(datatype_schema.get('oneOf'), list)):
datatype_schema.get('oneOf', []).append({"type": "null"})
elif (isinstance(datatype_schema.get('anyOf'), list)):
datatype_schema.get('anyOf', []).append({"type": "null"})
elif isinstance(schema_type, str):
datatype_schema['type'] = [datatype_schema['type'], "null"]
elif (isinstance(schema_type, list)):
datatype_schema["type"].append("null")
elif (isinstance(oneOf, list)):
datatype_schema["oneOf"].append(null_schema)
elif (isinstance(anyOf, list)):
datatype_schema["anyOf"].append(null_schema)
else:
log(f"Making schema {json.dumps(datatype_schema)} optional is not supported by this generator")

Expand Down
5 changes: 4 additions & 1 deletion tests/data-files/jsonschema/test1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"geometry": {
"allOf": [
{
"$ref": "https://geojson.org/schema/Geometry.json"
"anyOf": [
{"$ref": "https://geojson.org/schema/Polygon.json"},
{"$ref": "https://geojson.org/schema/MultiPolygon.json"}
]
},
{ "not": { "type": "null" } }
]
Expand Down
3 changes: 3 additions & 0 deletions tests/data-files/jsonschema/test1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ properties:
minLength: 1
geometry:
type: geometry
geometryTypes:
- Polygon
- MultiPolygon
bbox:
type: bounding-box
area:
Expand Down

0 comments on commit 2473338

Please sign in to comment.