summaryrefslogtreecommitdiffstats
path: root/t/complex.t
blob: 04dbe600178131e7e33c24b58f9fb4d06d806501 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

use Test::Requires 'MooseX::Types::URI', 'MooseX::Types::Email',
                   'Locale::Language', 'Locale::Currency',
                   'Number::Phone::US';

use Moose::Util::TypeConstraints;
use MooseX::Types::URI 'Uri';
use MooseX::Types::Email 'EmailAddress';

use MooseX::Validation::Doctypes;

subtype 'CurrencyCode',
    as 'Str',
    where { Locale::Currency::code2currency( $_ ) };
subtype 'LocaleCode',
    as 'Str',
    where { Locale::Language::code2language( $_ ) };
subtype 'PhoneNumber',
    as 'Str',
    where { Number::Phone::US::is_valid_number( $_ ) };

doctype 'Location' => {
    id       => 'Str',
    name     => 'Str',
    location => {
        address => {
            address1    => 'Str',
            city        => 'Str',
            country     => 'Str',
            postal_code => 'Str',
            address2    => 'Maybe[Str]',
            address3    => 'Maybe[Str]',
            address4    => 'Maybe[Str]',
            address5    => 'Maybe[Str]',
            state       => 'Maybe[Str]',
        },
        coordinates => {
            lon => 'Num',
            lat => 'Num',
        }
    },
    contact => {
        phone   => 'PhoneNumber',
        fax     => 'Maybe[PhoneNumber]',
        support => 'Maybe[PhoneNumber | MooseX::Types::URI::Uri | MooseX::Types::Email::EmailAddress]',
        web     => 'Maybe[MooseX::Types::URI::Uri]',
        email   => 'Maybe[MooseX::Types::Email::EmailAddress]',
    },
    i18n => {
        default_currency     => 'CurrencyCode',
        default_locale       => 'LocaleCode',
        available_currencies => 'ArrayRef[CurrencyCode]',
        available_locales    => 'ArrayRef[LocaleCode]',
    }
};

{
    my $location = find_type_constraint('Location');
    is_deeply(
        $location->doctype,
        {
            id       => 'Str',
            name     => 'Str',
            location => {
                address => {
                    address1    => 'Str',
                    city        => 'Str',
                    country     => 'Str',
                    postal_code => 'Str',
                    address2    => 'Maybe[Str]',
                    address3    => 'Maybe[Str]',
                    address4    => 'Maybe[Str]',
                    address5    => 'Maybe[Str]',
                    state       => 'Maybe[Str]',
                },
                coordinates => {
                    lon => 'Num',
                    lat => 'Num',
                }
            },
            contact => {
                phone   => 'PhoneNumber',
                fax     => 'Maybe[PhoneNumber]',
                support => 'Maybe[PhoneNumber | MooseX::Types::URI::Uri | MooseX::Types::Email::EmailAddress]',
                web     => 'Maybe[MooseX::Types::URI::Uri]',
                email   => 'Maybe[MooseX::Types::Email::EmailAddress]',
            },
            i18n => {
                default_currency     => 'CurrencyCode',
                default_locale       => 'LocaleCode',
                available_currencies => 'ArrayRef[CurrencyCode]',
                available_locales    => 'ArrayRef[LocaleCode]',
            }
        },
        "got the right doctype"
    );

    {
        my $errors = $location->validate({
            id       => '14931-FL-53',
            name     => 'My House',
            location => {
                address => {
                    address1    => '123 Any St',
                    city        => 'Anytown',
                    country     => 'USA',
                    postal_code => '00100',
                    address2    => 'Apt Q',
                    address5    => 'knock on the back door',
                    state       => 'IL',
                },
                coordinates => {
                    lon => '38',
                    lat => '57',
                }
            },
            contact => {
                phone   => '867-5309',
                support => 'anelson@cpan.org',
                web     => URI->new('https://metacpan.org/author/ANELSON'),
                email   => 'anelson@cpan.org',
            },
            i18n => {
                default_currency     => 'USD',
                default_locale       => 'en',
                available_currencies => [ 'USD', 'CAD', 'EUR' ],
                available_locales    => [ 'en' ]
            }
        });
        is($errors, undef, "no errors");
    }

    {
        my $errors = $location->validate({
            id       => '14931-FL-53',
            name     => 'My House',
            location => {
                address => {
                    address1    => '123 Any St',
                    city        => 'Anytown',
                    country     => 'USA',
                    postal_code => '00100',
                    address2    => 'Apt Q',
                    address5    => 'knock on the back door',
                    state       => 'IL',
                },
                coordinates => {
                    lon => '38q',
                    lat => '57',
                }
            },
            contact => {
                phone   => '867-5309',
                support => 'anelson@cpan.org',
                web     => URI->new('https://metacpan.org/author/ANELSON'),
                email   => 'anelson at cpan.org',
            },
            i18n => {
                default_locale       => 'en',
                available_currencies => [ 'dolla dolla bill', 'CAD', 'EUR' ],
                available_locales    => [ 'en' ]
            }
        });
        is_deeply(
            $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'"
                    }
                }
            },
            "got the right errors"
        );
        ok(!$errors->has_extra_data, "no extra data");
    }
}

done_testing;