flask-restful-swagger
flask-restful-swagger copied to clipboard
Make resource fields mandatory
I have the following model:
@swagger.model
class AuthModel:
resource_fields = {
'username': fields.Integer,
'password': fields.String
}
In the generated spec, the model will have both the resource fields as optional. How do I make them mandatory?
Very old question, but I discovered the answer to this myself today by looking through the source code and thought I'd share. To remove the 'optional' annotation which is automatically added to resource fields, you need to specify a required property in your class definition, which should contain a list of field names.
For example, to make the "username" field mandatory, you'd do the following:
@swagger.model
class AuthModel:
resource_fields = {
'username': fields.Integer,
'password': fields.String
}
required = ['username']
Or alternatively, to make all fields in your model mandatory:
@swagger.model
class AuthModel:
resource_fields = {
'username': fields.Integer,
'password': fields.String
}
required = list(resource_fields.keys())