From cd59caeec925cf0bf8625d567a442e3c46ae7475 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 11 Oct 2012 14:22:55 -0500 Subject: also provide stringification for the error object --- lib/MooseX/Validation/Doctypes/Errors.pm | 46 ++++++++++++++++++++++++++++++++ t/complex.t | 25 +++++++++-------- t/stringify.t | 33 +++++++++++++++++++++++ 3 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 t/stringify.t diff --git a/lib/MooseX/Validation/Doctypes/Errors.pm b/lib/MooseX/Validation/Doctypes/Errors.pm index ca4d54d..7601855 100644 --- a/lib/MooseX/Validation/Doctypes/Errors.pm +++ b/lib/MooseX/Validation/Doctypes/Errors.pm @@ -2,6 +2,8 @@ package MooseX::Validation::Doctypes::Errors; use Moose; # ABSTRACT: error class for MooseX::Validation::Doctypes +use overload '""' => 'stringify'; + =head1 SYNOPSIS use MooseX::Validation::Doctypes; @@ -113,6 +115,50 @@ sub TO_JSON { }; } +sub stringify { + my $self = shift; + + return join( + "\n", + (sort $self->_stringify_ref($self->errors)), + $self->_stringify_extra_data($self->extra_data), + ); +} + +sub _stringify_ref { + my $self = shift; + my ($data) = @_; + + return + if !defined $data; + + return $data + if !ref $data; + + return map { $self->_stringify_ref($_) } values %$data + if ref($data) eq 'HASH'; + + return map { $self->_stringify_ref($_) } @$data + if ref($data) eq 'ARRAY'; + + return "unknown data: $data"; +} + +sub _stringify_extra_data { + my $self = shift; + my ($data) = @_; + + return + unless defined $data; + + require Data::Dumper; + local $Data::Dumper::Terse = 1; + my $string = Data::Dumper::Dumper($data); + chomp($string); + + return ("extra data found:", $string); +} + __PACKAGE__->meta->make_immutable; no Moose; diff --git a/t/complex.t b/t/complex.t index b617cab..04dbe60 100644 --- a/t/complex.t +++ b/t/complex.t @@ -166,25 +166,24 @@ doctype 'Location' => { } }); is_deeply( - $errors, + $errors->errors, { - errors => { - contact => { - email => "invalid value \"anelson at cpan.org\" for 'contact.email'" - }, - i18n => { - available_currencies => "invalid value [ \"dolla dolla bill\", \"CAD\", \"EUR\" ] for 'i18n.available_currencies'", - default_currency => "invalid value undef for 'i18n.default_currency'" - }, - location => { - coordinates => { - lon => "invalid value \"38q\" for 'location.coordinates.lon'" - } + contact => { + email => "invalid value \"anelson at cpan.org\" for 'contact.email'" + }, + i18n => { + available_currencies => "invalid value [ \"dolla dolla bill\", \"CAD\", \"EUR\" ] for 'i18n.available_currencies'", + default_currency => "invalid value undef for 'i18n.default_currency'" + }, + location => { + coordinates => { + lon => "invalid value \"38q\" for 'location.coordinates.lon'" } } }, "got the right errors" ); + ok(!$errors->has_extra_data, "no extra data"); } } diff --git a/t/stringify.t b/t/stringify.t new file mode 100644 index 0000000..c8db5a4 --- /dev/null +++ b/t/stringify.t @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use Moose::Util::TypeConstraints 'find_type_constraint'; + +use MooseX::Validation::Doctypes; + +doctype 'Person' => { + id => 'Str', + name => 'Str', + title => 'Str', +}; + +{ + my $person = find_type_constraint('Person'); + my $errors = $person->validate({ foo => "bar" }); + + is( + "$errors", + "invalid value undef for 'id'\n" + . "invalid value undef for 'name'\n" + . "invalid value undef for 'title'\n" + . "extra data found:\n" + . "{\n" + . " 'foo' => 'bar'\n" + . "}", + "stringified properly" + ); +} + +done_testing; -- cgit v1.2.3