Skip to content

Instantly share code, notes, and snippets.

@akzhan akzhan/Model.pm
Last active Jun 21, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.