Git Product home page Git Product logo

peewee-validates's People

Contributors

cscps avatar nad2000 avatar sabinem avatar timster avatar

Stargazers

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

Watchers

 avatar  avatar

peewee-validates's Issues

Hello I wanted to get in touch, before I do something ..

I am looking for an opportunity to start contributing to an open source project and I chose you project, because I like it, I understand it and there might be something to help out on. As a first task, I decided to remove the print statement: there is one in your code, that I think is not supposed to be there. I forked the repo and deleted the statement in a new branch, I will send you a pull request, if you do not mind and I would also don't mind to put the docu into Spinx for you as a next try to contribute something.
This is the print statement in your code:
def validate_required():
def required_validator(field, data):
print('checking', field.name, 'with', data, 'and', field.value)
if not field.value:
raise ValidationError('required')
return required_validator

I am sure you can easily fix this yourself, but would you mind, if I send you my pull request? I feel like a newbie to open source and hope I am doing things right to get started, otherwise, please tell me.

Question validate_email( on model Field

I am not very experienced with peewee_validates, I was reading through the documentation and couldn't fully understand validation inside a model class, it seems you can only apply a validator to the whole class, is it possible to inside a model call to apply a validator to just one field? particularly a validate_email?

`class User(Model):
name = CharField(max_length=60)
address = CharField()
email = CharField(validator=validate_email())
billing = CharField()

class Meta:
    constraints = [SQL('UNIQUE("name" COLLATE NOCASE, "email" COLLATE NOCASE)')]
    database = db`

something like that to maybe combine with constraints?
I am sorry if it is a stupid question, I am still studying how to use databases.

Problems with ManyModelChoiceField validation.

Hello!
I have model like this:

class TestModel(peewee.Model):
    foo = peewee.ForeignKeyField(ModelOne)
    bar = peewee.ForeignKeyField(ModelTwo)
    class Meta:
        indexes = (
            (("foo", "bar"), True),
        )

And when i use <select multiple> i have a mysql error like this:
ERROR 1241 (21000): Operand should contain 1 column(s)

So, i'd made some research and find the problem.

    def perform_index_validation(self, data):
        """
        Validate any unique indexes specified on the model.
        This should happen after all the normal fields have been validated.
        This can add error messages to multiple fields.

        :return: None
        """
        # Build a list of dict containing query values for each unique index.
        index_data = []
        for columns, unique in self.instance._meta.indexes:
            if not unique:
                continue
            index_data.append({col: data.get(col, None) for col in columns})

        # Then query for each unique index to see if the value is unique.
        for index in index_data:
            _query = self.instance.filter(**index)_
            # If we have a primary key, need to exclude the current record from the check.
            if self.pk_field and self.pk_value:
                query = query.where(~(self.pk_field == self.pk_value))
            if query.count():
                err = ValidationError('index', fields=str.join(', ', index.keys()))
                for col in index.keys():
                    self.add_error(col, err)

query = self.instance.filter(**index) generate invalid sql query:

Count(*) 
FROM testmodel AS t1 
INNER JOIN modelone AS t2 ON (t1.foo_id = t2.id) 
INNER JOIN modeltwo AS t3 ON (t1.bar_id = t3.id) 
WHERE ((t1.foo_id = 7) AND (t1.bar_id = (2, 4)));

(t1.bar_id = (2, 4))) - this is a problem sql

I know, this is a peewee orm error, but you can solve this problem on your side like this:

    def perform_index_validation(self, data):
        """
        Validate any unique indexes specified on the model.
        This should happen after all the normal fields have been validated.
        This can add error messages to multiple fields.

        :return: None
        """
        # Build a list of dict containing query values for each unique index.
        index_data = []
        for columns, unique in self.instance._meta.indexes:
            if not unique:
                continue
            index_data.append({col: data.get(col, None) for col in columns})

        # Then query for each unique index to see if the value is unique.
        for index in index_data:
            new_index = {}
            for k, v in index.items():
                if issubclass(type(v), Iterable):
                    k = '%s__in' % k
                new_index[k] = v
            query = self.instance.filter(**new_index)
            # If we have a primary key, need to exclude the current record from the check.
            if self.pk_field and self.pk_value:
                if issubclass(type(self.pk_value), Iterable):
                    query = query.where(~(self.pk_field >> self.pk_value))
                else:
                    query = query.where(~(self.pk_field == self.pk_value))
            if query.count():
                err = ValidationError('index', fields=str.join(', ', index.keys()))
                for col in index.keys():
                    self.add_error(col, err)

p.s.
Your product wery handy. Thank you for it!

coerce of IntegerField fails with value 0

def coerce(self, value):
     try:
         return int(value) if value else None
     except (TypeError, ValueError):
         raise ValidationError('coerce_int')

This pice of code return None if value equals 0.

validate_one_of fails when options is a list of integers.

    def one_of_validator(field, data):
        if field.value is None:
            return
        options = values
        if callable(options):
            options = options()
        if field.value not in options:
            raise ValidationError('one_of', choices=str.join(', ', options))
    return one_of_validator

problem here: raise ValidationError('one_of', choices=str.join(', ', options))

str.join(', ', options) fails with sequence item 0: expected str instance, int found

possible solution is: ', '.join(map(str, options))
it's perfectly works with list of strings and list of numbers.

pypi still has 1.0.7

The latest version of this package is 1.0.8 while the one on pypi is still 1.0.7.

Could you please push the latest version to pypi?

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.