Using flask wtforms validators without using a form

前端 未结 2 2086
抹茶落季
抹茶落季 2021-02-09 04:18

I\'m receiving user registration data from an iOS application and I\'d like to use the validators that come with wtforms to make sure the email and password are acceptable. How

2条回答
  •  孤街浪徒
    2021-02-09 04:46

    Yes, this is entirely possible - the wtforms.Form constructor takes any MultiDict like interface (it just needs to have getlist), so you can just create an instance of werkzeug.datastructures.MultiDict from your JSON:

    data = MultiDict(mapping=request.json)
    form = YourForm(data)
    if form.validate():
        # Data is correct
    

    (assuming the field names match) and things will just work.

提交回复
热议问题