def generate_schema_dict(self, example_event, path_list = [], default_required = True, default_nullable = False, required = False, nullable = False):
        schema_dict = SchemaGenerator(example_event).to_dict(required = default_required, nullable = default_nullable)

        for path in path_list:
            SchemaGenerator.set_required(schema_dict, path, required = required, nullable = nullable)

        # Sanity check that the example event still passes validation
        jsonschema.validate(example_event, schema_dict)

        return schema_dict, copy.deepcopy(example_event)
    def test_conversion(self):
        generator = SchemaGenerator.from_json(fixtures.json_1)

        gotten = generator.to_dict()
        expected = json.loads(fixtures.json_schema_1)

        self.assertEqual(gotten, expected)
    def test_generator_should_return_text_plain_json_schema(self):
        generator = SchemaGenerator.from_json('{"p1": 1, "p2": "str", "p3": false}')

        gotten = normalize_json(generator.to_json())
        expected = normalize_json(fixtures.object_with_properties_schema)

        self.assertEqual(gotten, expected)
Пример #4
0
    def test_generator_should_return_text_plain_json_schema(self):
        generator = SchemaGenerator.from_json('{"p1": 1, "p2": "str", "p3": false}')

        gotten = normalize_json(generator.to_json())
        expected = normalize_json(fixtures.object_with_properties_schema)

        self.assertEqual(gotten, expected)
    def test_conversion(self):
        generator = SchemaGenerator.from_json(fixtures.json_1)

        gotten = generator.to_dict()
        expected = json.loads(fixtures.json_schema_1)

        self.assertEqual(gotten, expected)
Пример #6
0
    def generate_schema_dict(self,
                             example_event,
                             path_list=[],
                             default_required=True,
                             default_nullable=False,
                             required=False,
                             nullable=False):
        schema_dict = SchemaGenerator(example_event).to_dict(
            required=default_required, nullable=default_nullable)

        for path in path_list:
            SchemaGenerator.set_required(schema_dict,
                                         path,
                                         required=required,
                                         nullable=nullable)

        # Sanity check that the example event still passes validation
        jsonschema.validate(example_event, schema_dict)

        return schema_dict, copy.deepcopy(example_event)
Пример #7
0
def generate_json_schema(json_data, **kwargs):
    if isinstance(json_data, dict):
        json_data = json.dumps(json_data)
    if isinstance(json_data, str):
        try:
            from json_schema_generator import SchemaGenerator
            json.dumps(json_data)
        except:
            raise ValueError
        json_schema = SchemaGenerator.from_json(json_data)
        return json_schema.to_json(**kwargs)
    def test_generator_should_convert_object_with_properties(self):
        generator = SchemaGenerator.from_json('{"p1": 1, "p2": "str", "p3": false}')
        expected = json.loads(fixtures.object_with_properties_schema)

        self.assertEqual(generator.to_dict(), expected)
    def test_generator_should_convert_array(self):
        generator = SchemaGenerator.from_json('[]')
        expected = json.loads(fixtures.array_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #10
0
    def test_generator_should_convert_object(self):
        generator = SchemaGenerator.from_json('{}')
        expected = json.loads(fixtures.object_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #11
0
    def test_generator_should_convert_array_with_homogeneous_items(self):
        generator = SchemaGenerator.from_json('[1, 2, 3]')
        expected = json.loads(fixtures.array_of_number_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #12
0
    def test_generator_should_convert_string(self):
        generator = SchemaGenerator.from_json('"str"')
        expected = json.loads(fixtures.string_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #13
0
    def test_base_object_from_json_should_match_the_submitted(self):
        schema_dict = json.loads(fixtures.json_schema_1)
        generator = SchemaGenerator.from_json(fixtures.json_schema_1)

        self.assertIsInstance(generator, SchemaGenerator)
        self.assertEqual(generator.base_object, schema_dict)
    def test_base_object_from_json_should_match_the_submitted(self):
        schema_dict = json.loads(fixtures.json_schema_1)
        generator = SchemaGenerator.from_json(fixtures.json_schema_1)

        self.assertIsInstance(generator, SchemaGenerator)
        self.assertEqual(generator.base_object, schema_dict)
Пример #15
0
__author__ = 'jp'

from json_schema_generator import SchemaGenerator

input_file = "sample.json"
output_file = "sample.json_schema"

# Convert json to json schema
with open(input_file, 'r') as json_file:
    generator = SchemaGenerator.from_json(json_file.read())
    json_schema_data = generator.to_json(indent=4)

with open(output_file, 'w') as json_schema_file:
    json_schema_file.write(json_schema_data)

import json

with open("sample_ver_4.json_schema", 'r') as json_schema_file:
    json_schema_data = json.loads(json_schema_file.read())

# It generates json schema based on draft 3, if you want latest version use other tools
# Useful online tool to generate schema
# http://jsonschema.net/#/

# Create json schema objects

#Option 1
# import python_jsonschema_objects as pjs
# builder = pjs.ObjectBuilder(output_file)
# ns = builder.build_classes()
    def test_generator_should_instanciate_from_json(self):
        generator = SchemaGenerator.from_json(fixtures.json_1)

        self.assertIsInstance(generator, SchemaGenerator)
Пример #17
0
    def test_instance(self):
        schema_dict = json.loads(fixtures.json_schema_1)
        generator = SchemaGenerator(schema_dict)

        self.assertIsInstance(generator, SchemaGenerator)
        self.assertEqual(generator.base_object, schema_dict)
    def test_generator_should_convert_string(self):
        generator = SchemaGenerator.from_json('"str"')
        expected = json.loads(fixtures.string_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #19
0
    def test_generator_should_instanciate_from_json(self):
        generator = SchemaGenerator.from_json(fixtures.json_1)

        self.assertIsInstance(generator, SchemaGenerator)
    def test_generator_should_convert_boolean(self):
        generator = SchemaGenerator.from_json("true")
        expected = json.loads(fixtures.boolean_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #21
0
    def test_generator_should_convert_boolean(self):
        generator = SchemaGenerator.from_json('true')
        expected = json.loads(fixtures.boolean_json_schema)

        self.assertEqual(generator.to_dict(), expected)
    def test_generator_should_convert_array_with_homogeneous_items(self):
        generator = SchemaGenerator.from_json("[1, 2, 3]")
        expected = json.loads(fixtures.array_of_number_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #23
0
    def test_generator_should_convert_array_with_hetereogeneous_items(self):
        generator = SchemaGenerator.from_json('["a", 1, {}]')
        expected = json.loads(fixtures.mixed_array_json_schema)

        self.assertEqual(generator.to_dict(), expected)
    def test_generator_should_convert_array_with_hetereogeneous_items(self):
        generator = SchemaGenerator.from_json('["a", 1, {}]')
        expected = json.loads(fixtures.mixed_array_json_schema)

        self.assertEqual(generator.to_dict(), expected)
Пример #25
0
    def test_generator_should_convert_object_with_properties(self):
        generator = SchemaGenerator.from_json(
            '{"p1": 1, "p2": "str", "p3": false}')
        expected = json.loads(fixtures.object_with_properties_schema)

        self.assertEqual(generator.to_dict(), expected)
    def test_generator_should_convert_object(self):
        generator = SchemaGenerator.from_json("{}")
        expected = json.loads(fixtures.object_json_schema)

        self.assertEqual(generator.to_dict(), expected)
__author__ = 'jp'

from json_schema_generator import SchemaGenerator

input_file = "sample.json"
output_file = "sample.json_schema"

# Convert json to json schema
with open(input_file, 'r') as json_file:
    generator = SchemaGenerator.from_json(json_file.read())
    json_schema_data = generator.to_json(indent=4)

with open(output_file, 'w') as json_schema_file:
    json_schema_file.write(json_schema_data)

import json

with open("sample_ver_4.json_schema", 'r') as json_schema_file:
    json_schema_data = json.loads(json_schema_file.read())

# It generates json schema based on draft 3, if you want latest version use other tools
# Useful online tool to generate schema
# http://jsonschema.net/#/

# Create json schema objects

#Option 1
# import python_jsonschema_objects as pjs
# builder = pjs.ObjectBuilder(output_file)
# ns = builder.build_classes()
Пример #28
0
def step_impl(context):
    schema = SchemaGenerator.from_json(context.text)
    validate(context.response.json(), schema.to_dict())