| 1 | package Data::YADV::Checker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Try::Tiny; | ||
| 7 | use Data::YADV::CheckerASub; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ($class, $structure, %args) = @_; | ||
| 11 | |||
| 12 | bless {structure => $structure, %args}, $class; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub structure { $_[0]->{structure} } | ||
| 16 | sub schema { $_[0]->{schema} } | ||
| 17 | |||
| 18 | sub check { | ||
| 19 | my $self = shift; | ||
| 20 | my ($structure, $factory) = $self->_prepare_arguments(@_) or return; | ||
| 21 | |||
| 22 | $factory->($structure)->verify(); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub check_value { | ||
| 26 | my $self = shift; | ||
| 27 | my $cb = pop; | ||
| 28 | my $child = $self->_prepare_structure(@_) or return; | ||
| 29 | |||
| 30 | $self->schema->build_checker('Data::YADV::CheckerASub', $cb => $child) | ||
| 31 | ->verify(); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub check_defined { | ||
| 35 | my $self = shift; | ||
| 36 | my $structure = $self->_prepare_structure(@_) or return; | ||
| 37 | |||
| 38 | if (not defined $structure->get_structure) { | ||
| 39 | $self->error('element not defined', @_); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub check_each { | ||
| 44 | my $self = shift; | ||
| 45 | my ($node, $factory) = $self->_prepare_arguments(@_) or return; | ||
| 46 | |||
| 47 | return $self->error($node->get_type . ' is not iterable', | ||
| 48 | @{$node->get_path}) | ||
| 49 | unless $node->can('each'); | ||
| 50 | |||
| 51 | $node->each( | ||
| 52 | sub { | ||
| 53 | my ($node, $key) = @_; | ||
| 54 | $factory->($node)->verify($key); | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _get_child { | ||
| 60 | my ($self, $structure, @path) = @_; | ||
| 61 | |||
| 62 | my $child = try { | ||
| 63 | $structure->get_child(@path); | ||
| 64 | } | ||
| 65 | catch { | ||
| 66 | $self->_error_against_structure($structure, $_, @path); | ||
| 67 | }; | ||
| 68 | |||
| 69 | $self->_error_against_structure($structure, 'element not found', @path) unless defined $child; | ||
| 70 | |||
| 71 | $child; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _prepare_structure { | ||
| 75 | my ($self, @elements) = @_; | ||
| 76 | |||
| 77 | my $structure = $elements[0]; | ||
| 78 | if (ref $structure && $structure->can('get_child')) { | ||
| 79 | shift @elements; | ||
| 80 | } else { | ||
| 81 | $structure = $self->structure; | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->_get_child($structure, @elements); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _prepare_arguments { | ||
| 88 | my ($self, @elements) = @_; | ||
| 89 | my $schema = pop @elements; | ||
| 90 | |||
| 91 | my $structure = $self->_prepare_structure(@elements) or return; | ||
| 92 | my $checker_factory = $self->_checker_factory($schema); | ||
| 93 | |||
| 94 | ($structure, $checker_factory); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _checker_factory { | ||
| 98 | my ($self, $schema) = @_; | ||
| 99 | |||
| 100 | if (ref $schema eq 'CODE') { | ||
| 101 | return sub { | ||
| 102 | $self->schema->build_checker('Data::YADV::CheckerASub', $schema, | ||
| 103 | @_); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | return sub { | ||
| 108 | my $module = $self->schema->schema_to_module($schema); | ||
| 109 | $self->schema->build_checker($module, @_); | ||
| 110 | }; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub error { | ||
| 114 | my $self = shift; | ||
| 115 | |||
| 116 | $self->_error_against_structure($self->structure, @_); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _error_against_structure { | ||
| 120 | my ($self, $structure, $message, @path) = @_; | ||
| 121 | |||
| 122 | my $prefix = $structure->get_path_string(@path); | ||
| 123 | $prefix = '$structure' . ($prefix ? '->' : '') . $prefix; | ||
| 124 | |||
| 125 | $self->{error_cb}->($prefix, $message); | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; |