Giter Club home page Giter Club logo

Comments (6)

frol avatar frol commented on June 5, 2024 1

@DurandA Hi. That is a good question. I am still in search for a better way to implement this. Currently, the validation is done in resource processing and Model validations, for example, when I PATCH users:

  1. I handle PATCH request in resources.py: https://github.com/frol/flask-restplus-server-example/blob/master/app/modules/users/resources.py#L116-L152
  2. I iterate over the operations
  3. In the iteration function:
    1. I check for current user password: https://github.com/frol/flask-restplus-server-example/blob/master/app/modules/users/resources.py#L168-L179
    2. or just blindly try to set the passed fields (expecting an exception from Model validator if the value is not valid): https://github.com/frol/flask-restplus-server-example/blob/master/app/modules/users/resources.py#L181-L208 (notice setattr at the end)

from flask-restplus-server-example.

frol avatar frol commented on June 5, 2024 1

@DurandA Please, read this short article on the PATCH matter: http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/. After you read it, you should understand that the classes inherited from PatchJSONParameters have only 3 (three) constant fields: op, path, and value. Thus, there won't be any name field. You may try to use @validates_schema (it should work in theory, but I haven't tried it myself):

@validates_schema
def validate_name(self, data):
   if data['op'] == 'replace' and data['path'] == '/name' and len(data['value']) < 3:
        raise ValidationError('Too short!', 'name')

NOTE: This is just a workaround since there must be a nicer way to implement this.

from flask-restplus-server-example.

DurandA avatar DurandA commented on June 5, 2024 1

Thank you @frol for your support and detailed answers. 😃 You should get paid for this!
The article from William Durand is indeed excellent reading. I think that the workaround is good enough.

I wish that your patches (especially marshmallow integration) will be merged into Flask-RESTPlus one day.

from flask-restplus-server-example.

frol avatar frol commented on June 5, 2024

Just for your info, I have updated the above code snippet since there was a bug in it.

from flask-restplus-server-example.

frol avatar frol commented on June 5, 2024

@DurandA As to the patches, I wish I could merge them, but writing my patches I had only one thing in mind, that is to support marshmallow. However, merging my patches back would require some work on backwards compatibility with everything else. Some time ago I have found Flask-apispec, which seems to be much closer to what I really needed since it is a part of marshmallow/webargs/apispec family. Unfortunately, it seems to be abandoned though it also seems to be feature complete.

from flask-restplus-server-example.

frol avatar frol commented on June 5, 2024

@DurandA I would like to let you know that @khorolets refactored resources.py quite a bit, so I encourage you to take a look at the current state of this piece of art ;)

In particular, PatchJSONParameters was improved, so you can now implement @classmethods replace/test/etc to validate user input. To get a sense of it, take a look at Users' Parameters: https://github.com/frol/flask-restplus-server-example/blob/master/app/modules/users/parameters.py#L63-L114

from flask-restplus-server-example.

Related Issues (20)

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.