[Dbix-class] best way to cleanse / validate data

Eden Cardim edencardim at gmail.com
Mon Jan 17 11:19:13 GMT 2011


>>>>> "Seth" == Seth Daniel <dbix-class at sethdaniel.org> writes:

    Seth> Hi, I was wondering what is the preferred way to cleanse and
    Seth> validate incoming data when using DBIx::Class?

    Seth> I read with some interest a thread from 2009
    Seth> (http://lists.scsys.co.uk/pipermail/dbix-class/2009-July/008241.html)
    Seth> that discussed validation.  Is DBIx::Class::Validation still considered
    Seth> a good way to go?

    Seth> I initially thought I could use DBIx::Class::MooseColumns.  I converted
    Seth> my Result classes to use MooseColumns and then created a trait that
    Seth> converted the given value to all lower case.  While the trait works
    Seth> fine, and having moose-ish columns is great, it seems that the accessor
    Seth> is not called very often, so my experiment failed (the only time the
    Seth> data to be placed in the database was lower cased was when I called the
    Seth> accessor directly.  create() and similar don't seem to call the
    Seth> accessor (unless it's an inflated column)).

Personally, I've found that a generic validation solution is only
adequate for the most trivial cases, like constraining an int to a
certain range of values or checking for email well-formedness. For
anything more complex than that, like checking for uniqueness or
building a list of the possible values that are allowed for a certain
field, I'd say you're better off writing your own logic, YMMV.

    Seth> So beyond data validation I'd like any opinion on the *proper* way to
    Seth> cleanse data before it gets placed in the database.  Mostly I'm just
    Seth> looking for a way to make certain varchar columns are all lower case.
    Seth> I'm also looking for a good way to validate the incoming data for
    Seth> certain columns.  Again, I was hoping using MooseColumns would allow me
    Seth> to use Moose's type system, but since the accessors don't get called for
    Seth> some common DBIx::Class methods the usefulness is less than I had hoped.

How about wrapping the relevant API calls with Params::Validate?

-- 
Eden Cardim
Software Engineer
Shadowcat Systems Ltd.
http://www.shadowcat.co.uk
http://blog.edencardim.com



More information about the DBIx-Class mailing list