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

ListField: Add validation support for the elements values in the list #110

Open
avrahamshukron opened this issue Feb 6, 2018 · 1 comment
Assignees

Comments

@avrahamshukron
Copy link
Contributor

Right now the ListField class supports type validation on its elements, and validation agains the list itself (length etc.)
But there is no easy way to specify validators that should be applied to every item in the list.

For example, I might want a ListField of ints, where all the items must be in the range [0, 10].

One might create the following validator:

class ElementsValidator(object):
    def __init__(self, *item_validators):
        self.item_validators = item_validators

    def validate(self, value):
        for item in value:
            for v in self.item_validators:
                v.validate(item)


class Foo(models.Base):
     my_list = fields.ListField(int, validators=(
             ElementsValidator(validators.Min(0), validators.Max(10))
         ))

While this example will work, It has some problems:

  1. It will be very hard to modify the schema from this validator, as it needs to touch some schema elements created by the ListField itself, which might cause conflicts.
  2. It will be very hard to support different validation for each supported type in the list.

Since the type of each element of the list, and its validators actually creates one schema, I think that this feature should be implemented by the library itself, and not by custom validator.

I have no idea how to implement this cleanly without breaking the current API of ListField though...

@avrahamshukron avrahamshukron changed the title ListField: Add validation support for the actual values in the list ListField: Add validation support for the elements values in the list Feb 6, 2018
@DanielSchiavini
Copy link
Contributor

This is included in our fork: https://github.com/beregond/jsonmodels/pull/120

I don't see the problem with your 1st point, since the schema is applied to field_schema["items"].

The 2nd point is not a responsibility of the list, if you are using sub-models each model can implement their own validation. Native types (str, int, etc) will probably not be able to be validated by the same validator. In this case you'd need to create a custom validator.

@beregond beregond self-assigned this Mar 22, 2021
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

No branches or pull requests

3 participants