Mojolicious-Plugin-FormValidatorLazy
view release on metacpan or search on metacpan
package Mojolicious::Plugin::FormValidatorLazy;
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Plugin';
our $VERSION = '0.03';
use Data::Dumper;
use Mojo::JSON qw(decode_json encode_json);
use Mojo::Util qw{encode decode xml_escape hmac_sha1_sum secure_compare
b64_decode b64_encode};
use HTML::ValidationRules::Legacy qw{validate extract};
our $TERM_ACTION = 0;
our $TERM_SCHEMA = 1;
### ---
### register
### ---
sub register {
my ($self, $app, $opt) = @_;
if (!$wrapper) {
return $opt->{blackhole}->($c,
'Form schema is missing, possible hacking attempt');
}
if ($req->url->path ne $wrapper->{$TERM_ACTION}) {
return $opt->{blackhole}->($c,
'Action attribute has been tampered');
}
if (my $err = validate($wrapper->{$TERM_SCHEMA}, $req->params)) {
return $opt->{blackhole}->($c, $err);
}
}
});
$app->hook(after_dispatch => sub {
my $c = shift;
if ($c->res->headers->content_type =~ qr{^text/html} &&
$c->res->body =~ qr{<form\b}i) {
lib/HTML/ValidationRules/Legacy.pm view on Meta::CPAN
package HTML::ValidationRules::Legacy;
use strict;
use warnings;
use Mojo::Base 'Exporter';
use Mojo::JSON;
use Mojo::Util qw{decode};
use Mojo::Parameters;
use Scalar::Util qw(blessed);
our @EXPORT_OK = qw(extract validate),
our $TERM_PROPERTIES = 'properties';
our $TERM_REQUIRED = 'required';
our $TERM_MAXLENGTH = 'maxLength';
our $TERM_MIN_LENGTH = 'minLength';
our $TERM_OPTIONS = 'options';
our $TERM_PATTERN = 'pattern';
our $TERM_MIN = 'maximam';
our $TERM_MAX = 'minimum';
our $TERM_TYPE = 'type';
lib/HTML/ValidationRules/Legacy.pm view on Meta::CPAN
$props->{$name}->{$TERM_PATTERN} = $tag->attr->{pattern};
}
});
return {
$TERM_PROPERTIES => $props,
$TERM_ADD_PROPS => Mojo::JSON->false,
};
}
sub validate {
my ($schema, $params, $charset) = @_;
if (! (blessed($params) && $params->isa('Mojo::Parameters'))) {
my $wrapper = Mojo::Parameters->new;
$wrapper->charset($charset);
if (blessed($params) && $params->isa('Hash::MultiValue')) {
$wrapper->append($params->flatten);
} else {
$wrapper->append($params);
}
lib/HTML/ValidationRules/Legacy.pm view on Meta::CPAN
=head2 CLASS METHODS
=head3 extract
my $schema = extract($form_in_strig, $charset)
my $schema = extract($form_in_mojo_dom)
Generates a schema out of form string or Mojo::DOM instance. It returns
schema in hashref consists of JSON-schema-like properties.
=head3 validate
Validates form data against given schema.
my $error = validate($params_in_string, $schema, $charset);
my $error = validate($params_in_mojo_params, $schema);
=head1 AUTHOR
Sugama Keita, E<lt>sugama@jamadam.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) Sugama Keita.
This program is free software; you can redistribute it and/or
lib/Mojolicious/Plugin/FormValidatorLazy.pm view on Meta::CPAN
package Mojolicious::Plugin::FormValidatorLazy;
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Plugin';
our $VERSION = '0.03';
use Data::Dumper;
use Mojo::JSON qw(decode_json encode_json);
use Mojo::Util qw{encode decode xml_escape hmac_sha1_sum secure_compare
b64_decode b64_encode};
use HTML::ValidationRules::Legacy qw{validate extract};
our $TERM_ACTION = 0;
our $TERM_SCHEMA = 1;
### ---
### register
### ---
sub register {
my ($self, $app, $opt) = @_;
lib/Mojolicious/Plugin/FormValidatorLazy.pm view on Meta::CPAN
if (!$wrapper) {
return $opt->{blackhole}->($c,
'Form schema is missing, possible hacking attempt');
}
if ($req->url->path ne $wrapper->{$TERM_ACTION}) {
return $opt->{blackhole}->($c,
'Action attribute has been tampered');
}
if (my $err = validate($wrapper->{$TERM_SCHEMA}, $req->params)) {
return $opt->{blackhole}->($c, $err);
}
}
});
$app->hook(after_dispatch => sub {
my $c = shift;
if ($c->res->headers->content_type =~ qr{^text/html} &&
$c->res->body =~ qr{<form\b}i) {
( run in 2.180 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )