Subject: | Validate params to validate |
validate_one_param in Validate.xs silently ignores checks that are
spelled incorrectly. E.g. 'callback' instead of 'callbacks', 'regexp'
instead of 'regex' etc.
Please find attached a patch that attempts to add such functionality,
along with a test script.
Special thanks to almut at perlmonks.
Subject: | params-validate.patch |
diff --git a/lib/Params/Validate.xs b/lib/Params/Validate.xs
index 396a369..8edcd8e 100644
--- a/lib/Params/Validate.xs
+++ b/lib/Params/Validate.xs
@@ -509,6 +509,31 @@ validate_one_param(SV* value, SV* params, HV* spec, SV* id, HV* options, IV* unt
SV** temp;
IV i;
+ /* Check that hash key is among valid options */
+ static char* valid_keys[] = {
+ "callbacks", "can", "default", "depends", "isa", "optional", "regex", "type"
+ };
+ HE* he;
+ hv_iterinit(spec);
+ while (he = hv_iternext(spec)) {
+ STRLEN len;
+ char* key = HePV(he, len);
+ int ok = 0;
+ int j;
+ for (j=0; j < sizeof(valid_keys)/sizeof(valid_keys[0]); j++) {
+ if (strcmp(key, valid_keys[j]) == 0) {
+ ok = 1;
+ break;
+ }
+ }
+ if (!ok) {
+ SV* buffer = sv_2mortal(newSVpv("'",0));
+ sv_catpv(buffer, key);
+ sv_catpv(buffer, "': unknown option error\n");
+ FAIL(buffer, options);
+ }
+ }
+
/* check type */
if ((temp = hv_fetch(spec, "type", 4, 0))) {
IV type;
diff --git a/t/31-incorrect-spelling.t b/t/31-incorrect-spelling.t
new file mode 100644
index 0000000..8c6b28c
--- /dev/null
+++ b/t/31-incorrect-spelling.t
@@ -0,0 +1,56 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+use Params::Validate qw(validate validate_pos SCALAR);
+use Test::More tests => 3;
+
+{
+ my @p = ( foo => 1, bar => 2 );
+
+ eval {
+ validate(
+ @p, {
+ foo => {
+ type => SCALAR,
+ callbucks => { ### typo
+ 'one' => sub { 1 }
+ },
+ },
+ bar => { type => SCALAR },
+ }
+ );
+ };
+
+ like( $@, qr/unknown option/ );
+
+ eval {
+ validate(
+ @p, {
+ foo => {
+ hype => SCALAR, ### typo
+ callbacks => {
+ 'one' => sub { 1 }
+ },
+ },
+ bar => { type => SCALAR },
+ }
+ );
+ };
+
+ like( $@, qr/unknown option/ );
+ eval {
+ validate(
+ @p, {
+ foo => {
+ type => SCALAR,
+ regexp => qr/^\d+$/, ### typo
+ },
+ bar => { type => SCALAR },
+ }
+ );
+ };
+
+ like( $@, qr/unknown option/ );
+}
+