Validate: Add empty() and coerce() methods
Implementing the undocumented coerce_for_json() method from TUWF and elm_empty() from VNDB.
This commit is contained in:
parent
efa63ca96a
commit
8b807e6dcf
2 changed files with 131 additions and 5 deletions
36
t/validate-util.t
Normal file
36
t/validate-util.t
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
use v5.36;
|
||||
use Test::More;
|
||||
use FU::Validate;
|
||||
use FU::Util 'json_format';
|
||||
|
||||
my $schema = FU::Validate->compile({ keys => {
|
||||
bool => { anybool => 1 },
|
||||
num => { num => 1 },
|
||||
int => { int => 1 },
|
||||
str => { default => 'x' },
|
||||
intarray => { elems => { int => 1 } },
|
||||
any => { type => 'any' },
|
||||
}});
|
||||
|
||||
|
||||
is json_format($schema->coerce(undef)), 'null';
|
||||
is json_format($schema->coerce("str")), '"str"';
|
||||
|
||||
is json_format($schema->coerce({
|
||||
bool => 'abc',
|
||||
num => " 1.5 ",
|
||||
int => 9.7,
|
||||
str => !1,
|
||||
intarray => [ 1.5, -10, undef, ' 0E0 ' ],
|
||||
any => {},
|
||||
whatsthis => undef,
|
||||
}, unknown => 'remove'), canonical => 1),
|
||||
'{"any":{},"bool":true,"int":9,"intarray":[1,-10,null,0],"num":1.5,"str":""}';
|
||||
|
||||
is json_format($schema->coerce({uhm => 1}), canonical => 1),
|
||||
'{"any":null,"bool":false,"int":0,"intarray":[],"num":0,"str":"x","uhm":1}';
|
||||
|
||||
is json_format($schema->empty, canonical => 1),
|
||||
'{"any":null,"bool":false,"int":0,"intarray":[],"num":0,"str":"x"}';
|
||||
|
||||
done_testing;
|
||||
Loading…
Add table
Add a link
Reference in a new issue