Validate: Rework API, ->validate() now throws error instead of result object

This is a slight simplification and removes the need to pass around
partially normalized data. I've never found a use for the unsafe_data()
method.
This commit is contained in:
Yorhel 2025-03-05 15:32:01 +01:00
parent 7839e7df78
commit cbebc3a21e
3 changed files with 295 additions and 347 deletions

5
FU.pm
View file

@ -639,8 +639,8 @@ sub _getfield($data, @a) {
return $data->{$a[0]} if @a == 1 && !ref $a[0];
require FU::Validate;
my $schema = FU::Validate->compile(@a > 1 ? { keys => {@a} } : $a[0]);
my $res = $schema->validate($data);
fu->error(400, "Input validation failed") if !$res; # TODO: More detailed error message
my $res = eval { $schema->validate($data) };
fu->error(400, "Input validation failed") if $@; # TODO: More detailed error message
return @a == 2 ? $res->data->{$a[0]} : $res->data;
}
@ -659,7 +659,6 @@ sub formdata {
if (fu->header('content-type')||'') ne 'application/x-www-form-urlencoded';
FU::Util::query_decode($FU::REQ->{data});
} || fu->error(400, $@);
# TODO: Accept schema validation thing.
_getfield $FU::REQ->{formdata}, @_;
}