1#!/usr/bin/env perl
2use strict;
3use warnings;
4use lib ( '.', '../t' );
5use Test::More tests => 8;
6use Data::FormValidator;
7
8# This script tests validating keys with multiple data
9my $input_hash = {
10  single_value          => ' Just One ',
11  multi_values          => [ ' One ', ' Big ', ' Happy ', ' Family ' ],
12  re_multi_test         => [qw/at the circus/],
13  constraint_multi_test => [qw/12345 22234 oops/],
14};
15my $input_profile = {
16  required =>
17    [qw/single_value multi_values re_multi_test constraint_multi_test/],
18  filters       => [qw/trim/],
19  field_filters => {
20    single_value => 'lc',
21    multi_values => 'uc',
22  },
23  field_filter_regexp_map => {
24    '/_multi_test$/' => 'ucfirst',
25  },
26  constraints => {
27    constraint_multi_test => 'zip',
28  },
29};
30
31my $validator = new Data::FormValidator( { default => $input_profile } );
32
33my ( $valids, $missings, $invalids, $unknowns );
34eval {
35  ( $valids, $missings, $invalids, $unknowns ) =
36    $validator->validate( $input_hash, 'default' );
37};
38
39is( $valids->{single_value},
40  'just one', 'inconditional filters still work with single values' );
41
42is( lc $valids->{multi_values}->[0],
43  lc 'one', 'inconditional filters work with multi values' );
44
45is( $valids->{multi_values}->[0],
46  'ONE', 'field filters work with multiple values' );
47
48is( $valids->{re_multi_test}->[0],
49  'At', 'Test the filters applied to multiple values by RE work' );
50
51ok( !$valids->{constraint_multi_test},
52  'If any of the values fail the constraint, the field becomes invalid' );
53
54my $r;
55eval
56{
57  $r = Data::FormValidator->check( { undef_multi => [undef] },
58    { required => 'undef_multi' } );
59};
60diag "error: $@" if $@;
61ok( $r->missing('undef_multi'),
62  'multi-valued field containing only undef should be missing' );
63
64my $v;
65eval { $v = $r->valid('undef_multi'); };
66diag "error: $@" if $@;
67ok( !$v,
68  'multiple valued fields containing only undefined values should not be valid'
69);
70
71###
72
73eval {
74  $r = Data::FormValidator->check( {
75      cc_type => ['Check'],
76    },
77    {
78      required     => 'cc_type',
79      dependencies => {
80        cc_type => {
81          Check => [qw( cc_num )],
82          Visa  => [qw( cc_num cc_exp cc_name )],
83        },
84      },
85    } );
86};
87diag "error: $@" if $@;
88
89ok( $r->missing('cc_num'),
90  'a single valued array should still trigger the dependency check' );
91