summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2018-05-11 19:24:30 +0200
committerYorhel <git@yorhel.nl>2018-05-11 19:24:30 +0200
commitcc26eedd495cd230e2cc45423f9d98000adde6d8 (patch)
tree86577cac0ebd9232ab44175db65f9de460409af6 /lib
parent6b3e403004b30d77b3011a6c65f0ad4f870cd622 (diff)
TUWF::Validate: Add enum validation
Diffstat (limited to 'lib')
-rw-r--r--lib/TUWF/Validate.pm5
1 files changed, 5 insertions, 0 deletions
diff --git a/lib/TUWF/Validate.pm b/lib/TUWF/Validate.pm
index adeb1d6..f7b0aa4 100644
--- a/lib/TUWF/Validate.pm
+++ b/lib/TUWF/Validate.pm
@@ -62,6 +62,11 @@ our %default_validations = (
# error object to ensure that.
+{ type => 'scalar', func => sub { $_[0] =~ $reg ? 1 : { regex => "$reg", got => $_[0] } } }
},
+ enum => sub {
+ my @l = ref $_[0] eq 'HASH' ? sort keys %{$_[0]} : ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]);
+ my %opts = map +($_,1), @l;
+ +{ type => 'scalar', func => sub { $opts{ (my $v = $_[0]) } ? 1 : { expected => \@l, got => $_[0] } } }
+ },
minlength => _length(sub { $_[0] >= $_[1] }),
maxlength => _length(sub { $_[0] <= $_[1] }),