python,jsonschema, marshmallow, “None is not of type ‘string‘

时间: 2023-07-10 admin IT培训

python,jsonschema, marshmallow, “None is not of type ‘string‘

python,jsonschema, marshmallow, “None is not of type ‘string‘

1、示例代码:

class TestDto(Schema):swag_in = "body"    NAME = fields.String(type=["string", "null"], allow_none=True, description='名称', example='Test')BEGIN_DATE = fields.DateTime(type=["string", "null"], allow_none=True, description='开始日期', required=False,example='2022-10-30 00:00:00')END_DATE = fields.DateTime(type=["string", "null"], allow_none=True, description='结束日期', required=False, example='2023-10-30 00:00:00')

2、type=["string", "null"],jsonschema验证类型时允许为空

     allow_none=True,marshmallow验证时允许为空