-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
62 lines (54 loc) · 2.57 KB
/
validate.py
File metadata and controls
62 lines (54 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
import json
from pathlib import Path
import click
from jsonschema import Draft202012Validator, FormatChecker
@click.command()
@click.argument("input_path", type=click.Path(exists=True, path_type=Path))
@click.option("--schema", "schema_path", type=click.Path(exists=True, path_type=Path),
default=Path("specs/address-register.schema.json"), show_default=True,
help="Path to JSON Schema for address features")
@click.option("--schema-target", type=click.Choice(["collection", "feature", "properties"], case_sensitive=False),
default="collection", show_default=True,
help="What the input represents for validation")
def main(input_path: Path, schema_path: Path, schema_target: str):
"""Validate input against the schema.
Targets:
- collection: GeoJSON FeatureCollection with address features
- feature: GeoJSON Feature with address properties and Point geometry
- properties: properties object only (no geometry)
"""
with schema_path.open() as f:
schema = json.load(f)
validator = Draft202012Validator(schema, format_checker=FormatChecker())
with input_path.open() as f:
data = json.load(f)
errors = []
if schema_target == "collection":
if data.get("type") != "FeatureCollection":
click.echo("Input is not a GeoJSON FeatureCollection", err=True)
raise SystemExit(2)
# Validate entire collection against $defs.FeatureCollection
for e in validator.iter_errors(data):
errors.append(e.message)
elif schema_target == "feature":
if data.get("type") != "Feature":
click.echo("Input is not a GeoJSON Feature", err=True)
raise SystemExit(2)
# Validate feature by pointing into $defs.Feature via $ref root
# The root schema is FeatureCollection, but it has $defs.Feature
feature_schema = schema["$defs"]["Feature"]
feature_validator = Draft202012Validator(feature_schema, format_checker=FormatChecker())
for e in feature_validator.iter_errors(data):
errors.append(e.message)
else: # properties
props_schema = schema["$defs"]["AddressProperties"]
props_validator = Draft202012Validator(props_schema, format_checker=FormatChecker())
for e in props_validator.iter_errors(data):
errors.append(e.message)
if errors:
click.echo("Validation failed:\n" + "\n".join(f"- {msg}" for msg in errors), err=True)
raise SystemExit(1)
click.echo("Validation OK")
if __name__ == "__main__":
main()