The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Kelp::Module::ValidateTiny - Validate parameters in a Kelp Route Handler

VERSION

Version 0.01

SYNOPSIS

use Kelp::Module::ValidateTiny;
# inside your Kelp config file 
{
    modules => [ qw{SomeModule Validate::Tiny} ],
    modules_init => {
        ...
        ,
        # :all will import everything
        # no need to list MyApp here
        'Validate::Tiny' => {
            subs => [ qw{is_required is_required_id} ],
            into => [ qw{MyApp::OtherRouteClass} ], 
        }
    }
}
...
#inside a Kelp route

my $vt_rules = {
    fields => [...],
    filters => [...],
    checks => [...],
};

my $result = $self->validate($vt_rules)
# $result is a Validate::Tiny object

# process $result
...

# render the template form.tt if validation fails
# $errors and valid values are automatically passed, 
# to the template, but you can optionally pass some 
# more data to that template

my $result = $self->validate($rules, 
    on_error => 'form.tt',
    data => {
        message => 'You could try something else'
    },
);
# If validation fails, $result is an instance of 
# Validate::Tiny::PlackResponse and has a response 
# method that can be sent
return $result->response unless $result->success

# All data is valid here.
# use $result->data
  

DESCRIPTION

Kelp::Module::ValidateTiny adds Validate::Tiny's validator to your Kelp application.

METHODS

validate

This is the only method decorating $self. You can call it in three ways:

First you can pass it just a valid Validate::Tiny $rules hash reference. It will return a Validate::Tiny object and you can call all the usual V::T methods on it.

my $result = $self->validate($rules);
# $result is now a Validate::Tiny object

Second you can pass it a name ('on_error') and value (a template filename) pair. If your data passed the validation, the return value is the usual V::T object. However, if validation fails, the validate method returns an object that has a "response" method in addition to all the Validate::Tiny methods.

my $result = $self->validate(
    $rules,
    on_error => 'form'
);
return $result->response unless $result->success # form.tt rendered
...
# Your data was valid here
...
# Return some other response    

Note that calling $result->response if your validations succeeded is a fatal error. The template (form.tt in the code above) is rendered with a hashref that contains the key-value pairs of valid parameters plus a key "error" that points to another hashref with names of invalid parameters as keys and the corresponding error messages as values. So if your parameters were

{id => 41, lang => 'Perl', version => '5.10'}

and id was found to be invalid with your rules/checks, then the template 'form.tt' renderer is passed the following hashref:

{
	lang => 'Perl',
	version => '5.10',
	error {
		id => 'The answer is 42, not 41',
	}
}

This can be useful with a construct like [% error.name || name %] in your template.

Third, you can pass some additional values that will be passed "as is"" to the on_fail template

$self->validate($rules, 
    on_error => 'form.tt',
    data => {
        message => 'You could try something else next time!'
    },
);

Here the caller passes an additional key data so that your on_error template renderer gets the following hash ref

{
    lang => 'Perl',
    version => '5.10',
    error {
        id => 'The answer is 42, not 41',
    },
    message => 'You could try something else next time!'
}

AUTHOR

Gurunandan R. Bhat <gbhat@pobox.com>

COPYRIGHT

Copyright 2013- Gurunandan R. Bhat

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Kelp, Kelp::Module, Validate::Tiny