add IsValid::JSON goodie plugin
parent
1132903c34
commit
72421fc467
|
@ -0,0 +1,24 @@
|
|||
package DDG::Goodie::IsValid::JSON;
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
use Try::Tiny;
|
||||
use JSON qw(from_json);
|
||||
|
||||
zci answer_type => 'isvalid';
|
||||
zci is_cached => 1;
|
||||
|
||||
triggers start => 'is valid json', 'validate json';
|
||||
|
||||
handle remainder => sub {
|
||||
my $result = try {
|
||||
from_json($_);
|
||||
return 'valid';
|
||||
} catch {
|
||||
return 'invalid';
|
||||
};
|
||||
|
||||
return "Your JSON is $result!";
|
||||
};
|
||||
|
||||
1;
|
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::More;
|
||||
use DDG::Test::Goodie;
|
||||
|
||||
zci answer_type => 'isvalid';
|
||||
zci is_cached => 1;
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(DDG::Goodie::IsValid::JSON)],
|
||||
'is valid json {"test":"lol"}' => test_zci('Your JSON is valid!'),
|
||||
);
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(DDG::Goodie::IsValid::JSON)],
|
||||
'is valid json {"test" "lol"}' => test_zci('Your JSON is invalid!'),
|
||||
);
|
||||
|
||||
done_testing;
|
Loading…
Reference in New Issue