def test_range_validator(): schema = Address() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['Address']['properties'] assert props['floor']['minimum'] == 1 assert props['floor']['maximum'] == 4
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_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' ])
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 job_registration_schema(self): registration_schema_path = os.path.join( self.file_manager[self], self.file_manager.REGISTRATION_SCHEMA_NAME) with open(registration_schema_path, mode='r') as schema_file: file_data = ''.join([line for line in schema_file]) return JSONSchema().loads(file_data).data
def test_dump_schema(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert len(schema.fields) > 1 props = dumped['definitions']['UserSchema']['properties'] for field_name, field in schema.fields.items(): assert field_name in props
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_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 get_input_configuration(self): """ Get name and type json value for input parameters required by this plugin to operate. """ json_schema = JSONSchema() schema_blue_print = self.input_params.generate_schema(self.name + 'InputParams') schema_desc = schema_blue_print() return json_schema.dump(schema_desc).data
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 result(self): schema_path = os.path.join(self.file_manager[self], self.file_manager.JOB_RESULT_FILE_NAME) if not os.path.isfile(schema_path): return None with open(schema_path) as result_file: file_data = result_file.read() return JSONSchema().loads(file_data).data
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_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_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_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_metadata(): """Metadata should be available in the field definition.""" class TestSchema(Schema): myfield = fields.String(metadata={'foo': 'Bar'}) yourfield = fields.Integer(required=True, baz="waz") schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['TestSchema']['properties'] assert props['myfield']['foo'] == 'Bar' assert props['yourfield']['baz'] == 'waz' assert 'metadata' not in props['myfield'] assert 'metadata' not in props['yourfield'] # repeat process to assure idempotency json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['TestSchema']['properties'] assert props['myfield']['foo'] == 'Bar' assert props['yourfield']['baz'] == 'waz'
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 get(self): output = {} for plugin_id, plugin in config.APPCONFIG['plugins'].items(): output[plugin_id] = {} for handler in plugin.HANDLERS: schema = handler.REQUEST_SCHEMA() logging.info(schema) logging.info(id(schema)) output[plugin_id][handler.ID] = { "route": (plugin.BASE_ROUTE + '/' + handler.ROUTE).rstrip('/'), "request_schema": JSONSchema().dump(schema) } self.write(output)
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 get_combiner_conds(combiner_types): conds = [] for combiner_type in combiner_types: combiner_cls = combiner_registry[combiner_type] schema_cls = combiner_cls.get_schema_cls() schema = marshmallow_dataclass.class_schema(schema_cls)() schema_json = JSONSchema().dump(schema) combiner_json = schema_json["definitions"][ schema_cls.__name__]["properties"] # TODO: add type to schema: https://github.com/lovasoa/marshmallow_dataclass/issues/62 combiner_cond = create_cond({"type": combiner_type}, combiner_json) conds.append(combiner_cond) return conds
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_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 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 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 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 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'