# Pastebin vvaVRqKo package Mojolicious::Plugin::JSON::Validator; use Mojo::Base 'Mojolicious::Plugin'; use Mojo::Loader; use JSON::Validator; our $VERSION = '0.01'; sub register { my ($self, $app) = @_; $app->hook(around_action => sub { my ($next, $c, $action, $last) = @_; my $route = $c->match->endpoint; my $schema; if($route->to->{"json_validator.schema"}) { $schema = $route->to->{"json_validator.schema"}; } else { $schema = $app->home->rel_file(sprintf "%s.schema.json", $route->name); } print $schema, $/; if(ref $schema or $schema =~ m{^\w+://} or -f $schema) { my $validator = JSON::Validator->new->schema($schema); if(my @errors = $validator->validate($c->req->json)) { $c->render(status_code => 400, json => {errors => [@errors]}); return } } $next->() }); } 1;