File: Class.pm

package info (click to toggle)
libvalidation-class-perl 7.900057-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,572 kB
  • ctags: 355
  • sloc: perl: 21,493; makefile: 2
file content (1807 lines) | stat: -rw-r--r-- 43,142 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
# ABSTRACT: Powerful Data Validation Framework

package Validation::Class;

use 5.10.0;
use strict;
use warnings;

use Module::Find;

use Validation::Class::Util '!has';
use Clone 'clone';
use Exporter ();

use Validation::Class::Prototype;

our $VERSION = '7.900057'; # VERSION

our @ISA    = qw(Exporter);
our @EXPORT = qw(

    adopt
    adt
    attribute
    bld
    build
    dir
    directive
    doc
    document
    ens
    ensure
    fld
    field
    flt
    filter
    has
    load
    msg
    message
    mth
    method
    mxn
    mixin
    pro
    profile
    set

);

sub return_class_proto {

    my $class = shift || caller(2);

    return prototype_registry->get($class) || do {

        # build new prototype class

        my $proto = Validation::Class::Prototype->new(
            package => $class
        );

        no strict 'refs';
        no warnings 'redefine';

        # respect foreign constructors (such as $class->new) if found

        my $new = $class->can("new") ?
            "initialize_validator" : "new"
        ;

        # injected into every derived class (override if necessary)

        *{"$class\::$new"}      = sub { goto \&$new };
        *{"$class\::proto"}     = sub { goto \&prototype };
        *{"$class\::prototype"} = sub { goto \&prototype };

        # inject prototype class aliases unless exist

        my @aliases = $proto->proxy_methods;

        foreach my $alias (@aliases) {

            next if $class->can($alias);

            # slight-of-hand

            $proto->set_method($alias, sub {

                shift @_;

                $proto->$alias(@_);

            });

        }

        # inject wrapped prototype class aliases unless exist

        my @wrapped_aliases = $proto->proxy_methods_wrapped;

        foreach my $alias (@wrapped_aliases) {

            next if $class->can($alias);

            # slight-of-hand

            $proto->set_method($alias, sub {

                my $self = shift @_;

                $proto->$alias($self, @_);

            });

        }

        # cache prototype
        prototype_registry->add($class => $proto);

        $proto; # return-once

    };

}

sub configure_class_proto {

    my $configuration_routine = pop;

    return unless "CODE" eq ref $configuration_routine;

    no strict 'refs';

    my $proto = return_class_proto shift;

    $configuration_routine->($proto);

    return $proto;

}

sub import {

    my $caller = caller(0) || caller(1);

    strict->import;
    warnings->import;

    __PACKAGE__->export_to_level(1, @_);

    return return_class_proto $caller # provision prototype when used

}

sub initialize_validator {

    my $self   = shift;
    my $proto  = $self->prototype;

    my $arguments = $proto->build_args(@_);

    # provision a validation class configuration

    $proto->snapshot;

    # override prototype attributes if requested

    if (defined($arguments->{fields})) {
        my $fields = delete $arguments->{fields};
        $proto->fields->clear->add($fields);
    }

    if (defined($arguments->{params})) {
        my $params = delete $arguments->{params};
        $proto->params->clear->add(clone $params);
    }

    # process attribute assignments

    my $proxy_methods = { map { $_ => 1 } ($proto->proxy_methods) } ;

    while (my($name, $value) = each (%{$arguments})) {

        $self->$name($value) if

            $self->can($name)              &&
            $proto->fields->has($name)     ||
            $proto->attributes->has($name) || $proxy_methods->{$name}

        ;

    }

    # process builders

    foreach my $builder ($proto->builders->list) {

        $builder->($self, $arguments);

    }

    # initialize prototype

    $proto->normalize($self);

    # ready-set-go !!!

    return $self;

}




sub adt { goto &adopt } sub adopt {

    my $package = shift if @_ == 4;

    my ($class, $type, $name) = @_;

    my $aliases = {
        has => 'attribute',
        dir => 'directive',
        doc => 'document',
        fld => 'field',
        flt => 'filter',
        msg => 'message',
        mth => 'method',
        mxn => 'mixin',
        pro => 'profile'
    };

    my $keywords = { map { $_ => $_ } values %{$aliases} };

    $type = $keywords->{$type} || $aliases->{$type};

    return unless $class && $name && $type;

    my $store  = "${type}s";
    my $config = prototype_registry->get($class)->configuration;
    my $data   = clone $config->$store->get($name);

    @_ = ($name => $data) and goto &$type;

    return;

}


sub has { goto &attribute } sub attribute {

    my $package = shift if @_ == 3;

    my ($attributes, $default) = @_;

    return unless $attributes;

    $attributes = [$attributes] unless isa_arrayref $attributes;

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_attribute($_ => $default) for @{$attributes};

        return $proto;

    };

}


sub bld { goto &build } sub build {

    my $package = shift if @_ == 2;

    my ($code) = @_;

    return unless ("CODE" eq ref $code);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_builder($code);

        return $proto;

    };

}


sub dir { goto &directive } sub directive {

    my $package = shift if @_ == 3;

    my ($name, $code) = @_;

    return unless ($name && $code);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_directive($name, $code);

        return $proto;

    };

}


sub doc { goto &document } sub document {

    my $package = shift if @_ == 3;

    my ($name, $data) = @_;

    $data ||= {};

    return unless ($name && $data);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_document($name, $data);

        return $proto;

    };

};



sub ens { goto &ensure } sub ensure {

    my $package = shift if @_ == 3;

    my ($name, $data) = @_;

    $data ||= {};

    return unless ($name && $data);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_ensure($name, $data);

        return $proto;

    };

}


sub fld { goto &field } sub field {

    my $package = shift if @_ == 3;

    my ($name, $data) = @_;

    $data ||= {};

    return unless ($name && $data);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_field($name, $data);

        return $proto;

    };

}


sub flt { goto &filter } sub filter {

    my $package = shift if @_ == 3;

    my ($name, $code) = @_;

    return unless ($name && $code);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_filter($name, $code);

        return $proto;

    };

}


sub set { goto &load } sub load {

    my $package;
    my $data;

    # handle different types of invocations

    # 1   - load({})
    # 2+  - load(a => b)
    # 2+  - package->load({})
    # 3+  - package->load(a => b)

    # --

    # load({})

    if (@_ == 1) {

        if ("HASH" eq ref $_[0]) {

            $data = shift;

        }

    }

    # load(a => b)
    # package->load({})

    elsif (@_ == 2) {

        if ("HASH" eq ref $_[-1]) {

            $package = shift;
            $data    = shift;

        }

        else {

            $data = {@_};

        }

    }

    # load(a => b)
    # package->load(a => b)

    elsif (@_ >= 3) {

        if (@_ % 2) {

            $package = shift;
            $data    = {@_};

        }

        else {

            $data = {@_};

        }

    }

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_settings($data);

        return $proto;

    };

}


sub msg { goto &message } sub message {

    my $package = shift if @_ == 3;

    my ($name, $template) = @_;

    return unless ($name && $template);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_message($name, $template);

        return $proto;

    };

}


sub mth { goto &method } sub method {

    my $package = shift if @_ == 3;

    my ($name, $data) = @_;

    return unless ($name && $data);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_method($name, $data);

        return $proto;

    };

}


sub mxn { goto &mixin } sub mixin {

    my $package = shift if @_ == 3;

    my ($name, $data) = @_;

    $data ||= {};

    return unless ($name && $data);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_mixin($name, $data);

        return $proto;

    };

}


sub new {

    my $class = shift;

    $class = ref $class || $class;

    my $proto = return_class_proto $class;

    my $self  = bless {},  $class;

    initialize_validator $self, @_;

    return $self;

}


sub pro { goto &profile } sub profile {

    my $package = shift if @_ == 3;

    my ($name, $code) = @_;

    return unless ($name && $code);

    return configure_class_proto $package => sub {

        my ($proto) = @_;

        $proto->register_profile($name, $code);

        return $proto;

    };

}


sub proto { goto &prototype } sub prototype {

    my ($self) = pop @_;

    return return_class_proto ref $self || $self;

}


1;

__END__

=pod

=head1 NAME

Validation::Class - Powerful Data Validation Framework

=head1 VERSION

version 7.900057

=head1 SYNOPSIS

    use Validation::Class::Simple::Streamer;

    my  $params = {username => 'admin', password => 's3cret'};
    my  $input  = Validation::Class::Simple::Streamer->new(params => $params);

    # check username parameter
    $input->check('username')->required->between('5-255');
    $input->filters([qw/trim strip/]);

    # check password parameter
    $input->check('password')->required->between('5-255')->min_symbols(1);
    $input->filters([qw/trim strip/]);

    # run validate
    $input->validate or die $input->errors_to_string;

=head1 DESCRIPTION

Validation::Class is a scalable data validation library with interfaces for
applications of all sizes. The most common usage of Validation::Class is to
transform class namespaces into data validation domains where consistency and
reuse are primary concerns. Validation::Class provides an extensible framework
for defining reusable data validation rules. It ships with a complete set of
pre-defined validations and filters referred to as
L<"directives"|Validation::Class::Directives/DIRECTIVES>.

The core feature-set consist of self-validating methods, validation profiles,
reusable validation rules and templates, pre and post input filtering, class
inheritance, automatic array handling, and extensibility (e.g. overriding
default error messages, creating custom validators, creating custom input
filters and much more). Validation::Class promotes DRY (don't repeat yourself)
code. The main benefit in using Validation::Class is that the architecture is
designed to increase the consistency of data input handling. The following is
a more traditional usage of Validation::Class, using the DSL to construct a
validator class:

    package MyApp::Person;

    use Validation::Class;

    # data validation template
    mixin basic     => {
        required    => 1,
        max_length  => 255,
        filters     => [qw/trim strip/]
    };

    # data validation rules for the username parameter
    field username  => {
        mixin       => 'basic',
        min_length  => 5
    };

    # data validation rules for the password parameter
    field password  => {
        mixin       => 'basic',
        min_length  => 5,
        min_symbols => 1
    };

    package main;

    my $person = MyApp::Person->new(username => 'admin', password => 'secr3t');

    # validate rules on the person object
    unless ($person->validates) {
        # handle the failures
        warn $person->errors_to_string;
    }

    1;

=head1 QUICKSTART

If you are looking for a simple in-line data validation module built
using the same tenets and principles as Validation::Class, please review
L<Validation::Class::Simple> or L<Validation::Class::Simple::Streamer>. If you
are new to Validation::Class, or would like more information on the
underpinnings of this library and how it views and approaches data validation,
please review L<Validation::Class::Whitepaper>. Please review the
L<Validation::Class::Cookbook/GUIDED-TOUR> for a detailed step-by-step look into
how Validation::Class works.

=head1 KEYWORDS

=head2 adopt

The adopt keyword (or adt) copies configuration and functionality from
other Validation::Class classes. The adopt keyword takes three arguments, the
name of the class to be introspected, and the configuration type and name to be
recreated. Basically, anything you can configure using a Validation::Class
keyword can be adopted into other classes using this keyword with the exception
of coderefs registered using the build keyword. Please note! If you are adopting
a field declaration which has an associated mixin directive defined on the
target class, you must adopt the mixin explicitly if you wish it's values to be
interpolated.

    package MyApp::Exployee;

    use Validate::Class;
    use MyApp::Person;

    adopt MyApp::Person, mixin   => 'basic';
    adopt MyApp::Person, field   => 'first_name';
    adopt MyApp::Person, field   => 'last_name';
    adopt MyApp::Person, profile => 'has_fullname';

    1;

=head2 attribute

The attribute keyword (or has) registers a class attribute, i.e. it creates an
accessor (getter and setter) on the class. Attribute declaration is flexible and
only requires an attribute name to be configured. Additionally, the attribute
keyword can takes two arguments, the attribute's name and a scalar or coderef to
be used as it's default value.

    package MyApp::Person;

    use Validate::Class;

    attribute 'first_name' => 'Peter';
    attribute 'last_name'  => 'Venkman';
    attribute 'full_name'  => sub {
        join ', ', $_[0]->last_name, $_[0]->first_name
    };

    attribute 'email_address';

    1;

=head2 build

The build keyword (or bld) registers a coderef to be run at instantiation much
in the same way the common BUILD routine is used in modern OO frameworks.

    package MyApp::Person;

    use Validation::Class;

    build sub {

        my ($self, $args) = @_;

        # run after instantiation in the order defined

    };

    1;

The build keyword takes one argument, a coderef which is passed the instantiated
class object.

=head2 directive

The directive keyword (or dir) registers custom validator directives to be used
in your field definitions. Please note that custom directives can only be used
with field definitions. This is a means of extending the list of directives per
instance. See the list of core directives, L<Validation::Class::Directives>,
or review L<Validation::Class::Directive> for insight into creating your own
CPAN installable directives.

    package MyApp::Person;

    use Validate::Class;

    # define a custom class-level directive
    directive 'blacklisted' => sub {

        my ($self, $field, $param) = @_;

        if (defined $field->{blacklisted} && defined $param) {
            if ($field->{required} || $param) {
                if (exists_in_blacklist($field->{blacklisted}, $param)) {
                    my $handle = $field->label || $field->name;
                    $field->errors->add("$handle has been blacklisted");
                    return 0;
                }
            }
        }

        return 1;

    };

    field 'email_address' => {
        blacklisted => '/path/to/blacklist'
        email => 1,
    };

    1;

The directive keyword takes two arguments, the name of the directive and a
coderef which will be used to validate the associated field. The coderef is
passed four ordered parameters; a directive object, the class prototype object,
the current field object, and the matching parameter's value. The validator
(coderef) is evaluated by its return value as well as whether it altered any
error containers.

=head2 document

The document keyword (or doc) registers a data matching profile which can be
used to validate heiarchal data. It will store a hashref with pre-define path
matching rules for the data structures you wish to validate. The "path matching
rules", which use a specialized object notation, referred to as the document
notation, can be thought of as a kind-of simplified regular expression which is
executed against the flattened data structure. The following are a few general
use-cases:

    package MyApp::Person;

    use Validation::Class;

    field  'string' => {
        mixin => [':str']
    };

    # given this JSON data structure
    {
        "id": "1234-A",
        "name": {
            "first_name" : "Bob",
            "last_name"  : "Smith",
         },
        "title": "CIO",
        "friends" : [],
    }

    # select id to validate against the string rule
    document 'foobar'  =>
        { 'id' => 'string' };

    # select name -> first_name/last_name to validate against the string rule
    document 'foobar'  =>
        {'name.first_name' => 'string', 'name.last_name' => 'string'};

    # or
    document 'foobar'  =>
        {'name.*_name' => 'string'};

    # select each element in friends to validate against the string rule
    document 'foobar'  =>
        { 'friends.@'  => 'string' };

    # or select an element of a hashref in each element in friends to validate
    # against the string rule
    document 'foobar'  =>
        { 'friends.@.name' => 'string' };

The document declaration's keys should follow the aforementioned document
notation schema and it's values should be strings which correspond to the names
of fields (or other document declarations) that will be used to preform the
data validation. It is possible to combine document declarations to validate
hierarchical data that contains data structures matching one or more document
patterns. The following is an example of what that might look like.

    package MyApp::Person;

    use Validation::Class;

    # data validation rule
    field  'name' => {
        mixin      => [':str'],
        pattern    => qr/^[A-Za-z ]+$/,
        max_length => 20,
    };

    # data validation map / document notation schema
    document 'friend' => {
        'name' => 'name'
    };

    # data validation map / document notation schema
    document 'person' => {
        'name' => 'name',
        'friends.@' => 'friend'
    };

    package main;

    my $data = {
        "name"   => "Anita Campbell-Green",
        "friends" => [
            { "name" => "Horace" },
            { "name" => "Skinner" },
            { "name" => "Alonzo" },
            { "name" => "Frederick" },
        ],
    };

    my $person = MyApp::Person->new;

    unless ($person->validate_document(person => $data)) {
        warn $person->errors_to_string if $person->error_count;
    }

    1;

Alternatively, the following is a more verbose data validation class using
traditional styling and configuration.

    package MyApp::Person;

    use Validation::Class;

    field  'id' => {
        mixin      => [':str'],
        filters    => ['numeric'],
        max_length => 2,
    };

    field  'name' => {
        mixin      => [':str'],
        pattern    => qr/^[A-Za-z ]+$/,
        max_length => 20,
    };

    field  'rating' => {
        mixin      => [':str'],
        pattern    => qr/^\-?\d+$/,
    };

    field  'tag' => {
        mixin      => [':str'],
        pattern    => qr/^(?!evil)\w+/,
        max_length => 20,
    };

    document 'person' => {
        'id'                             => 'id',
        'name'                           => 'name',
        'company.name'                   => 'name',
        'company.supervisor.name'        => 'name',
        'company.supervisor.rating.@.*'  => 'rating',
        'company.tags.@'                 => 'name'
    };

    package main;

    my $data = {
        "id"      => "1234-ABC",
        "name"    => "Anita Campbell-Green",
        "title"   => "Designer",
        "company" => {
            "name"       => "House of de Vil",
            "supervisor" => {
                "name"   => "Cruella de Vil",
                "rating" => [
                    {   "support"  => -9,
                        "guidance" => -9
                    }
                ]
            },
            "tags" => [
                "evil",
                "cruelty",
                "dogs"
            ]
        },
    };

    my $person = MyApp::Person->new;

    unless ($person->validate_document(person => $data)) {
        warn $person->errors_to_string if $person->error_count;
    }

    1;

Additionally, the following is yet another way to validate a document by
passing the document specification directly instead of by name.

    package MyApp::Person;

    use Validation::Class;

    package main;

    my $data = {
        "id"      => "1234-ABC",
        "name"    => "Anita Campbell-Green",
        "title"   => "Designer",
        "company" => {
            "name"       => "House of de Vil",
            "supervisor" => {
                "name"   => "Cruella de Vil",
                "rating" => [
                    {   "support"  => -9,
                        "guidance" => -9
                    }
                ]
            },
            "tags" => [
                "evil",
                "cruelty",
                "dogs"
            ]
        },
    };

    my $spec = {
        'id'                            => { max_length => 2 },
        'name'                          => { mixin      => ':str' },
        'company.name'                  => { mixin      => ':str' },
        'company.supervisor.name'       => { mixin      => ':str' },
        'company.supervisor.rating.@.*' => { pattern    => qr/^(?!evil)\w+/ },
        'company.tags.@'                => { max_length => 20 },
    };

    my $person = MyApp::Person->new;

    unless ($person->validate_document($spec => $data)) {
        warn $person->errors_to_string if $person->error_count;
    }

    1;

=head2 ensure

The ensure keyword (or ens) is used to convert a pre-existing method
into an auto-validating method. The auto-validating method will be
registered and function as if it was created using the method keyword.
The original pre-existing method will be overridden with a modifed version
which performs the pre and/or post validation routines.

    package MyApp::Person;

    use Validation::Class;

    sub register {
        ...
    }

    ensure register => {
        input  => ['name', '+email', 'username', '+password', '+password2'],
        output => ['+id'], # optional output validation, dies on failure
    };

    package main;

    my $person = MyApp::Person->new(params => $params);

    if ($person->register) {
        # handle the successful registration
    }

    1;

The ensure keyword takes two arguments, the name of the method to be
overridden and a hashref of required key/value pairs. The hashref may
have an input key (e.g. input, input_document, input_profile, or input_method).
The `input` key (specifically) must have a value which must be either an
arrayref of fields to be validated, or a scalar value which matches (a
validation profile or auto-validating method name). The hashref may also have
an output key (e.g. output, output_document, output_profile, or output_method).
The `output` key (specifically) must have a value which must be either an
arrayref of fields to be validated, or a scalar value which matches (a
validation profile or auto-validating method name). Whether and what the
method returns is yours to decide. The method will return undefined if
validation fails. The ensure keyword wraps and functions much in the same way
as the method keyword.

=head2 field

The field keyword (or fld) registers a data validation rule for reuse and
validation in code. The field name should correspond with the parameter name
expected to be passed to your validation class or validated against.

    package MyApp::Person;

    use Validation::Class;

    field 'username' => {
        required   => 1,
        min_length => 1,
        max_length => 255
    };

The field keyword takes two arguments, the field name and a hashref of key/values
pairs known as directives. For more information on pre-defined directives, please
review the L<"list of core directives"|Validation::Class::Directives/DIRECTIVES>.

The field keyword also creates accessors which provide easy access to the
field's corresponding parameter value(s). Accessors will be created using the
field's name as a label having any special characters replaced with an
underscore.

    # accessor will be created as send_reminders
    field 'send-reminders' => {
        length => 1
    };

Please note that prefixing field names with a double plus-symbol instructs the
register to merge your declaration with any pre-existing declarations within the
same scope (e.g. fields imported via loading roles), whereas prefixing field
names with a single plus-symbol instructs the register to overwrite any
pre-existing declarations.

    package MyApp::Person;

    use Validation::Class;

    set role => 'MyApp::User';

    # append existing field and overwrite directives
    field '++email_address' => {
        required => 1
    };

    # redefine existing field
    field '+login' => {
        required => 1
    };

    1;

=head2 filter

The filter keyword (or flt) registers custom filters to be used in your field
definitions. It is a means of extending the pre-existing filters declared by
the L<"filters directive"|Validation::Class::Directive::Filters> before
instantiation.

    package MyApp::Person;

    use Validate::Class;

    filter 'flatten' => sub {
        $_[0] =~ s/[\t\r\n]+/ /g;
        return $_[0];
    };

    field 'biography' => {
        filters => ['trim', 'strip', 'flatten']
    };

    1;

The filter keyword takes two arguments, the name of the filter and a
coderef which will be used to filter the value the associated field. The coderef
is passed the value of the field and that value MUST be operated on directly.
The coderef should also return the transformed value.

=head2 load

The load keyword (or set), which can also be used as a class method, provides
options for extending the current class by declaring roles, requirements, etc.

The process of applying roles, requirement, and other settings to the current
class mainly involves introspecting the namespace's methods and merging relevant
parts of the prototype configuration.

=head2 load-classes

The `classes` (or class) option uses L<Module::Find> to load all child classes
(in-all-subdirectories) for convenient access through the
L<Validation::Class::Prototype/class> method, and when introspecting a larger
application. This option accepts an arrayref or single argument.

    package MyApp;

    use Validation::Class;

    load classes => ['MyApp::Domain1', 'MyApp::Domain2'];

    package main;

    my $app = MyApp->new;

    my $person = $app->class('person'); # return a new MyApp::Person object

    1;

=head2 load-requirements

    package MyApp::User;

    use Validate::Class;

    load requirements => 'activate';

    package MyApp::Person;

    use Validation::Class;

    load role => 'MyApp::User';

    sub activate {}

    1;

The `requirements` (or required) option is used to ensure that if/when the class
is used as a role the calling class has specific pre-existing methods. This
option accepts an arrayref or single argument.

    package MyApp::User;

    use Validate::Class;

    load requirements => ['activate', 'deactivate'];

    1;

=head2 load-roles

    package MyApp::Person;

    use Validation::Class;

    load role => 'MyApp::User';

    1;

The `roles` (or role) option is used to load and inherit functionality from
other validation classes. These classes should be used and thought-of as roles
although they can also be fully-functioning validation classes. This option
accepts an arrayref or single argument.

    package MyApp::Person;

    use Validation::Class;

    load roles => ['MyApp::User', 'MyApp::Visitor'];

    1;

=head2 message

The message keyword (or msg) registers a class-level error message template that
will be used in place of the error message defined in the corresponding directive
class if defined. Error messages can also be overridden at the individual
field-level as well. See the L<Validation::Class::Directive::Messages> for
instructions on how to override error messages at the field-level.

    package MyApp::Person;

    use Validation::Class;

    field email_address => {
        required   => 1,
        min_length => 3,
        messages   => {
            # field-level error message override
            min_length => '%s is not even close to being a valid email address'
        }
    };

    # class-level error message overrides
    message required   => '%s is needed to proceed';
    message min_length => '%s needs more characters';

    1;

The message keyword takes two arguments, the name of the directive whose error
message you wish to override and a string which will be used to as a template
which is feed to sprintf to format the message.

=head2 method

The method keyword (or mth) is used to register an auto-validating method.
Similar to method signatures, an auto-validating method can leverage
pre-existing validation rules and profiles to ensure a method has the
required pre/post-conditions and data necessary for execution.

    package MyApp::Person;

    use Validation::Class;

    method 'register' => {

        input  => ['name', '+email', 'username', '+password', '+password2'],
        output => ['+id'], # optional output validation, dies on failure
        using  => sub {

            my ($self, @args) = @_;

            # do something registrationy
            $self->id(...); # set the ID field for output validation

            return $self;

        }

    };

    package main;

    my $person = MyApp::Person->new(params => $params);

    if ($person->register) {

        # handle the successful registration

    }

    1;

The method keyword takes two arguments, the name of the method to be created
and a hashref of required key/value pairs. The hashref may have a `using` key
whose value is the coderef to be executed upon successful validation. The
`using` key is only optional when a pre-existing subroutine has the same name
or the method being declared prefixed with a dash or dash-process-dash. The
following are valid subroutine names to be called by the method declaration in
absence of a `using` key. Please note, unlike the ensure keyword, any
pre-existing subroutines will not be wrapped-and-replaced and can be executed
without validation if called directly.

    sub _name {
        ...
    }

    sub _process_name {
        ...
    }

The hashref may have an input key
(e.g. input, input_document, input_profile, or input_method). The `input` key
(specifically) must have a value which must be either an arrayref of fields to
be validated, or a scalar value which matches (a validation profile or
auto-validating method name), which will be used to perform data validation
B<before> the aforementioned coderef has been executed. Whether and what the
method returns is yours to decide. The method will return undefined if
validation fails.

    # alternate usage

    method 'registration' => {
        input  => ['name', '+email', 'username', '+password', '+password2'],
        output => ['+id'], # optional output validation, dies on failure
    };

    sub _process_registration {
        my ($self, @args) = @_;
            $self->id(...); # set the ID field for output validation
        return $self;
    }

Optionally the hashref may also have an output key (e.g. output,
output_document, output_profile, or output_method). The `output` key
(specifically) must have a value which must be either an arrayref of
fields to be validated, or a scalar value which matches (a validation profile
or auto-validating method name), which will be used to perform data validation
B<after> the aforementioned coderef has been executed.

Please note that output validation failure will cause the program to die,
the premise behind this decision is based on the assumption that given
successfully validated input a routine's output should be predictable and
if an error occurs it is most-likely a program error as opposed to a user error.

See the ignore_failure and report_failure attributes on the prototype to
control how method validation failures are handled.

=head2 mixin

The mixin keyword (or mxn) registers a validation rule template that can be
applied (or "mixed-in") to any field by specifying the mixin directive. Mixin
directives are processed first so existing field directives will override any
directives created by the mixin directive.

    package MyApp::Person;

    use Validation::Class;

    mixin 'boilerplate' => {
        required   => 1,
        min_length => 1,
        max_length => 255
    };

    field 'username' => {
        # min_length, max_length, .. required will be overridden
        mixin    => 'boilerplate',
        required => 0
    };

Since version 7.900015, all classes are automatically configured with the
following default mixins for the sake of convenience:

    mixin ':flg' => {
        required   => 1,
        min_length => 1,
        filters    => [qw/trim strip numeric/],
        between    => [0, 1]
    };

    mixin ':num' => {
        required   => 1,
        min_length => 1,
        filters    => [qw/trim strip numeric/]
    };

    mixin ':str' => {
        required   => 1,
        min_length => 1,
        filters    => [qw/trim strip/]
    };

Please note that the aforementioned mixin names are prefixed with a semi-colon but
are treated as an exception to the rule. Prefixing mixin names with a double
plus-symbol instructs the register to merge your declaration with any pre-existing
declarations within the same scope (e.g. mixins imported via loading roles),
whereas prefixing mixin names with a single plus-symbol instructs the register
to overwrite any pre-existing declarations.

    package MyApp::Moderator;

    use Validation::Class;

    set role => 'MyApp::Person';

    # overwrite and append existing mixin
    mixin '++boilerplate' => {
        min_symbols => 1
    };

    # redefine existing mixin
    mixin '+username' => {
        required => 1
    };

    1;

The mixin keyword takes two arguments, the mixin name and a hashref of key/values
pairs known as directives.

=head2 profile

The profile keyword (or pro) registers a validation profile (coderef) which as
in the traditional use of the term is a sequence of validation routines that
validates data relevant to a specific action.

    package MyApp::Person;

    use Validation::Class;

    profile 'check_email' => sub {

        my ($self, @args) = @_;

        if ($self->email_exists) {
            my $email = $self->fields->get('email');
            $email->errors->add('Email already exists');
            return 0;
        }

        return 1;

    };

    package main;

    my $user = MyApp::Person->new(params => $params);

    unless ($user->validate_profile('check_email')) {
        # handle failures
    }

    1;

The profile keyword takes two arguments, a profile name and coderef which will
be used to execute a sequence of actions for validation purposes.

=head1 METHODS

=head2 new

The new method instantiates a new class object, it performs a series of actions
(magic) required for the class to function properly, and for that reason, this
method should never be overridden. Use the build keyword for hooking into the
instantiation process.

In the event a foreign (pre-existing) `new` method is detected, an
`initialize_validator` method will be injected into the class containing the
code (magic) necessary to normalize your environment.

    package MyApp::Person;

    use Validation::Class;

    # hook
    build sub {

        my ($self, @args) = @_; # on instantiation

    };

    sub new {

        # rolled my own
        my $self = bless {}, shift;

        # execute magic
        $self->initialize_validator;

    }

    1;

=head2 prototype

The prototype method (or proto) returns an instance of the associated class
prototype. The class prototype is responsible for manipulating and validating
the data model (the class). It is not likely that you'll need to access
this method directly, see L<Validation::Class::Prototype>.

    package MyApp::Person;

    use Validation::Class;

    package main;

    my $person = MyApp::Person->new;

    my $prototype = $person->prototype;

    1;

=head1 PROXY METHODS

Validation::Class mostly provides sugar functions for modeling your data
validation requirements. Each class you create is associated with a prototype
class which provides the data validation engine and keeps your class namespace
free from pollution, please see L<Validation::Class::Prototype> for more
information on specific methods and attributes. Validation::Class injects a few
proxy methods into your class which are basically aliases to the corresponding
prototype class methods, however it is possible to access the prototype directly
using the proto/prototype methods.

=head2 class

    $self->class;

See L<Validation::Class::Prototype/class> for full documentation.

=head2 clear_queue

    $self->clear_queue;

See L<Validation::Class::Prototype/clear_queue> for full documentation.

=head2 error_count

    $self->error_count;

See L<Validation::Class::Prototype/error_count> for full documentation.

=head2 error_fields

    $self->error_fields;

See L<Validation::Class::Prototype/error_fields> for full documentation.

=head2 errors

    $self->errors;

See L<Validation::Class::Prototype/errors> for full documentation.

=head2 errors_to_string

    $self->errors_to_string;

See L<Validation::Class::Prototype/errors_to_string> for full documentation.

=head2 get_errors

    $self->get_errors;

See L<Validation::Class::Prototype/get_errors> for full documentation.

=head2 get_fields

    $self->get_fields;

See L<Validation::Class::Prototype/get_fields> for full documentation.

=head2 get_hash

    $self->get_hash;

See L<Validation::Class::Prototype/get_hash> for full documentation.

=head2 get_params

    $self->get_params;

See L<Validation::Class::Prototype/get_params> for full documentation.

=head2 get_values

    $self->get_values;

See L<Validation::Class::Prototype/get_values> for full documentation.

=head2 fields

    $self->fields;

See L<Validation::Class::Prototype/fields> for full documentation.

=head2 filtering

    $self->filtering;

See L<Validation::Class::Prototype/filtering> for full documentation.

=head2 ignore_failure

    $self->ignore_failure;

See L<Validation::Class::Prototype/ignore_failure> for full documentation.

=head2 ignore_intervention

    $self->ignore_intervention;

See L<Validation::Class::Prototype/ignore_intervention> for full documentation.

=head2 ignore_unknown

    $self->ignore_unknown;

See L<Validation::Class::Prototype/ignore_unknown> for full documentation.

=head2 is_valid

    $self->is_valid;

See L<Validation::Class::Prototype/is_valid> for full documentation.

=head2 param

    $self->param;

See L<Validation::Class::Prototype/param> for full documentation.

=head2 params

    $self->params;

See L<Validation::Class::Prototype/params> for full documentation.

=head2 plugin

    $self->plugin;

See L<Validation::Class::Prototype/plugin> for full documentation.

=head2 queue

    $self->queue;

See L<Validation::Class::Prototype/queue> for full documentation.

=head2 report_failure

    $self->report_failure;

See L<Validation::Class::Prototype/report_failure> for full
documentation.

=head2 report_unknown

    $self->report_unknown;

See L<Validation::Class::Prototype/report_unknown> for full documentation.

=head2 reset_errors

    $self->reset_errors;

See L<Validation::Class::Prototype/reset_errors> for full documentation.

=head2 reset_fields

    $self->reset_fields;

See L<Validation::Class::Prototype/reset_fields> for full documentation.

=head2 reset_params

    $self->reset_params;

See L<Validation::Class::Prototype/reset_params> for full documentation.

=head2 set_errors

    $self->set_errors;

See L<Validation::Class::Prototype/set_errors> for full documentation.

=head2 set_fields

    $self->set_fields;

See L<Validation::Class::Prototype/set_fields> for full documentation.

=head2 set_params

    $self->set_params;

See L<Validation::Class::Prototype/set_params> for full documentation.

=head2 set_method

    $self->set_method;

See L<Validation::Class::Prototype/set_method> for full documentation.

=head2 stash

    $self->stash;

See L<Validation::Class::Prototype/stash> for full documentation.

=head2 validate

    $self->validate;

See L<Validation::Class::Prototype/validate> for full documentation.

=head2 validate_document

    $self->validate_document;

See L<Validation::Class::Prototype/validate_document> for full documentation.

=head2 validate_method

    $self->validate_method;

See L<Validation::Class::Prototype/validate_method> for full documentation.

=head2 validate_profile

    $self->validate_profile;

See L<Validation::Class::Prototype/validate_profile> for full documentation.

=head1 UPGRADE

Validation::Class is stable, its feature-set is complete, and is currently in
maintenance-only mode, i.e. Validation::Class will only be updated with minor
enhancements and bug fixes. However, the lessons learned will be incorporated
into a compelete rewrite uploaded under the namespace L<Validation::Interface>.
The Validation::Interface fork is designed to have a much simpler API with less
options and better execution, focused on validating hierarchical data as its
primarily objective.

=head1 EXTENSIBILITY

Validation::Class does NOT provide method modifiers but can be easily extended
with L<Class::Method::Modifiers>.

=head2 before

    before foo => sub { ... };

See L<< Class::Method::Modifiers/before method(s) => sub { ... } >> for full
documentation.

=head2 around

    around foo => sub { ... };

See L<< Class::Method::Modifiers/around method(s) => sub { ... } >> for full
documentation.

=head2 after

    after foo => sub { ... };

See L<< Class::Method::Modifiers/after method(s) => sub { ... } >> for full
documentation.

=head1 SEE ALSO

Validation::Class does not validate blessed objects. If you need a means for
validating object types you should use a modern object system like L<Moo>,
L<Mouse>, or L<Moose>. Alternatively, you could use decoupled object
validators like L<Type::Tiny>, L<Params::Validate> or L<Specio>.

=head1 AUTHOR

Al Newkirk <anewkirk@ana.io>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Al Newkirk.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut