Code Monkey home page Code Monkey logo

flask-inputs's Introduction

Flask-Inputs

⚠️ This package is no longer maintained. The Flask-Inputs package on PyPi (version 0.3.0) supports up to Python 3.8. Any further breaking changes will no longer be supported or fixed. Feel free to fork this project and continue improving it.

Project Status License Build Status Coverage Status

Introduction

WTForms is awesome for validating POST data. What about other request data?

The Flask-Inputs extension adds support for WTForms to validate request data from args to headers to json.

Installation

To install Flask-Inputs, simply:

$ pip install flask-inputs

JSON validation requires jsonschema:

$ pip install jsonschema

Documentation

Documentation is available at http://pythonhosted.org/Flask-Inputs

Contribute

Feel free to fork this repository and republish it. I will no longer be maintaining this project.

License

MIT

flask-inputs's People

Contributors

koenbrouwer avatar nathancahill avatar rez4mt avatar s8sg avatar waltsu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

flask-inputs's Issues

Does the library support python 3?

When I try to use it, I get the following error:

File "/home/luke/Documents/code/git/notebook/venv/lib/python3.6/site-packages/flask_inputs/inputs.py", line 34, in __init__
for field, validators in input.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

AFAIK dict.iteritems isn't available in Python 3

Numeric values

Numeric values fail to validate with, for example, NumberRange built-in validator.
Maybe the structure should be kind of like:

rules = {
    'age': {type:'integer',
            validators:[DataRequired(u'age is required'), NumberRange(1,120)]}
}

and then the internal input representation is a Wtforms IntegerField. This way input is properly coerced.

Another option would be to just directly set type equal to any of the field types that Wtforms provides, but I don't know if the "Field" suffix of all the field types would introduce improper naming, since Flask-inputs validates more than form fields.
Example:

from wtforms.fields import IntegerField

rules = {
    'age': {type:IntegerField,
            validators:[DataRequired(u'age is required'), NumberRange(1,120)]}
}

Field withe errors not returned

When there is an error in the during the validation process only the error messages are returned the fields that generated the error is not return along with the validation message

'dict' object has no attribute 'iteritems'

Hello,
Thanks for "flask-inputs" library. It is working on my python2 but on python3, it gives an error 'dict'`` object has no attribute 'iteritems'''

I also search that iteritems is no longer supported in python3, is there other way to use flask-inputs on python3? Or if none, what other ways I can use to validate the json input for my [POST] method request.

Thank you!

Support python3

Python 3 use dict.items() instead of dict.iteritems()
Would you plan to support python3?
Thanks

jsonschema with type integer is read as type number?

schema = { 'type': 'object', 'properties': { 'size': {'type': 'integer', 'minLength': 1, 'multipleOf': 10 } } }

here is my schema, but in my input, it can accept . and if I input a string the error says that "str" is not a number

Did anyone experience the same case? May I know how did your solve this issue?
Thank you!

Regex for form keys.

I have a form which contains a variable number of fields notated as p1_value, p2_value, p3_value, etc. (e.g. sometimes there may only be p1 and p2, yet other times they may be a p4). How can I represent this when validating the form? Thank you.

form = {
        '_value': [
            Regexp(SOME_PATTERN, message=u'MESSAGE')
        ]
    }

Discuss new feature: return status code after validation

Hey, guys,

Thanks for your effort! I found this package exactly is what I wanted. Here, I want to discuss several improvable functions or missing features.

How do you think about let Flask-Inputs also return the HTTP status code for error handling.

For now, Flask-Inputs only raise error message when validation failed. eg:

class CustomInputs(Inputs):
    headers = {
        'Content-Type': [
            AnyOf(
                ('application/json', ),
                message="Content-Type of Headers must be 'application/json'",
            ),
        ]
    }

@app.route('/api', methods=('GET', 'POST')
def api_inference():
    inputs = CustomInputs(request)

    if not inputs.validate():
        return jsonify(success=False, errors=inputs.errors)

Flask-Inputs works well and return a error message to client, if the request has a wrong Content-Type. But the client still would receive status code 200 which indicates that the client's request was successfully received, understood, and accepted (according to RFC 2616). Actually, this request was just received but accepted. The correct status code should be 415 indicates that the origin server is refusing to service the request because the payload is in a format not supported.

Hence, what I expected is:

class CustomInputs(Inputs):
    headers = {
        'Content-Type': [
            AnyOf(
                ('application/json', ),
                message="Content-Type of Headers must be 'application/json'",
            ),
        ]
    }

@app.route('/api', methods=('GET', 'POST')
def api_inference():
    inputs = CustomInputs(request)

    if not inputs.validate():
        return jsonify(success=False, errors=inputs.errors), inputs.status_code

The major different is in second return variable of last line inputs.status_code. This will make both client/server much easier to debug, check and log information.

I think this is really a valuable and necessary function.

Of course, there are also problems would be troubles when implementing this function:

(1) Priority of status code. If there are multiple wrong validations, which one should be returned. Two choices: a. Protocol sequence. Check headers first, then validate json or form or else things defined in HTTP protocol. Return first error status codes. b. Depending on implementation of codes, like follow valid_attrs = ['args', 'form', 'values', 'cookies', 'headers', 'json', 'rule'] attributes.

(2) The way to define or custom status code for specific validation. Where to define error status code. Here, I provide one solution. eg:

class CustomInputs(Inputs):
    headers = {
        'Content-Type': [
            AnyOf(
                ('application/json', ),
                message="Content-Type of Headers must be 'application/json'",
                errorcode=415,
            ),
        ]
    }

Now, validaors of Flask-Inputs are depending on wtforms packages. If necessary, could we rewrite or reimplement ourself validators based on wtforms.validators. (Yeah, sometimes, I ported several validators directly from source codes, becuase I didn't use package wtforms in my codes.)

This probably isn't the best way. We can discuss to get a better solution to define status code for each validation.

Thanks for reading. Looking forwarding to knowing your opinions. Or whether developer accept pull request in this new feature.

Regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.