Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/validate #333

Merged
merged 2 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Catmandu/Fix/Condition/valid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ Catmandu::Fix::Condition::valid - Execute fixes if the data passes validation

=head1 SEE ALSO

L<Catmandu::Fix>
See L<Catmandu::Fix::validate> to check and get validation errors.

=cut
104 changes: 104 additions & 0 deletions lib/Catmandu/Fix/validate.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package Catmandu::Fix::validate;

use Catmandu::Sane;

our $VERSION = '1.07';

use Moo;
use Catmandu::Util qw(require_package);
use namespace::clean;
use Catmandu::Fix::Has;

has path => (fix_arg => 1);
has name => (fix_arg => 1);
has verbose => (fix_opt => 1);
has error_field => (fix_opt => 1, default => 'errors');
has opts => (fix_opt => 'collect');
has validator => (is => 'lazy', init_arg => undef);

with 'Catmandu::Fix::SimpleGetValue';

sub emit_value {
my ($self, $var, $fixer) = @_;
my $validator_var = $fixer->capture($self->validator);
my $verbose = $fixer->capture($self->verbose);
my $errors = $fixer->generate_var;
my $error_field = $self->error_field
? $fixer->split_path($self->error_field) : undef;

my $perl = $fixer->emit_declare_vars($errors)
. "${errors} = ${validator_var}->last_errors;"
. $fixer->emit_create_path(
$fixer->var,
$error_field,
sub {
my $var = shift;
"${var} = ${errors}";
}
)
. "if(${verbose}) {"
. $fixer->emit_foreach(
$errors,
sub {
my $v = shift;
"say STDERR is_ref(${v})"
."? Catmandu->export_to_string(${v},'JSON',array=>0) : ${v}"
}
)
. "}";

"unless (${validator_var}->is_valid(${var})) { $perl }";
}

sub _build_validator {
my ($self) = @_;
require_package($self->name, 'Catmandu::Validator')->new($self->opts);
}

1;

__END__

=pod

=head1 NAME

Catmandu::Fix::validate - validate data and keep errors

=head1 SYNOPSIS

# Check author field against a JSON Schema
validate('author', JSONSchema, schema: 'my/schema.json')
if exists(errors)
... # do something
end

# Check item against a custom validator, store in errors in 'warnings'
validate('author', MyValidatorClass, error_field: warnings)

=head1 DESCRIPTION

This L<Catmandu::Fix> validates data with a L<Catmandu::Validator> and stores
errors in field C<errors> for further inspection.

=head1 CONFIGURATION

=over

=item error_field

Path where to store errors. Set to C<errors> by default.

=item verbose

Print errors to STDERR. Non-scalar errors are serialized to JSON.

=back

Additional options are passed to the validator.

=head1 SEE ALSO

L<Catmandu::Fix::Condition::valid>

=cut
2 changes: 2 additions & 0 deletions lib/Catmandu/Validator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ Catmandu::Validator - Namespace for packages that can validate records in Catman
publish_record($record);
});

See L<Catmandu::Fix::validate> and L<Catmandu::Fix::Condition::valid> to use validators in fixes (L<Catmandu::Fix>).

=head1 DESCRIPTION

A Catmandu::Validator is a base class for Perl packages that can validate data.
Expand Down
36 changes: 36 additions & 0 deletions t/Catmandu-Fix-validate.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
#use Catmandu::Fix::set_field;

my $pkg;

BEGIN {
$pkg = 'Catmandu::Fix::validate';
use_ok $pkg;
}

my $validator;
sub record { { name => { foo => 'bar' }, @_ } };

$validator = $pkg->new( '', 'Simple', handler => sub {});
is_deeply $validator->fix(record), record, "no errors";

$validator = $pkg->new( '', 'Simple', handler => sub { 'fail' });
is_deeply $validator->fix(record), record( errors => ['fail'] ), "errors";

$validator = $pkg->new( 'name', 'Simple',
handler => sub { $_[0] },
error_field => 'warnings',
);
is_deeply $validator->fix(record),
record( warnings => [{ foo => 'bar'}] ),
"got errors with error_field";

$validator = $pkg->new( '', 'Simple', handler => sub { [{},1,{}] });
$validator->fix(record);

done_testing;