Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support validation against specs in docstrings #123

Merged
merged 3 commits into from
Jun 26, 2017

Conversation

roo-oliv
Copy link
Contributor

@roo-oliv roo-oliv commented Jun 26, 2017

Despite Flasgger supports defining the specs in the docstring, when one wants automatic POST requests data validation it is not supported doing so if the swagger specifications are defined in the docstring.

This PR adds support for that through a new validate annotation:

swagger = Swagger(app)

@app.route('/pets', methods=['POST'])
@swagger.validate('Pet')
def post():
    """
    Pet POST endpoint
    ---
    tags:
      - pet
    parameters:
      - name: body
        in: body
        required: true
        schema:
          id: Pet
          required:
            - name
          properties:
            name:
              type: string
              description: The pet's name.
              default: "Rex"
            tags:
              type: array
              description: optional list of tags
              default: ["bulldog"]
              items:
                type: string

    responses:
      200:
        description: A single pet item
        schema:
          $ref: '#/definitions/Pet'
    """
    return db.insert(request.data)

@coveralls
Copy link

coveralls commented Jun 26, 2017

Coverage Status

Coverage decreased (-0.7%) to 81.848% when pulling b85cde3 on allrod5:validation-from-docstring into 316cc6e on rochacbruno:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+10.5%) to 93.031% when pulling a366bae on allrod5:validation-from-docstring into 316cc6e on rochacbruno:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+10.5%) to 93.031% when pulling a366bae on allrod5:validation-from-docstring into 316cc6e on rochacbruno:master.

@rochacbruno rochacbruno merged commit 10a48ae into flasgger:master Jun 26, 2017
@roo-oliv roo-oliv deleted the validation-from-docstring branch June 26, 2017 17:08
@creising
Copy link

I was recently trying to use this feature and I ran into an issue. It looks like if I reference a definition in the schema instead of defining the object there I get the following error Filepath or specs is needed to validate. I use this same object in a couple of different methods so I only want to define it in one of them (e.g. the same validation may be used on both a POST or a PUT).

Here is a quick example:
The schema attribute post looks something like

        schema:
          "$ref": "#/definitions/ExampleObject"

Thanks!

@shivamMg
Copy link

@creising I too found out that this doesn't work. There's an open issue around that: #464.

Eventually I just used marshmallow's load() method: Schema().load()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants