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

minLength/maxLength being added to all types #97

Closed
YuriHeupa opened this issue Nov 9, 2016 · 2 comments

Comments

@YuriHeupa
Copy link
Contributor

commented Nov 9, 2016

Marshmallow allows me to add validation against nested schema length, doing as the follows:

class FooSchema(Schema):
    bars = fields.Nested('BarSchema', validate=Length(min=1), many=True)

It'll validate if the bars array contains at least 1 object, but using apispec to generate the spec for this schema, it'll output the follow:

{
  "definitions": {
    "FooSchema": {
      "properties": {
        "bars": {
          "items": {
            "$ref": "#/definitions/BarSchema"
          },
          "minLength": 1,
          "type": "array"
        }
      }
    }
  }
}

Based on swagger specification http://json-schema.org/latest/json-schema-validation.html#anchor26 (5.6/5.7) the minLength and maxLength can only be used for string types, in other words these fields can't be used with arrays, so the field2length needs checking if the value is an array to properly add length validators.

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 13, 2016

The sections of the json-schema spec you linked to say

A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.

But I don't see language that suggests that these keywords cannot be used for other types. Am I missing something?

@YuriHeupa

This comment has been minimized.

Copy link
Contributor Author

commented Nov 14, 2016

I think that because it doesn't say it shouldn't be used not necessairly means that it could be used with other types aside from strings. I think if minlength/maxlength could be used with other types, it wouldn't fail with the existing swagger validators, however it's failing (try giving a shot with flex validator).

For array types the attrs minItems and maxItems should be used instead.

I updated my PR #98 with the appropriate changes.

@YuriHeupa YuriHeupa changed the title minLength/maxLength being added to other types besides the string minLength/maxLength being added to other types besides arrays Nov 14, 2016

@YuriHeupa YuriHeupa changed the title minLength/maxLength being added to other types besides arrays minLength/maxLength being added to all types Nov 14, 2016

@sloria sloria closed this in #98 Nov 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.