Skip to content

Latest commit

 

History

History
340 lines (230 loc) · 12.9 KB

README.md

File metadata and controls

340 lines (230 loc) · 12.9 KB

NAME

JSON::Validator - Validate data against a JSON schema

SYNOPSIS

Using a schema object

JSON::Validator::Schema or any of the sub classes can be used instead of JSON::Validator. The only reason to use JSON::Validator directly is if you don't know the schema version up front.

Basics

use JSON::Validator;
my $jv = JSON::Validator->new;

# Define a schema - http://json-schema.org/learn/miscellaneous-examples.html
# You can also load schema from disk or web
$jv->schema({
  type       => "object",
  required   => ["firstName", "lastName"],
  properties => {
    firstName => {type => "string"},
    lastName  => {type => "string"},
    age       => {type => "integer", minimum => 0, description => "Age in years"}
  }
});

# Validate your data
my @errors = $jv->validate({firstName => "Jan Henning", lastName => "Thorsen", age => -42});

# Do something if any errors was found
die "@errors" if @errors;

Using joi

# Use joi() to build the schema
use JSON::Validator::Joi 'joi';

$jv->schema(joi->object->props({
  firstName => joi->string->required,
  lastName  => joi->string->required,
  age       => joi->integer->min(0),
}));

# joi() can also validate directly
my @errors = joi(
  {firstName => "Jan Henning", lastName => "Thorsen", age => -42},
  joi->object->props({
    firstName => joi->string->required,
    lastName  => joi->string->required,
    age       => joi->integer->min(0),
  }),
);

DESCRIPTION

JSON::Validator is a data structure validation library based around JSON Schema. This module can be used directly with a JSON schema or you can use the elegant DSL schema-builder JSON::Validator::Joi to define the schema programmatically.

Supported schema formats

JSON::Validator can load JSON schemas in multiple formats: Plain perl data structured (as shown in "SYNOPSIS"), JSON or YAML. The JSON parsing is done with Mojo::JSON, while YAML files requires YAML::PP or YAML::XS.

Resources

Here are some resources that are related to JSON schemas and validation:

Bundled specifications

This module comes with some JSON specifications bundled, so your application don't have to fetch those from the web. These specifications should be up to date, but please submit an issue if they are not.

Files referenced to an URL will automatically be cached if the first element in "cache_paths" is a writable directory. Note that the cache headers for the remote assets are not honored, so you will manually need to remove any cached file, should you need to refresh them.

To download and cache an online asset, do this:

JSON_VALIDATOR_CACHE_PATH=/some/writable/directory perl myapp.pl

Here is the list of the bundled specifications:

Optional modules

ATTRIBUTES

cache_paths

Proxy attribute for "cache_paths" in JSON::Validator::Store.

formats

This attribute will be used as default value for "formats" in JSON::Validator::Schema. It is highly recommended to change this directly on the "schema" instead:

$jv->formats(...);         # Legacy
$jv->schema->formats(...); # Recommended way

recursive_data_protection

This attribute will be used as default value for "recursive_data_protection" in JSON::Validator::Schema. It is highly recommended to change this directly on the "schema" instead:

$jv->recursive_data_protection(...);         # Legacy
$jv->schema->recursive_data_protection(...); # Recommended way

store

$store = $jv->store;

Holds a JSON::Validator::Store object that caches the retrieved schemas. This object will be shared amongst different "schema" objects to prevent a schema from having to be downloaded again.

ua

Proxy attribute for "ua" in JSON::Validator::Store.

METHODS

bundle

This method can be used to get a bundled version of "schema". It will however return a data-structure instead of a new object. See "bundle" in JSON::Validator::Schema for an alternative.

# These two lines does the same
$data = $jv->bundle;
$data = $jv->schema->bundle->data;

# Recommended way
$schema = $jv->schema->bundle;

coerce

This attribute will be used as default value for "coerce" in JSON::Validator::Schema. It is highly recommended to change this directly on the "schema" instead:

$jv->coerce(...);         # Legacy
$jv->schema->coerce(...); # Recommended way

get

Proxy method for "get" in JSON::Validator::Schema.

new

$jv = JSON::Validator->new(%attributes);
$jv = JSON::Validator->new(\%attributes);

Creates a new JSON::Validate object.

load_and_validate_schema

This method will be deprecated in the future. See "errors" in JSON::Validator::Schema and "is_invalid" in JSON::Validator::Schema instead.

schema

$jv     = $jv->schema($json_or_yaml_string);
$jv     = $jv->schema($url);
$jv     = $jv->schema(\%schema);
$jv     = $jv->schema(JSON::Validator::Joi->new);
$jv     = $jv->schema(JSON::Validator::Schema->new);
$schema = $jv->schema;

Used to set a schema from either a data structure or a URL.

$schema will be an instance of JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6 JSON::Validator::Schema::Draft7, JSON::Validator::Schema::Draft201909, JSON::Validator::Schema::OpenAPIv2, JSON::Validator::Schema::OpenAPIv3 or JSON::Validator::Schema.

The $url can take many forms, but needs to point to a text file in the JSON or YAML format.

  • file://...

    A file on disk. Note that it is required to use the "file" scheme if you want to reference absolute paths on your file system.

  • http://... or https://...

    A web resource will be fetched using the Mojo::UserAgent, stored in "ua".

  • data://Some::Module/spec.json

    Will load a given "spec.json" file from Some::Module using "data_section" in JSON::Validator::Util.

  • data:///spec.json

    A "data" URL without a module name will use the current package and search up the call/inheritance tree.

  • Any other URL

    An URL (without a recognized scheme) will be treated as a path to a file on disk. If the file could not be found on disk and the path starts with "/", then the will be loaded from the app defined in "ua". Something like this:

      $jv->ua->server->app(MyMojoApp->new);
      $jv->ua->get('/any/other/url.json');
    

validate

Proxy method for "validate" in JSON::Validator::Schema.

SEE ALSO

COPYRIGHT AND LICENSE

Copyright (C) 2014-2021, Jan Henning Thorsen

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

AUTHORS

Project Founder

Jan Henning Thorsen - [email protected]

Contributors

  • Aleksandr Orlenko
  • Alexander Hartmaier
  • Alexander Karelas
  • Bernhard Graf
  • Brad Barden
  • Dagfinn Ilmari Mannsåker
  • Daniel Böhmer
  • David Cantrell
  • Ed J
  • Ere Maijala
  • Fabrizio Gennari
  • Ilya Rassadin
  • Jason Cooper
  • Karen Etheridge
  • Kenichi Ishigaki
  • Kevin M. Goess
  • Kirill Matusov
  • Krasimir Berov
  • Lari Taskula
  • Lee Johnson
  • Martin Renvoize
  • Mattias Päivärinta
  • Michael Jemmeson
  • Michael Schout
  • Mohammad S Anwar
  • Nick Morrott
  • Pierre-Aymeric Masse
  • Roy Storey
  • Russell Jenkins
  • Sebastian Riedel
  • Stephan Hradek
  • Tim Stallard
  • Zoffix Znet