如果 json 参数没有值(空字符串),如何引发异常?

How to raise an exception if json parameter is not having value (empty string)?

如果我的输入 json 如下所示,我想引发异常:

{
"username":"user",
"password": ""
}

如果我将密码作为 null 传递,那么我想引发异常。我可以知道它是如何被棉花糖处理的吗? 我不想像下面这样明确检查:

request_jsn = request.get_json()
if 'password' in request_jsn.keys() :
   if request_jsn['password'] :
     password = request_jsn['password']
from marshmallow import Schema, fields
class UserSchema(Schema):
username = fields.String(required=True)
password = fields.String(required=True)
--------------
def post(self):

    if not request.json:
        return jsonify( {'msg':"Unsupported media type,Requests must be JSON",'code': 415})
    try:
       request_jsn =  Schema().load(request.get_json())
    except ValidationError as e:
        return jsonify(error_dict(current_request_id(), str(e), 400))

我找到了使用它的方法。

username = fields.String(required=True, validate=validate.Length(min=1, error="Field should not be empty.")) 
password = fields.String(required=True, validate=validate.Length(min=1, error="Field should not be empty.")