Last active
June 21, 2017 08:42
-
-
Save akzhan/3d4889fdcdc6b5e4c0c055133d49e388 to your computer and use it in GitHub Desktop.
use JSON schema to validate params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
validation_schemata: | |
data: | |
type: array | |
minItems: 1 | |
maxItems: 1000 | |
items: | |
type: integer | |
format: int64 | |
minimum: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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