summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2018-06-17 16:11:50 +0200
committerYorhel <git@yorhel.nl>2018-06-17 16:11:52 +0200
commitcbd2e59cca389bf683d0b2d8664b85a532d9e3fe (patch)
tree7393c2f9abb24f50074a2a121e01dd73ce5cb2f0 /t
parentc7c265d397869be6187f60317d98be745e5f3279 (diff)
Validate::Interop::coerce_for_json(): Add "unknown" config option + inherit that from schema
This allows doing some basic normalization (i.e. removing keys) from the generated JSON.
Diffstat (limited to 't')
-rw-r--r--t/interop.t9
1 files changed, 7 insertions, 2 deletions
diff --git a/t/interop.t b/t/interop.t
index 878de1b..acb3fb0 100644
--- a/t/interop.t
+++ b/t/interop.t
@@ -52,6 +52,7 @@ my @serialized = (
[ { type => 'array', values => {anybool=>1} }, ['a',1,0], '[true,true,false]' ],
[ { type => 'hash' }, {}, '{}' ],
[ { type => 'hash' }, {a=>1,b=>'2'}, '{"a":1,"b":"2"}' ],
+ [ { type => 'hash', keys => {b=>{}} }, {}, '{}' ],
[ { type => 'hash', keys => {a=>{anybool=>1},b=>{int=>1}} }, {a=>1,b=>'10'}, '{"a":true,"b":10}' ],
[ { required => 0 }, undef, 'null' ],
[ { required => 0, jsonbool => 1 }, undef, 'null' ],
@@ -65,14 +66,18 @@ subtest 'JSON::XS coercion', sub {
eval { require JSON::XS; 1 } or plan skip_all => 'JSON::XS not installed';
my @extra = (
[ { type => 'num' }, '10', '10' ],
- [ { type => 'hash', keys => {a=>{anybool=>1},b=>{int=>1}} }, {a=>1,b=>'10',c=>[]}, '{"a":true,"b":10,"c":[]}' ],
+ [ { type => 'hash', keys => {a=>{anybool=>1},b=>{int=>1}} }, {a=>1,b=>'10',c=>[]}, '{"a":true,"b":10}' ],
+ [ { type => 'hash', unknown => 'pass', keys => {a=>{anybool=>1},b=>{int=>1}} }, {a=>1,b=>'10',c=>[]}, '{"a":true,"b":10,"c":[]}' ],
);
+ my $js = JSON::XS->new->canonical->allow_nonref;
for (@serialized, @extra) {
my($schema, $in, $out) = @$_;
my $inc = dclone([$in])->[0];
- is(JSON::XS->new->canonical->allow_nonref->encode(compile({}, $schema)->analyze->coerce_for_json($in)), $out);
+ is($js->encode(compile({}, $schema)->analyze->coerce_for_json($in)), $out);
is_deeply $inc, $in;
}
+ is($js->encode(compile({}, { type => 'hash', keys => {} })->analyze->coerce_for_json({a=>1}, unknown => 'pass')), '{"a":1}');
+ ok !eval { $js->encode(compile({}, { type => 'hash', keys => {} })->analyze->coerce_for_json({a=>1}, unknown => 'reject')); 1 };
};
subtest 'Cpanel::JSON::XS coercion', sub {