timster / peewee-validates Goto Github PK
View Code? Open in Web Editor NEWA simple and flexible model and data validator for Peewee ORM.
Home Page: https://peewee-validates.readthedocs.io
License: MIT License
A simple and flexible model and data validator for Peewee ORM.
Home Page: https://peewee-validates.readthedocs.io
License: MIT License
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.
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.
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!
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.
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.
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.