What is the preferred way to validate data for a row in DBIx :: Class?

I need to check incoming data in several web application controllers before saving to DB ( DBIx::Class ). Basically, I need to check the value for each column using a callback (anonymous sub). At first I thought about using Params::Validate in every controller. But this approach has two problems:

  • There is no easy way to deal with Params::Validate validation errors, as it just dies from the first invalid parameter with an error string.

  • I have to duplicate the validation logic for each column in all controllers that violates the DRY principle.

I think the best way is to do the logical part of model validation. What is the preferred way to do this in DBIx::Class ?

+4
source share
2 answers

To add validation callbacks to column metadata, use add_columns in the result class, for example

 __PACKAGE__->add_columns( '+mycolumn' => { validate => sub { my ($schema, $val) = @_; # validate $val, possibly using $schema }, }, ... ); 

To facilitate the use of these callbacks, you can create the DBIx :: Class component .

+1
source

I'm not sure what and exactly how you want to check your data, but have you tried using DBIx :: Class :: Validation for your needs? He must match.

+1
source

Source: https://habr.com/ru/post/1411652/


All Articles