add IsValid::JSON goodie plugin
This commit is contained in:
parent
1132903c34
commit
72421fc467
24
lib/DDG/Goodie/IsValid/JSON.pm
Normal file
24
lib/DDG/Goodie/IsValid/JSON.pm
Normal 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
22
t/IsValid.t
Executable 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;
|
Loading…
x
Reference in New Issue
Block a user