diff options
author | Yorhel <git@yorhel.nl> | 2015-09-20 16:14:59 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2015-09-20 16:14:59 +0200 |
commit | ed86cfd12b0bed7352e2be525b8e63cb4d6d5448 (patch) | |
tree | 321f346791cbff80c8355a736e6f4ecb90084d49 /lib/VNDB/Func.pm | |
parent | bb7a7cc178144457d401c3c6479a41788cf681dd (diff) |
formValidate: Add json template and remove json_validate() function
This is less convenient than I had expected, because all the form
handling code is designed to work with plain strings rather than any
scalar. This means the json data has to be encoded again to get into
$frm (not doing this means that, if the form didn't validate, the field
won't be filled out correctly). And then decoded for validation, and
then encoded again for comparison.
I suspect the better solution is to fix the form handling code to handle
arbitrary data structures: comparison can be done by deep comparison
rather than a simple string compare, and the form generator can
auto-encode-to-json if it sees a complex object.
Another advantage of this solution is that the comparison function can
be less strict with respect to number formatting. In the current scheme
you have to be very careful that numbers are not automatically coerced
into string format, otherwise the comparison will fail.
Either way, that's an idea for the future...
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r-- | lib/VNDB/Func.pm | 28 |
1 files changed, 1 insertions, 27 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm index 40af8e73..02489980 100644 --- a/lib/VNDB/Func.pm +++ b/lib/VNDB/Func.pm @@ -11,7 +11,7 @@ use VNDBUtil; our @EXPORT = (@VNDBUtil::EXPORT, qw| clearfloat cssicon tagscore mt minage fil_parse fil_serialize parenttags childtags charspoil imgpath imgurl fmtvote - json_encode json_decode script_json json_validate + json_encode json_decode script_json mtvoiced mtani mtvnlen mtrlstat mtvnlstat mtbloodt |); @@ -228,32 +228,6 @@ sub script_json { } -# Special validation function for simple JSON structures as form fields. It can -# only validate arrays of key-value objects. The key-value objects are then -# validated using kv_validate. -# Returns the parsed json object on success, undef on error and sets $frm->{_err}. -# Doesn't provide a user-friendly error message if validation fails. It's the -# responsibility of the JS code to handle the interface with the user. -sub json_validate { - my($frm, $name, @fields) = @_; - my $data = eval { json_decode $frm->{$name} }; - goto error if $@ || ref $data ne 'ARRAY'; - my %known_fields = map +($_->{field},1), @fields; - for my $i (0..$#$data) { - goto error if ref $data->[$i] ne 'HASH'; - # Require that all keys are known and have a scalar value. - goto error if grep !$known_fields{$_} || ref($data->[$i]{$_}), keys %{$data->[$i]}; - $data->[$i] = kv_validate({ field => sub { $data->[$i]{shift()} } }, $TUWF::OBJ->{_TUWF}{validate_templates}, \@fields); - goto error if $data->[$i]{_err}; - } - - return $data; -error: - push @{$frm->{_err}}, [ 'aliases', 'template', 'json' ] ; - return undef; -} - - # mt() wrappers for data-dependent translation strings that have a special # value for 'unknown'. sub mtvoiced { !$_[0] ? mt '_unknown' : mt '_voiced_'.$_[0]; } |