def test_range_validator(): schema = Address() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['floor']['minimum'] == 1 assert dumped['properties']['floor']['maximum'] == 4
def test_range_validator(): schema = Address() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["floor"]["minimum"] == 1 assert dumped["properties"]["floor"]["maximum"] == 4
def test_length_validator_value_error(): class BadSchema(Schema): bob = fields.Integer(validate=validate.Length(min=1, max=3)) schema = BadSchema(strict=True) json_schema = JSONSchema() with pytest.raises(ValueError): json_schema.dump(schema)
def test_unknown_typed_field(self): class Colour(fields.Field): def _jsonschema_type_mapping(self): return { 'type': 'string', } def _serialize(self, value, attr, obj): r, g, b = value r = hex(r)[2:] g = hex(g)[2:] b = hex(b)[2:] return '#' + r + g + b class UserSchema(Schema): name = fields.String(required=True) favourite_colour = Colour() schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data self.assertEqual(dumped['properties']['favourite_colour'], {'type': 'string'})
def test_dump_schema(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert len(schema.fields) > 1 for field_name, field in schema.fields.items(): assert field_name in dumped["properties"]
def test_dump_schema(self): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data self._validate_schema(dumped) self.assertGreater(len(schema.fields), 1) for field_name, field in schema.fields.items(): self.assertIn(field_name, dumped['properties'])
def test_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={'description': 'Brown Cow'}) yourfield = fields.Integer(required=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['myfield']['description'] == 'Brown Cow'
def test_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={"description": "Brown Cow"}) yourfield = fields.Integer(required=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["myfield"]["description"] == "Brown Cow"
def test_range_non_number_error(): class TestSchema(Schema): foo = fields.String(validate=validate.Range(max=4)) schema = TestSchema() json_schema = JSONSchema() with pytest.raises(UnsupportedValueError): json_schema.dump(schema)
def test_union_based(): class TestNestedSchema(Schema): field_1 = fields.String() field_2 = fields.Integer() class TestSchema(Schema): union_prop = Union( [fields.String(), fields.Integer(), fields.Nested(TestNestedSchema)] ) # Should be sorting of fields schema = TestSchema() json_schema = JSONSchema() data = json_schema.dump(schema) # Expect only the `anyOf` key assert "anyOf" in data["definitions"]["TestSchema"]["properties"]["union_prop"] assert len(data["definitions"]["TestSchema"]["properties"]["union_prop"]) == 1 string_schema = {"type": "string", "title": ""} integer_schema = {"type": "string", "title": ""} referenced_nested_schema = { "type": "object", "$ref": "#/definitions/TestNestedSchema", } actual_nested_schema = { "type": "object", "properties": { "field_1": {"type": "string", "title": "field_1"}, "field_2": {"type": "number", "title": "field_2", "format": "integer"}, }, "additionalProperties": False, } assert ( string_schema in data["definitions"]["TestSchema"]["properties"]["union_prop"]["anyOf"] ) assert ( integer_schema in data["definitions"]["TestSchema"]["properties"]["union_prop"]["anyOf"] ) assert ( referenced_nested_schema in data["definitions"]["TestSchema"]["properties"]["union_prop"]["anyOf"] ) assert data["definitions"]["TestNestedSchema"] == actual_nested_schema # Expect three possible schemas for the union type assert ( len(data["definitions"]["TestSchema"]["properties"]["union_prop"]["anyOf"]) == 3 )
def test_length_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['name']['minLength'] == 1 assert dumped['properties']['name']['maxLength'] == 255 assert dumped['properties']['addresses']['minItems'] == 1 assert dumped['properties']['addresses']['maxItems'] == 3 assert dumped['properties']['const']['minLength'] == 50 assert dumped['properties']['const']['maxLength'] == 50
def test_length_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["name"]["minLength"] == 1 assert dumped["properties"]["name"]["maxLength"] == 255 assert dumped["properties"]["addresses"]["minItems"] == 1 assert dumped["properties"]["addresses"]["maxItems"] == 3 assert dumped["properties"]["const"]["minLength"] == 50 assert dumped["properties"]["const"]["maxLength"] == 50
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert ( dumped['definitions']['UserSchema']['properties']['sex']['enum'] == [ 'male', 'female', 'non_binary', 'other' ]) assert (dumped['definitions']['UserSchema']['properties']['sex'] ['enumNames'] == ['Male', 'Female', 'Non-binary/fluid', 'Other'])
def test_title(): class TestSchema(Schema): myfield = fields.String(metadata={'title': 'Brown Cowzz'}) yourfield = fields.Integer(required=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['definitions']['TestSchema']['properties']['myfield'][ 'title'] == 'Brown Cowzz'
def test_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={'description': 'Brown Cow'}) yourfield = fields.Integer(required=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['TestSchema']['properties'] assert props['myfield']['description'] == 'Brown Cow'
def test_additional_properties_invalid_value(): class TestSchema(Schema): class Meta: additional_properties = "foo" foo = fields.Integer() schema = TestSchema() json_schema = JSONSchema() with pytest.raises(UnsupportedValueError): json_schema.dump(schema)
def test_nested_recursive(): """A self-referential schema should not cause an infinite recurse.""" class RecursiveSchema(Schema): foo = fields.Integer(required=True) children = fields.Nested('RecursiveSchema', many=True) schema = RecursiveSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['RecursiveSchema']['properties'] assert 'RecursiveSchema' in props['children']['items']['$ref']
def test_length_validator_error(): class BadSchema(Schema): bob = fields.Integer(validate=validate.Length(min=1, max=3)) class Meta: strict = True schema = BadSchema() json_schema = JSONSchema() with pytest.raises(UnsupportedValueError): json_schema.dump(schema)
def test_unknown_typed_field_throws_valueerror(): class Invalid(fields.Field): def _serialize(self, value, attr, obj): return value class UserSchema(Schema): favourite_colour = Invalid() schema = UserSchema() json_schema = JSONSchema() with pytest.raises(ValueError): json_schema.dump(schema).data
def test_readonly(): class TestSchema(Schema): id = fields.Integer(required=True) readonly_fld = fields.String(dump_only=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped['properties']['readonly_fld'] == { 'title': 'readonly_fld', 'type': 'string', 'readonly': True, }
def test_list(): class ListSchema(Schema): foo = fields.List(fields.String(min), required=True) schema = ListSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_json = dumped['definitions']['ListSchema']['properties']['foo'] assert nested_json['type'] == 'array' assert 'items' in nested_json item_schema = nested_json['items'] assert item_schema['type'] == 'string'
def test_nested_string_to_cls(): class TestSchema(Schema): foo = fields.Integer(required=True) class TestNestedSchema(Schema): foo2 = fields.Integer(required=True) nested = fields.Nested('TestSchema') schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_json = dumped['properties']['nested'] assert nested_json['properties']['foo']['format'] == 'integer' assert nested_json['type'] == 'object'
def test_handle_range_no_minimum(): class SchemaMin(Schema): floor = fields.Integer(validate=validate.Range(min=1, max=4)) class SchemaNoMin(Schema): floor = fields.Integer(validate=validate.Range(max=4)) schema1 = SchemaMin(strict=True) schema2 = SchemaNoMin(strict=True) json_schema = JSONSchema() dumped1 = json_schema.dump(schema1) dumped2 = json_schema.dump(schema2) dumped1.data['properties']['floor']['minimum'] == 1 dumped1.data['properties']['floor']['exclusiveMinimum'] is True dumped2.data['properties']['floor']['minimum'] == 0 dumped2.data['properties']['floor']['exclusiveMinimum'] is False
def test_metadata_direct_from_field(): """Should be able to get metadata without accessing metadata kwarg.""" class TestSchema(Schema): id = fields.Integer(required=True) metadata_field = fields.String(description='Directly on the field!') schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped['definitions']['TestSchema']['properties'][ 'metadata_field'] == { 'title': 'metadata_field', 'type': 'string', 'description': 'Directly on the field!', }
def spec(): if self.cached_spec is None: json_schema = JSONSchema() definitions = dict( (k, list(json_schema.dump(v)['definitions'].values())[0]) for k, v in self.schemas.items()) spec = flask_swagger.swagger( flask, template=dict(definitions=definitions, info=self.info, securityDefinitions=self.securities)) self.cached_spec = json.dumps(spec, cls=JSONEncoder2, allow_nan=False) return self.cached_spec
def test_enum_based_load_dump_value(): class TestEnum(Enum): value_1 = 0 value_2 = 1 value_3 = 2 class TestSchema(Schema): enum_prop = EnumField(TestEnum, by_value=True) # Should be sorting of fields schema = TestSchema() json_schema = JSONSchema() with pytest.raises(NotImplementedError): validate_and_dump(json_schema.dump(schema))
def to_jsonschema(cls, dump: bool = True): from marshmallow_jsonschema import JSONSchema # pylint:disable=import-error value = JSONSchema().dump(cls.SCHEMA()) # pylint:disable=not-callable if dump: return cls._dump(value) return value
def test_handle_range_not_number_returns_same_instance(): class SchemaWithStringRange(Schema): floor = fields.String(validate=validate.Range(min=1, max=4)) class SchemaWithNoRange(Schema): floor = fields.String() class SchemaWithIntRangeValidate(Schema): floor = fields.Integer(validate=validate.Range(min=1, max=4)) class SchemaWithIntRangeNoValidate(Schema): floor = fields.Integer() schema1 = SchemaWithStringRange(strict=True) schema2 = SchemaWithNoRange(strict=True) schema3 = SchemaWithIntRangeValidate(strict=True) schema4 = SchemaWithIntRangeNoValidate(strict=True) json_schema = JSONSchema() json_schema.dump(schema1) == json_schema.dump(schema2) json_schema.dump(schema3) != json_schema.dump(schema4)
def schema_to_json(schema: Schema): """Convert marshmallow.Schema to valid json schema :param schema: the marshmallow.Schema to convert :return: the converted json schema """ return JSONSchema().dump(schema)
def test_handle_range_no_minimum(): class SchemaMin(Schema): floor = fields.Integer(validate=validate.Range(min=1, max=4)) class SchemaNoMin(Schema): floor = fields.Integer(validate=validate.Range(max=4)) schema1 = SchemaMin(strict=True) schema2 = SchemaNoMin(strict=True) json_schema = JSONSchema() dumped1 = json_schema.dump(schema1).data['definitions']['SchemaMin'] dumped2 = json_schema.dump(schema2).data['definitions']['SchemaNoMin'] dumped1['properties']['floor']['minimum'] == 1 'exclusiveMinimum' not in dumped1['properties']['floor'].keys() 'minimum' not in dumped2['properties']['floor'] 'exclusiveMinimum' not in dumped2['properties']['floor']
def job_registration_schema(self, schema_to_write): schema_path = os.path.join(self.file_manager[self], self.file_manager.REGISTRATION_SCHEMA_NAME) JSONSchema().validate(schema_to_write) self.file_manager.write(json.dumps(schema_to_write), schema_path)
def test_function(): """Function fields can be serialised if type is given.""" class FnSchema(Schema): fn_str = fields.Function(lambda: "string", required=True, _jsonschema_type_mapping={'type': 'string'}) fn_int = fields.Function(lambda: 123, required=True, _jsonschema_type_mapping={'type': 'number'}) schema = FnSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data props = dumped['definitions']['FnSchema']['properties'] assert props['fn_int']['type'] == 'number' assert props['fn_str']['type'] == 'string'
def test_nested_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={"description": "Brown Cow"}) yourfield = fields.Integer(required=True) class TestNestedSchema(Schema): nested = fields.Nested(TestSchema, metadata={"description": "Nested 1", "title": "Title1"}) yourfield_nested = fields.Integer(required=True) schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_dmp = dumped["properties"]["nested"] assert nested_dmp["properties"]["myfield"]["description"] == "Brown Cow" assert nested_dmp["description"] == "Nested 1" assert nested_dmp["title"] == "Title1"
def parameters(self, new_schema): schema_path = os.path.join(self.file_manager[self], self.file_manager.JOB_PARAMETER_FILE_NAME) JSONSchema().validate(new_schema) with open(schema_path, mode='w+') as schema_file: schema_file.write(json.dumps(new_schema))
def test_nested_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={'description': 'Brown Cow'}) yourfield = fields.Integer(required=True) class TestNestedSchema(Schema): nested = fields.Nested( TestSchema, metadata={'description': 'Nested 1', 'title': 'Title1'}) yourfield_nested = fields.Integer(required=True) schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_dmp = dumped['properties']['nested'] assert nested_dmp['properties']['myfield']['description'] == 'Brown Cow' assert nested_dmp['description'] == 'Nested 1' assert nested_dmp['title'] == 'Title1'
def test_list_nested(): """Test that a list field will work with an inner nested field.""" class InnerSchema(Schema): foo = fields.Integer(required=True) class ListSchema(Schema): bar = fields.List(fields.Nested(InnerSchema), required=True) schema = ListSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_json = dumped['definitions']['ListSchema']['properties']['bar'] assert nested_json['type'] == 'array' assert 'items' in nested_json item_schema = nested_json['items'] assert 'InnerSchema' in item_schema['$ref']
def job_result_schema(self): schema_path = os.path.join(self.file_manager[self], self.file_manager.RESULT_SCHEMA_NAME) with open(schema_path, mode='r') as schema_file: schema = json.loads(''.join([line for line in schema_file])) return JSONSchema().load(schema).data
def test_dumps_iterable_enums(): mapping = {'a': 0, 'b': 1, 'c': 2} class TestSchema(Schema): foo = fields.Integer( validate=validate.OneOf(mapping.values(), labels=mapping.keys())) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped['definitions']['TestSchema']['properties']['foo'] == { 'enum': [v for v in mapping.values()], 'enumNames': [k for k in mapping.keys()], 'format': 'integer', 'title': 'foo', 'type': 'number' }
def job_result_schema(self, schema_to_write): data, errors = JSONSchema().dumps(schema_to_write) if errors: raise ValueError('The supplied schema is not a JSON Schema') schema_path = os.path.join(self.file_manager[self], self.file_manager.RESULT_SCHEMA_NAME) self.file_manager.write(data, schema_path)
def json_schema(schema): # Fixes an hedge case: when "required "is empty "[]" then it must be removed, or it will throw error otherwise. return { k: v for k, v in next( iter(next(iter(JSONSchema().dump( schema).data.values())).values())).items() if k != 'required' or v }
def test_unknown_typed_field(): class Colour(fields.Field): def _jsonschema_type_mapping(self): return {"type": "string"} def _serialize(self, value, attr, obj): r, g, b = value r = hex(r)[2:] g = hex(g)[2:] b = hex(b)[2:] return "#" + r + g + b class UserSchema(Schema): name = fields.String(required=True) favourite_colour = Colour() schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped["properties"]["favourite_colour"] == {"type": "string"}
def test_nested_only_exclude(self): schema = Group() json_schema = JSONSchema() dumped = json_schema.dump(schema).data for key, field in schema.declared_fields.items(): original_fields = field.schema.declared_fields.keys() nested_dict = dumped['properties'][key]['items']['properties'] actual_properties = list(nested_dict.keys()) if field.only: # only takes precedence over exclude if isinstance(field.only, str): expected_properties = [field.only] else: expected_properties = list(field.only) elif field.exclude: expected_properties = list(original_fields - set(field.exclude)) else: expected_properties = original_fields self.assertEqual(sorted(expected_properties), sorted(actual_properties))
def test_default(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["id"]["default"] == "no-id"
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['sex']['enum'] == ['male', 'female']
def test_default(self): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data self._validate_schema(dumped) self.assertEqual(dumped['properties']['id']['default'], 'no-id')
def test_default(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['id']['default'] == 'no-id'
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["sex"]["enum"] == ["male", "female"]