Skip to content

Commit

Permalink
Merge pull request #27 from OpenDataServices/schema_input
Browse files Browse the repository at this point in the history
Can pass schema, not just filename
  • Loading branch information
James (ODSC) authored Jul 30, 2021
2 parents 33f85ea + cad63c3 commit 232af28
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.4.0] - 2021-07-30

### Added

- Can pass schema to Python class as well as filename

## [0.3.0] - 2021-01-27

### Added
Expand Down
25 changes: 18 additions & 7 deletions compiletojsonschema/compiletojsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
class CompileToJsonSchema:
def __init__(
self,
input_filename,
input_filename=None,
set_additional_properties_false_everywhere=False,
codelist_base_directory=None,
input_schema=None,
):
if not isinstance(input_schema, dict) and not input_filename:
raise Exception("Must pass input_filename or input_schema")
self.input_filename = input_filename
self.input_schema = input_schema
self.set_additional_properties_false_everywhere = (
set_additional_properties_false_everywhere
)
Expand All @@ -25,12 +29,19 @@ def __init__(
self.codelist_base_directory = os.getcwd()

def get(self):
with open(self.input_filename) as fp:
resolved = jsonref.load(
fp,
object_pairs_hook=OrderedDict,
base_uri=pathlib.Path(os.path.realpath(self.input_filename)).as_uri(),
)
if self.input_filename:
with open(self.input_filename) as fp:
resolved = jsonref.load(
fp,
object_pairs_hook=OrderedDict,
base_uri=pathlib.Path(
os.path.realpath(self.input_filename)
).as_uri(),
)
elif isinstance(self.input_schema, dict):
resolved = jsonref.JsonRef.replace_refs(self.input_schema)
else:
raise Exception("Must pass input_filename or input_schema")

resolved = self.__process(resolved)

Expand Down
12 changes: 12 additions & 0 deletions docs/installing.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Installing
==========

Via pip
-------

It is available at https://pypi.org/project/compiletojsonschema/

.. code-block:: shell-session
pip install compiletojsonschema
Directly
--------

To install,

1) Check out the git repository ( https://github.com/OpenDataServices/compile-to-json-schema ) onto your machine.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="compiletojsonschema",
version="0.3.0",
version="0.4.0",
author="Open Data Services",
author_email="[email protected]",
url="https://github.com/OpenDataServices/compile-to-json-schema",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

from compiletojsonschema.compiletojsonschema import CompileToJsonSchema


def test_no_pass():
"""If you do not pass any inputs (eg input_filename, input_schema) it should raise an exception"""
with pytest.raises(Exception):
CompileToJsonSchema()
28 changes: 28 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
from collections import OrderedDict

from compiletojsonschema.compiletojsonschema import CompileToJsonSchema

Expand All @@ -23,6 +24,28 @@ def test_in_file():
assert out["properties"]["home_address"]["description"] == "Where the person lives"


def test_in_file_pass_as_schema():

input_filename = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"fixtures",
"simple",
"in_file.json",
)

with open(input_filename) as fp:
input_schema = json.load(fp, object_pairs_hook=OrderedDict,)

ctjs = CompileToJsonSchema(input_schema=input_schema)
out_string = ctjs.get_as_string()
out = json.loads(out_string)

assert out["properties"]["work_address"]["title"] == "Work Address"
assert out["properties"]["work_address"]["description"] == "Where the person works"
assert out["properties"]["home_address"]["title"] == "Home Address"
assert out["properties"]["home_address"]["description"] == "Where the person lives"


def test_file_main():

input_filename = os.path.join(
Expand Down Expand Up @@ -65,3 +88,8 @@ def test_file_list_anyof():
out["items"]["oneOf"][1]["properties"]["address"]["description"]
== "Where the person works"
)


def test_passing_empty_schema_is_ok():
ctjs = CompileToJsonSchema(input_schema={})
assert "{}" == ctjs.get_as_string()

0 comments on commit 232af28

Please sign in to comment.