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)
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 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)
def test_generator_should_convert_object(self): generator = SchemaGenerator.from_json('{}') expected = json.loads(fixtures.object_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)
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)
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)
__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)
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_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_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)
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_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)
def step_impl(context): schema = SchemaGenerator.from_json(context.text) validate(context.response.json(), schema.to_dict())