add IsValid::JSON goodie plugin

master
Alessandro Ghedini 2012-08-02 20:49:57 +02:00
parent 1132903c34
commit 72421fc467
2 changed files with 46 additions and 0 deletions

View File

@ -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;

22
t/IsValid.t Executable file
View File

@ -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;