data validation for SQLAlchemy declarative models
Asked Answered
G

4

15

I'm using CherryPy, Mako templates, and SQLAlchemy in a web app. I'm coming from a Ruby on Rails background and I'm trying to set up some data validation for my models. I can't figure out the best way to ensure, say, a 'name' field has a value when some other field has a value. I tried using SAValidation but it allowed me to create new rows where a required column was blank, even when I used validates_presence_of on the column. I've been looking at WTForms but that seems to involve a lot of duplicated code--I already have my model class set up with the columns in the table, why do I need to repeat all those columns again just to say "hey this one needs a value"? I'm coming from the "skinny controller, fat model" mindset and have been looking for Rails-like methods in my model like validates_presence_of or validates_length_of. How should I go about validating the data my model receives, and ensuring Session.add/Session.merge fail when the validations fail?

Garnett answered 20/6, 2011 at 18:58 Comment(0)
G
-4

I ended up using WTForms after all.

Garnett answered 1/7, 2011 at 18:56 Comment(1)
How'd you get around the code duplication? Can you explain a bit how you're using it? I'm a rails dev as well but looking to develop an app with flask+sqlalchemy...I've seen WTForms but it seems a bit foreign to me in that you're always storing validation in the view...while it feels more valid (and more dry) to store it in the model.Hensley
T
17

Take a look at the documentation for adding validation methods. You could just add an "update" method that takes the POST dict, makes sure that required keys are present, and uses the decorated validators to set the values (raising an error if anything is awry).

Transcend answered 20/6, 2011 at 19:7 Comment(2)
Validation methods only work for one field at a time. He obviously means validation that depends on relationship between fields.Ault
Works with multiple fields by (a) list all the fields you want to validate in the decorator @validates('validates_presence_of', 'validates_length_of') (b) ensure your validation triggers on last declared field within the list of fields so the other fields have been set already if key == 'validates_length_of': [code to validate]Astrea
C
3

I wrote SAValidation for the specific purpose of avoiding code duplication when it comes to validating model data. It works well for us, at least for our use cases.

In our tests, we have examples of the model's setup and tests to show the validation works.

Covert answered 14/12, 2011 at 15:28 Comment(0)
M
0

API Logic Server provides business rules for SQLAlchemy models. This includes not only multi-field, multi-table validations, but multi-table validations. It's open source.

Miguel answered 26/4, 2022 at 23:10 Comment(0)
G
-4

I ended up using WTForms after all.

Garnett answered 1/7, 2011 at 18:56 Comment(1)
How'd you get around the code duplication? Can you explain a bit how you're using it? I'm a rails dev as well but looking to develop an app with flask+sqlalchemy...I've seen WTForms but it seems a bit foreign to me in that you're always storing validation in the view...while it feels more valid (and more dry) to store it in the model.Hensley

© 2022 - 2024 — McMap. All rights reserved.