diff --git a/lib/Catmandu/Fix/Condition/valid.pm b/lib/Catmandu/Fix/Condition/valid.pm index 5ec3dd4cb..13d2c439b 100644 --- a/lib/Catmandu/Fix/Condition/valid.pm +++ b/lib/Catmandu/Fix/Condition/valid.pm @@ -49,6 +49,6 @@ Catmandu::Fix::Condition::valid - Execute fixes if the data passes validation =head1 SEE ALSO -L +See L to check and get validation errors. =cut diff --git a/lib/Catmandu/Fix/validate.pm b/lib/Catmandu/Fix/validate.pm new file mode 100644 index 000000000..9eb81a858 --- /dev/null +++ b/lib/Catmandu/Fix/validate.pm @@ -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 validates data with a L and stores +errors in field C for further inspection. + +=head1 CONFIGURATION + +=over + +=item error_field + +Path where to store errors. Set to C 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 + +=cut diff --git a/lib/Catmandu/Validator.pm b/lib/Catmandu/Validator.pm index a0e0392b5..630711995 100644 --- a/lib/Catmandu/Validator.pm +++ b/lib/Catmandu/Validator.pm @@ -162,6 +162,8 @@ Catmandu::Validator - Namespace for packages that can validate records in Catman publish_record($record); }); +See L and L to use validators in fixes (L). + =head1 DESCRIPTION A Catmandu::Validator is a base class for Perl packages that can validate data. diff --git a/t/Catmandu-Fix-validate.t b/t/Catmandu-Fix-validate.t new file mode 100644 index 000000000..02f547a29 --- /dev/null +++ b/t/Catmandu-Fix-validate.t @@ -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;