summaryrefslogtreecommitdiff
path: root/lib/TUWF/Misc.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/TUWF/Misc.pm')
-rw-r--r--lib/TUWF/Misc.pm43
1 files changed, 40 insertions, 3 deletions
diff --git a/lib/TUWF/Misc.pm b/lib/TUWF/Misc.pm
index 91b3ae2..edea8de 100644
--- a/lib/TUWF/Misc.pm
+++ b/lib/TUWF/Misc.pm
@@ -13,7 +13,6 @@ use TUWF::Validate;
our $VERSION = '1.2';
-our @EXPORT = ('formValidate', 'mail');
our @EXPORT_OK = ('uri_escape', 'kv_validate');
@@ -148,7 +147,7 @@ sub _validate { # value, \%templates, \%rules
-sub formValidate {
+sub TUWF::Object::formValidate {
my($self, @fields) = @_;
return kv_validate(
{ post => sub { $self->reqPosts(shift) },
@@ -164,7 +163,7 @@ sub formValidate {
# A simple mail function, body and headers as arguments. Usage:
# $self->mail('body', header1 => 'value of header 1', ..);
-sub mail {
+sub TUWF::Object::mail {
my $self = shift;
my $body = shift;
my %hs = @_;
@@ -192,4 +191,42 @@ sub mail {
}
+sub TUWF::Object::compile {
+ TUWF::Validate::compile($_[0]{_TUWF}{custom_validations}, $_[1]);
+}
+
+
+sub _compile {
+ ref $_[0] eq 'TUWF::Validate' ? $_[0] : $TUWF::OBJ->compile($_[0]);
+}
+
+
+sub TUWF::Object::validate {
+ my $self = shift;
+ my $what = shift;
+
+ return _compile($_[0])->validate($self->reqJSON) if $what eq 'json';
+
+ # 'param' is special, and not really encouraged. Create a new hash based on
+ # reqParam() and cache the result.
+ $self->{_TUWF}{Req}{PARAM} ||= {
+ map { my @v = $self->reqParams($_); +($_, @v > 1 ? \@v : $v[0]) } $self->reqParams()
+ } if $what eq 'param';
+
+ my $source =
+ $what eq 'get' ? $self->{_TUWF}{Req}{GET} :
+ $what eq 'post' ? $self->{_TUWF}{Req}{POST} :
+ $what eq 'param' ? $self->{_TUWF}{Req}{PARAM}
+ : croak "Invalid source type '$what'";
+
+ # Multi-value, schema hash or object
+ return _compile($_[0])->validate($source) if @_ == 1;
+
+ # Single value
+ return _compile($_[1])->validate($source->{$_[0]}) if @_ == 2;
+
+ # Multi-value, separate params
+ _comile({ @_ })->validate($source);
+}
+
1;