All Projects â†’ jhthorsen â†’ Json Validator

jhthorsen / Json Validator

👮 Validate data against a JSON schema

Programming Languages

perl
6916 projects

JSON::Validator

A module for validating data against a JSON Schema.

use Mojolicious::Lite -signatures;
use JSON::Validator 'joi';
use Mojo::JSON qw(false to_json true);

post '/users' => sub ($c) {
  my $user = $c->req->json;

  # Validate input JSON document
  my @errors = joi(
    $user,
    joi->object->props(
      email    => joi->email->required,
      username => joi->string->min(1)->required,
      password => joi->string->min(12)->required,
    )
  );

  # Report back on invalid input
  return $c->render(json => {errors => \@errors}, status => 400) if @errors;

  # Handle the $user in some way
  $c->app->log->info(to_json $user);

  # Report back the status
  return $c->render(json => {accepted => true}, status => 201);
};

app->start;

Installation

All you need is a one-liner, it takes seconds to install.

$ curl -L https://cpanmin.us | perl - -M https://cpan.metacpan.org -n JSON::Validator

We recommend the use of a Perlbrew environment.

Want to know more?

Take a look at our excellent documentation!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].