Skip to content

Instantly share code, notes, and snippets.

@akzhan
Last active June 21, 2017 08:42
Show Gist options
  • Save akzhan/3d4889fdcdc6b5e4c0c055133d49e388 to your computer and use it in GitHub Desktop.
Save akzhan/3d4889fdcdc6b5e4c0c055133d49e388 to your computer and use it in GitHub Desktop.
use JSON schema to validate params
validation_schemata:
data:
type: array
minItems: 1
maxItems: 1000
items:
type: integer
format: int64
minimum: 1
sub update {
my $self = shift;
my $data = $self->req->json;
return $self->render(
json => {
success => \0,
error => "No data provided",
}
) unless $data;
my @validation_errors = data_validator()->validate($data);
return $self->render(
json => {
success => \0,
error => "@validation_errors",
}
) if scalar @validation_errors;
# ...
}
sub data_validator {
state $validator = (sub {
my $instance = JSON::Validator->new;
$instance->schema( YourCompany::Config->validation_schemata->{data} );
$instance;
})->();
$validator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment