Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: addition of cli command for schema validation
* NEW Adds a new command which validates schemas. Command comes with an '--ignore_indexes' option. Signed-off-by: PaulinaLach <paulina.malgorzata.lach@cern.ch>
- Loading branch information
Paulina1
committed
Sep 30, 2016
1 parent
e1ae7b7
commit 3ed2c8d
Showing
19 changed files
with
555 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
Installation | ||
============ | ||
Invenio's DoSchema module is on PyPI so all you need is: | ||
|
||
.. code-block:: console | ||
$ pip install doschema | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
as an Intergovernmental Organization or submit itself to any jurisdiction. | ||
|
||
|
||
===== | ||
Usage | ||
===== | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of DoSchema. | ||
# Copyright (C) 2016 CERN. | ||
# | ||
# DoSchema is free software; you can redistribute it | ||
# and/or modify it under the terms of the GNU General Public License as | ||
# published by the Free Software Foundation; either version 2 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# DoSchema is distributed in the hope that it will be | ||
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with DoSchema; if not, write to the | ||
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307, USA. | ||
# | ||
# In applying this license, CERN does not | ||
# waive the privileges and immunities granted to it by virtue of its status | ||
# as an Intergovernmental Organization or submit itself to any jurisdiction. | ||
|
||
"""CLI commands.""" | ||
|
||
import json | ||
from io import open | ||
|
||
import click | ||
|
||
import doschema.validation | ||
from doschema.errors import JSONSchemaCompatibilityError | ||
from doschema.utils import detect_encoding | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
"""CLI group.""" | ||
pass # pragma: no cover | ||
|
||
|
||
@cli.command() | ||
@click.argument( | ||
'schemas', | ||
type=click.Path( | ||
exists=True, | ||
file_okay=True, | ||
dir_okay=False, | ||
resolve_path=True | ||
), | ||
nargs=-1 | ||
) | ||
@click.option( | ||
'--ignore_index/--with_index', | ||
default=True, | ||
help="Enable/Disable conflict detection between different indices of " | ||
"array fields in JSON-Schemas. Enabled by default." | ||
) | ||
def validate(schemas, ignore_index): | ||
"""Main function for cli.""" | ||
try: | ||
schema_validator = doschema.validation.JSONSchemaValidator( | ||
ignore_index) | ||
for schema in schemas: | ||
with open(schema, 'rb') as infile: | ||
byte_file = infile.read() | ||
encoding = detect_encoding(byte_file) | ||
string_file = byte_file.decode(encoding) | ||
json_schema = json.loads(string_file) | ||
schema_validator.validate(json_schema, schema) | ||
except JSONSchemaCompatibilityError as e: | ||
raise click.ClickException(str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of DoSchema. | ||
# Copyright (C) 2016 CERN. | ||
# | ||
# DoSchema is free software; you can redistribute it | ||
# and/or modify it under the terms of the GNU General Public License as | ||
# published by the Free Software Foundation; either version 2 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# DoSchema is distributed in the hope that it will be | ||
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with DoSchema; if not, write to the | ||
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307, USA. | ||
# | ||
# In applying this license, CERN does not | ||
# waive the privileges and immunities granted to it by virtue of its status | ||
# as an Intergovernmental Organization or submit itself to any jurisdiction. | ||
|
||
"""Utils module.""" | ||
|
||
import chardet | ||
|
||
|
||
def detect_encoding(byte_file): | ||
"""Detect encoding of a file with schema.""" | ||
encoding = chardet.detect(byte_file)['encoding'] | ||
if encoding in ['UTF-16BE', 'UTF-16LE']: | ||
encoding = 'UTF-16' | ||
elif encoding in ['UTF-32BE', 'UTF-32LE']: | ||
encoding = 'UTF-32' | ||
return encoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of DoSchema. | ||
# Copyright (C) 2016 CERN. | ||
# | ||
# DoSchema is free software; you can redistribute it | ||
# and/or modify it under the terms of the GNU General Public License as | ||
# published by the Free Software Foundation; either version 2 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# DoSchema is distributed in the hope that it will be | ||
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with DoSchema; if not, write to the | ||
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307, USA. | ||
# | ||
# In applying this license, CERN does not | ||
# waive the privileges and immunities granted to it by virtue of its status | ||
# as an Intergovernmental Organization or submit itself to any jurisdiction. | ||
|
||
|
||
"""In this example, there is no option set, so by default | ||
"--ignore_index" option is enabled. | ||
Thus array indexes are ignored and for each array field, all items have to be | ||
of the same type. | ||
Run this example: | ||
.. code-block:: console | ||
$ cd examples | ||
$ python app.py | ||
The same result could be created with the cli: | ||
.. code-block:: console | ||
$ doschema file1.json file2.json | ||
""" | ||
|
||
import json | ||
from io import open | ||
|
||
import doschema.validation | ||
from doschema.utils import detect_encoding | ||
|
||
|
||
schemas = [ | ||
'./examples/jsonschema_ignore_index_option.json' | ||
] | ||
|
||
schema_validator = doschema.validation.JSONSchemaValidator() | ||
for schema in schemas: | ||
with open(schema, 'rb') as infile: | ||
byte_file = infile.read() | ||
encoding = detect_encoding(byte_file) | ||
string_file = byte_file.decode(encoding) | ||
json_schema = json.loads(string_file) | ||
schema_validator.validate(json_schema, schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of DoSchema. | ||
# Copyright (C) 2016 CERN. | ||
# | ||
# DoSchema is free software; you can redistribute it | ||
# and/or modify it under the terms of the GNU General Public License as | ||
# published by the Free Software Foundation; either version 2 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# DoSchema is distributed in the hope that it will be | ||
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with DoSchema; if not, write to the | ||
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307, USA. | ||
# | ||
# In applying this license, CERN does not | ||
# waive the privileges and immunities granted to it by virtue of its status | ||
# as an Intergovernmental Organization or submit itself to any jurisdiction. | ||
|
||
|
||
"""In this example, "with_index" option is enabled. | ||
Thus, types in arrays will be checked with their indexes and items of the same | ||
array can have different types. | ||
Run this example: | ||
.. code-block:: console | ||
$ cd examples | ||
$ python app.py | ||
The same result could be created with the cli: | ||
.. code-block:: console | ||
$ doschema file1.json file2.json --with_index | ||
""" | ||
|
||
import json | ||
from io import open | ||
|
||
import doschema.validation | ||
from doschema.utils import detect_encoding | ||
|
||
|
||
schemas = [ | ||
'./examples/jsonschema_with_index_option.json' | ||
] | ||
|
||
schema_validator = doschema.validation.JSONSchemaValidator(ignore_index=False) | ||
for schema in schemas: | ||
with open(schema, 'rb') as infile: | ||
byte_file = infile.read() | ||
encoding = detect_encoding(byte_file) | ||
string_file = byte_file.decode(encoding) | ||
json_schema = json.loads(string_file) | ||
schema_validator.validate(json_schema, schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type":"object", | ||
"properties": { | ||
"abc": { | ||
"type":"integer" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"type":"object", | ||
"properties":{ | ||
"experiment_info":{ | ||
"type":"array", | ||
"items":[ | ||
{ | ||
"type":"object", | ||
"properties":{ | ||
"field_A":{ | ||
"type":"string" | ||
} | ||
} | ||
},{ | ||
"type":"object", | ||
"properties":{ | ||
"field_A":{ | ||
"type":"string" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.