Bugzilla – Attachment 154125 Details for
Bug 34430
Add Koha::Validator module
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 34430: Module for validating input
Bug-34430-Module-for-validating-input.patch (text/plain), 3.86 KB, created by
Marcel de Rooy
on 2023-08-01 11:55:39 UTC
(
hide
)
Description:
Bug 34430: Module for validating input
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2023-08-01 11:55:39 UTC
Size:
3.86 KB
patch
obsolete
>From 8ac083d13b3f429d8911cc870f03776ac72bd14b Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Thu, 27 Jul 2023 09:37:39 +0000 >Subject: [PATCH] Bug 34430: Module for validating input >Content-Type: text/plain; charset=utf-8 > >Test plan: >Run t/Koha/Validator.t >--- > Koha/Validator.pm | 57 +++++++++++++++++++++++++++++++++++++++++++++ > t/Koha/Validator.t | 58 ++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 115 insertions(+) > create mode 100644 Koha/Validator.pm > create mode 100755 t/Koha/Validator.t > >diff --git a/Koha/Validator.pm b/Koha/Validator.pm >new file mode 100644 >index 0000000000..e343ee5a35 >--- /dev/null >+++ b/Koha/Validator.pm >@@ -0,0 +1,57 @@ >+package Koha::Validator; >+ >+#TODO License >+ >+use Modern::Perl; >+use List::Util qw(any); >+use Koha::Exceptions; >+use Koha::Email; >+ >+our $default_rules = { >+ digits_only => qr/^\d+$/, >+ email => \&_check_email, >+}; >+ >+#TODO POD >+ >+sub new { >+ my ( $class, $params ) = @_; >+ my $self = bless $params // {}, $class; >+ return $self; >+} >+ >+sub load { >+ my ( $self, $rules ) = @_; >+ $self->{rules} = $rules if ref($rules) eq 'HASH'; >+} >+ >+sub check_array { #TODO Add later >+} >+ >+sub check_hash { >+ my ( $self, $values ) = @_; >+ return if ref($values) ne 'HASH'; >+ >+ foreach my $key (%$values) { >+ my $rule = $self->{rules}->{$key} or next; >+ $rule = $default_rules->{$rule} if ref($rule) eq ''; >+ if ( ref($rule) eq 'Regexp' ) { >+ next if $values->{$key} =~ $rule; >+ } elsif ( ref($rule) eq 'CODE' ) { >+ next if $rule->( $values->{$key} ); >+ } elsif ( ref($rule) eq 'ARRAY' ) { >+ next if any { $_ eq $values->{$key} } @$rule; >+ } >+ Koha::Exceptions::BadParameter->throw( parameter => $key ); >+ } >+ return 1; >+} >+ >+sub _check_email { # TODO Incorporate into Koha::Email? >+ my $email = shift; >+ return 0 if !Koha::Email->is_valid($email); >+ return 0 if $email =~ /[\`\$]/; >+ return 1; >+} >+ >+1; >diff --git a/t/Koha/Validator.t b/t/Koha/Validator.t >new file mode 100755 >index 0000000000..8c4479c772 >--- /dev/null >+++ b/t/Koha/Validator.t >@@ -0,0 +1,58 @@ >+#!/usr/bin/perl >+ >+# Copyright 2023 Rijksmuseum >+# >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Test::More tests => 1; >+use Test::Warn; >+use Try::Tiny; >+ >+use Koha::Validator; >+ >+subtest 'trivial tests' => sub { >+ plan tests => 3; >+ >+ my $rules = { >+ test01 => 'digits_only', >+ test02 => [ 1, 2 ], >+ test03 => sub { 0 }, >+ test04 => 'email', >+ test05 => qr/test/, >+ }; >+ my $validator = Koha::Validator->new( { rules => $rules } ); >+ try { >+ $validator->check_hash( >+ { test01 => 2, test02 => 1, test04 => 'marcel@test.nl', test05 => 'includes test word' } ); >+ ok( 1, 'validator green' ); >+ } catch { >+ ok(0); >+ }; >+ try { >+ $validator->check_hash( { test01 => 2, test02 => 1, test03 => 'nomatterwhat' } ); >+ ok( 0, 'we should not be here' ); >+ } catch { >+ ok( 1, 'validator red' ); >+ }; >+ try { >+ $validator->check_hash( { test04 => 'withbacktic`ls`@test.com' } ); >+ ok( 0, 'we should not be here' ); >+ } catch { >+ ok( 1, 'validator red for email' ); >+ }; >+}; >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 34430
:
154125
|
154768