Add option to read from stdin
This commit is contained in:
parent
1938aa23b7
commit
3ed93dbc9b
@ -1,4 +1,5 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
import sys
|
||||||
import json
|
import json
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import jsonschema.protocols
|
import jsonschema.protocols
|
||||||
@ -11,14 +12,14 @@ from referencing.jsonschema import DRAFT202012
|
|||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Validate a JSON document against a JSON schema"
|
description="Validate a JSON document against a JSON schema"
|
||||||
)
|
)
|
||||||
parser.add_argument("file", type=str, help="the JSON file to validate")
|
parser.add_argument("-f", "--file",
|
||||||
|
help="file containing the JSON document to validate. " +
|
||||||
|
"If absent, the JSON document is read from the standard input")
|
||||||
parser.add_argument("-s", "--schema", required=True,
|
parser.add_argument("-s", "--schema", required=True,
|
||||||
help="file containing the schema definition."
|
help="file containing the schema definition"
|
||||||
)
|
)
|
||||||
parser.add_argument("-r", "--resource", action="append", default=[],
|
parser.add_argument("-r", "--resource", nargs="+", default=[],
|
||||||
help="additional schemas to be added to the registry " +
|
help="additional schemas to be added to the registry for reference resolution")
|
||||||
"for reference resolution. Multiple schema files can " +
|
|
||||||
"be added by specifying the option multiple times.")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@ -34,7 +35,7 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
registry = resource @ registry
|
registry = resource @ registry
|
||||||
# Load the JSON document to validate
|
# Load the JSON document to validate
|
||||||
with open(file=args.file, mode="r") as f:
|
with open(file=args.file, mode="r") if args.file else sys.stdin as f:
|
||||||
document = json.load(f)
|
document = json.load(f)
|
||||||
# Set the validator with the desired schema and check the schema itself before validating the document
|
# Set the validator with the desired schema and check the schema itself before validating the document
|
||||||
validator = jsonschema.validators.validator_for(schema)(schema, registry=registry)
|
validator = jsonschema.validators.validator_for(schema)(schema, registry=registry)
|
||||||
|
Loading…
Reference in New Issue
Block a user