On Sep 27, 2011, at 1:57 PM, Kirk Strauser wrote:
> Does SA natively support (or is there a module on PyPI that supports)
> client-side validation of SQLAlchemy objects? For example, I have this
> declarative class:
>
> class ImportedPayment(Base):
> __tablename__ = 'importedpayment'
> __table_args = {'schema': 'public'}
> paymentid = Column(Integer, primary_key=True)
> externalid = Column(String(16), nullable=False)
> line = Column(Integer, nullable=False)
> invoicestatus = Column(String(32), nullable=False)
> quantity = Column(Numeric(scale=2), nullable=False)
> rate = Column(Numeric(scale=2), nullable=False)
>
>
> Is there an easy way to do this?
OK so the aspect you're looking for here is to define those validations just
once, this is easy enough through a recipe like this:
def notnull(key):
def validate(obj):
if getattr(obj, key) is None:
return "Object %s key %s is None" % (obj, key)
else:
return False
return validate
_validators = {}
def get_validators(someobject):
mapper = object_mapper(someobject)
if mapper in _validators:
return _validators[mapper]
_validators[mapper] = v = []
for prop in mapper.iterate_properties():
if hasattr(prop, "columns"):
col = prop.columns[0]
if not col.nullable:
v.append(notnull(prop.key))
# ... ad nauesum, i.e.
# if <something else about the column>:
# v.append(<some other kind of validation function>)
return v
def validate(someobject):
for validator in get_validators(someobject):
msg = validator()
if msg:
log(msg)
# etc., i.e.
# alert_the_authorities()
> If not, why?
So the theme for today is "why does SQLA have recipes", basically when we can
provide the core fragment of a feature but not a fully polished, documented,
tested, packaged result, something that can just as easily be delivered as a
small batch of customizable source code gets the job done pretty well, and
would be better suited as a separate library if fully fleshed out.
The above recipe lacks a lot of features one might want, such as customizable
ways of defining the validation failure, behavior on the receipt of a failed
validation, etc. A full blown "validation" library might use the idea above
but expand upon it in a much bigger way. I've had other ad-hoc validation
use cases that wouldn't work with the above structure, instead needing a
slightly different structure, so having a small thing just as code for now is
more flexible than a built in "feature" that only handles a small subset of use
cases.
> And if the answer to that is "because you haven't written it yet", would
> anyone be interested in using it if I were to create such a thing?
You might want to check around if similar things don't exist already, I did
find http://pypi.python.org/pypi/SAValidation/ and
http://pypi.python.org/pypi/sqlalchemy_elixir_validations/ for example, there
might be features there that are of use. But by all means, produce a better
validation library for SQLAlchemy, the more the merrier and I'd love to see
more.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.