From ae23460b9cf0be4856478b2273dd089bbfed81f8 Mon Sep 17 00:00:00 2001 From: Jakob Voss Date: Wed, 31 Jan 2018 13:38:21 +0100 Subject: [PATCH 1/2] Add Catmandu::Fix::validate --- lib/Catmandu/Fix/Condition/valid.pm | 2 +- lib/Catmandu/Fix/validate.pm | 75 +++++++++++++++++++++++++++++ lib/Catmandu/Validator.pm | 2 + t/Catmandu-Fix-validator.t | 33 +++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 lib/Catmandu/Fix/validate.pm create mode 100644 t/Catmandu-Fix-validator.t 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..8e465db45 --- /dev/null +++ b/lib/Catmandu/Fix/validate.pm @@ -0,0 +1,75 @@ +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 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 $error_field = $self->error_field + ? $fixer->split_path($self->error_field) : undef; + + my $perl = $fixer->emit_create_path( + $fixer->var, + $error_field, + sub { + my $var = shift; + "${var} = ${validator_var}->last_errors;"; + } + ); + + "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. The error field can be +configured with option C. 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-validator.t b/t/Catmandu-Fix-validator.t new file mode 100644 index 000000000..75491f34e --- /dev/null +++ b/t/Catmandu-Fix-validator.t @@ -0,0 +1,33 @@ +#!/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"; + +done_testing; From ab0a06865e6892f14aa306b6f57a8d53e3e12c93 Mon Sep 17 00:00:00 2001 From: Jakob Voss Date: Wed, 31 Jan 2018 15:10:52 +0100 Subject: [PATCH 2/2] Add option verbose to validate Fix --- lib/Catmandu/Fix/validate.pm | 51 +++++++++++++++---- ...ix-validator.t => Catmandu-Fix-validate.t} | 3 ++ 2 files changed, 43 insertions(+), 11 deletions(-) rename t/{Catmandu-Fix-validator.t => Catmandu-Fix-validate.t} (88%) diff --git a/lib/Catmandu/Fix/validate.pm b/lib/Catmandu/Fix/validate.pm index 8e465db45..9eb81a858 100644 --- a/lib/Catmandu/Fix/validate.pm +++ b/lib/Catmandu/Fix/validate.pm @@ -11,6 +11,7 @@ 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); @@ -20,17 +21,31 @@ 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_create_path( - $fixer->var, - $error_field, - sub { - my $var = shift; - "${var} = ${validator_var}->last_errors;"; - } - ); + 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 }"; } @@ -64,9 +79,23 @@ Catmandu::Fix::validate - validate data and keep errors =head1 DESCRIPTION This L validates data with a L and stores -errors in field C for further inspection. The error field can be -configured with option C. Additional options are passed to the -validator. +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 diff --git a/t/Catmandu-Fix-validator.t b/t/Catmandu-Fix-validate.t similarity index 88% rename from t/Catmandu-Fix-validator.t rename to t/Catmandu-Fix-validate.t index 75491f34e..02f547a29 100644 --- a/t/Catmandu-Fix-validator.t +++ b/t/Catmandu-Fix-validate.t @@ -30,4 +30,7 @@ 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;