Skip to content

Commit

Permalink
Added CLI format checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke authored and Julian committed May 4, 2014
1 parent b535522 commit 0eb8a89
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions jsonschema/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@
import argparse
import json

from jsonschema import validate
from . import (
validate, Draft4Validator, Draft3Validator,
draft3_format_checker, draft4_format_checker,
)
from .validators import validator_for

def main():
parser = argparse.ArgumentParser(description='JSON Schema validator')
parser.add_argument('schema', help='filename of the JSON Schema')
parser.add_argument('document', help='filename of the JSON document to validate')
parser.add_argument('--format', help='validate value format', action='store_true')
args = parser.parse_args()

schema = json.load(open(args.schema, 'r'))
document = json.load(open(args.document, 'r'))

validate(document, schema)
validator = validator_for(schema)
if args.format:
if validator == Draft4Validator:
format_checker = draft4_format_checker
elif validator == Draft3Validator:
format_checker = draft3_format_checker
else:
raise NotImplementedError("No format validator for %s specified"
% validator.__name__)
else:
format_checker = None

validate(document, schema, validator, format_checker=format_checker)
# validate raises if the document is invalid, and will show a Traceback to
# the user. If the document is valid, show a congratulating message.
print("√ JSON document is valid.")
Expand Down

0 comments on commit 0eb8a89

Please sign in to comment.