Skip to content

Instantly share code, notes, and snippets.

@corbinbs
Created October 2, 2012 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corbinbs/3821919 to your computer and use it in GitHub Desktop.
Save corbinbs/3821919 to your computer and use it in GitHub Desktop.
Flask JSON Required Example
@app.route("/do/something", methods=['POST'])
@json_required(
required_fields={
'first_name':"Please enter your first name.",
'last_name':"Please enter your last name.",
'email':'Please specify a valid email address',
'date_of_birth':'Please enter your date of birth'
},
validations=[
('email', 'Please specify a valid email address', lambda email: email is not None and EMAIL_REGEX.match(email)),
('email', "This email is already in use. Please try a different email address.", verify_account_available),
('date_of_birth', 'Please enter a valid date of birth', valid_date_of_birth)
]
)
def do_something_useful():
#Confidently use the data in request.json...
return jsonify(status='OK')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment