.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "JSON::Validator 3pm" .TH JSON::Validator 3pm "2023-03-06" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" JSON::Validator \- Validate data against a JSON schema .SH "SYNOPSIS" .IX Header "SYNOPSIS" .SS "Using a schema object" .IX Subsection "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. .SS "Basics" .IX Subsection "Basics" .Vb 2 \& 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; .Ve .SS "Using joi" .IX Subsection "Using joi" .Vb 2 \& # Use joi() to build the schema \& use JSON::Validator::Joi \*(Aqjoi\*(Aq; \& \& $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), \& }), \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" JSON::Validator is a data structure validation library based around \&\s-1JSON\s0 Schema . This module can be used directly with a \s-1JSON\s0 schema or you can use the elegant \s-1DSL\s0 schema-builder JSON::Validator::Joi to define the schema programmatically. .SS "Supported schema formats" .IX Subsection "Supported schema formats" JSON::Validator can load \s-1JSON\s0 schemas in multiple formats: Plain perl data structured (as shown in \*(L"\s-1SYNOPSIS\*(R"\s0), \s-1JSON\s0 or \s-1YAML.\s0 The \s-1JSON\s0 parsing is done with Mojo::JSON, while \s-1YAML\s0 files requires \s-1YAML::PP\s0 or \s-1YAML::XS\s0. .SS "Resources" .IX Subsection "Resources" Here are some resources that are related to \s-1JSON\s0 schemas and validation: .IP "\(bu" 4 .IP "\(bu" 4 .IP "\(bu" 4 .SS "Bundled specifications" .IX Subsection "Bundled specifications" This module comes with some \s-1JSON\s0 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. .PP Files referenced to an \s-1URL\s0 will automatically be cached if the first element in \&\*(L"cache_paths\*(R" is a writable directory. Note that the cache headers for the remote assets are \fBnot\fR honored, so you will manually need to remove any cached file, should you need to refresh them. .PP To download and cache an online asset, do this: .PP .Vb 1 \& JSON_VALIDATOR_CACHE_PATH=/some/writable/directory perl myapp.pl .Ve .PP Here is the list of the bundled specifications: .IP "\(bu" 2 \&\s-1JSON\s0 schema, draft 4, 6, 7, 2019\-09. .Sp Web page: .Sp \&\f(CW$ref\fR: , , . .IP "\(bu" 2 \&\s-1JSON\s0 schema for JSONPatch files .Sp Web page: .Sp \&\f(CW$ref\fR: .IP "\(bu" 2 Swagger / OpenAPI specification, version 2 .Sp Web page: .Sp \&\f(CW$ref\fR: .IP "\(bu" 2 OpenAPI specification, version 3 .Sp Web page: .Sp \&\f(CW$ref\fR: https://spec.openapis.org/oas/3.0/schema/2019\-04\-02 .Sp This specification is still \s-1EXPERIMENTAL.\s0 .IP "\(bu" 2 Swagger Petstore .Sp This is used for unit tests, and should not be relied on by external users. .SS "Optional modules" .IX Subsection "Optional modules" .IP "\(bu" 2 Sereal::Encoder .Sp Installing Sereal::Encoder v4.00 (or later) will make \&\*(L"data_checksum\*(R" in JSON::Validator::Util significantly faster. This function is used both when parsing schemas and validating data. .IP "\(bu" 2 Format validators .Sp See the documentation in JSON::Validator::Formats for other optional modules to do validation of specific \*(L"format\*(R", such as \*(L"hostname\*(R", \*(L"ipv4\*(R" and others. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" .SS "cache_paths" .IX Subsection "cache_paths" Proxy attribute for \*(L"cache_paths\*(R" in JSON::Validator::Store. .SS "formats" .IX Subsection "formats" This attribute will be used as default value for \&\*(L"formats\*(R" in JSON::Validator::Schema. It is highly recommended to change this directly on the \*(L"schema\*(R" instead: .PP .Vb 2 \& $jv\->formats(...); # Legacy \& $jv\->schema\->formats(...); # Recommended way .Ve .SS "recursive_data_protection" .IX Subsection "recursive_data_protection" This attribute will be used as default value for \&\*(L"recursive_data_protection\*(R" in JSON::Validator::Schema. It is highly recommended to change this directly on the \*(L"schema\*(R" instead: .PP .Vb 2 \& $jv\->recursive_data_protection(...); # Legacy \& $jv\->schema\->recursive_data_protection(...); # Recommended way .Ve .SS "store" .IX Subsection "store" .Vb 1 \& $store = $jv\->store; .Ve .PP Holds a JSON::Validator::Store object that caches the retrieved schemas. This object will be shared amongst different \*(L"schema\*(R" objects to prevent a schema from having to be downloaded again. .SS "ua" .IX Subsection "ua" Proxy attribute for \*(L"ua\*(R" in JSON::Validator::Store. .SH "METHODS" .IX Header "METHODS" .SS "bundle" .IX Subsection "bundle" This method can be used to get a bundled version of \*(L"schema\*(R". It will however return a data-structure instead of a new object. See \&\*(L"bundle\*(R" in JSON::Validator::Schema for an alternative. .PP .Vb 3 \& # These two lines does the same \& $data = $jv\->bundle; \& $data = $jv\->schema\->bundle\->data; \& \& # Recommended way \& $schema = $jv\->schema\->bundle; .Ve .SS "coerce" .IX Subsection "coerce" This attribute will be used as default value for \&\*(L"coerce\*(R" in JSON::Validator::Schema. It is highly recommended to change this directly on the \*(L"schema\*(R" instead: .PP .Vb 2 \& $jv\->coerce(...); # Legacy \& $jv\->schema\->coerce(...); # Recommended way .Ve .SS "get" .IX Subsection "get" Proxy method for \*(L"get\*(R" in JSON::Validator::Schema. .SS "new" .IX Subsection "new" .Vb 2 \& $jv = JSON::Validator\->new(%attributes); \& $jv = JSON::Validator\->new(\e%attributes); .Ve .PP Creates a new JSON::Validate object. .SS "load_and_validate_schema" .IX Subsection "load_and_validate_schema" This method will be deprecated in the future. See \&\*(L"errors\*(R" in JSON::Validator::Schema and \*(L"is_invalid\*(R" in JSON::Validator::Schema instead. .SS "schema" .IX Subsection "schema" .Vb 6 \& $jv = $jv\->schema($json_or_yaml_string); \& $jv = $jv\->schema($url); \& $jv = $jv\->schema(\e%schema); \& $jv = $jv\->schema(JSON::Validator::Joi\->new); \& $jv = $jv\->schema(JSON::Validator::Schema\->new); \& $schema = $jv\->schema; .Ve .PP Used to set a schema from either a data structure or a \s-1URL.\s0 .PP \&\f(CW$schema\fR 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. .PP The \f(CW$url\fR can take many forms, but needs to point to a text file in the \&\s-1JSON\s0 or \s-1YAML\s0 format. .IP "\(bu" 4 file://... .Sp A file on disk. Note that it is required to use the \*(L"file\*(R" scheme if you want to reference absolute paths on your file system. .IP "\(bu" 4 http://... or https://... .Sp A web resource will be fetched using the Mojo::UserAgent, stored in \*(L"ua\*(R". .IP "\(bu" 4 data://Some::Module/spec.json .Sp Will load a given \*(L"spec.json\*(R" file from \f(CW\*(C`Some::Module\*(C'\fR using \&\*(L"data_section\*(R" in JSON::Validator::Util. .IP "\(bu" 4 data:///spec.json .Sp A \*(L"data\*(R" \s-1URL\s0 without a module name will use the current package and search up the call/inheritance tree. .IP "\(bu" 4 Any other \s-1URL\s0 .Sp An \s-1URL\s0 (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 \*(L"/\*(R", then the will be loaded from the app defined in \*(L"ua\*(R". Something like this: .Sp .Vb 2 \& $jv\->ua\->server\->app(MyMojoApp\->new); \& $jv\->ua\->get(\*(Aq/any/other/url.json\*(Aq); .Ve .SS "validate" .IX Subsection "validate" Proxy method for \*(L"validate\*(R" in JSON::Validator::Schema. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 2 JSON::Validator::Formats .Sp JSON::Validator::Formats contains utility functions for validating data types. Could be useful for validating data without loading a schema. .IP "\(bu" 2 JSON::Validator::Schema .Sp JSON::Validator::Schema is the base class for JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6 JSON::Validator::Schema::Draft7, JSON::Validator::Schema::Draft201909, JSON::Validator::Schema::OpenAPIv2 or JSON::Validator::Schema::OpenAPIv3. .IP "\(bu" 2 JSON::Validator::Util .Sp JSON::Validator::Util contains many useful function when working with schemas. .IP "\(bu" 2 Mojolicious::Plugin::OpenAPI .Sp Mojolicious::Plugin::OpenAPI is a plugin for Mojolicious that utilize JSON::Validator and the OpenAPI specification to build routes with input and output validation. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2014\-2021, Jan Henning Thorsen .PP This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. .SH "AUTHORS" .IX Header "AUTHORS" .SS "Project Founder" .IX Subsection "Project Founder" Jan Henning Thorsen \- \f(CW\*(C`jhthorsen@cpan.org\*(C'\fR .SS "Contributors" .IX Subsection "Contributors" .IP "\(bu" 2 Aleksandr Orlenko .IP "\(bu" 2 Alexander Hartmaier .IP "\(bu" 2 Alexander Karelas .IP "\(bu" 2 Bernhard Graf .IP "\(bu" 2 Brad Barden .IP "\(bu" 2 Dagfinn Ilmari Mannsåker .IP "\(bu" 2 Daniel Böhmer .IP "\(bu" 2 David Cantrell .IP "\(bu" 2 Ed J .IP "\(bu" 2 Ere Maijala .IP "\(bu" 2 Fabrizio Gennari .IP "\(bu" 2 Ilya Rassadin .IP "\(bu" 2 Jason Cooper .IP "\(bu" 2 Karen Etheridge .IP "\(bu" 2 Kenichi Ishigaki .IP "\(bu" 2 Kevin M. Goess .IP "\(bu" 2 Kirill Matusov .IP "\(bu" 2 Krasimir Berov .IP "\(bu" 2 Lari Taskula .IP "\(bu" 2 Lee Johnson .IP "\(bu" 2 Martin Renvoize .IP "\(bu" 2 Mattias Päivärinta .IP "\(bu" 2 Michael Jemmeson .IP "\(bu" 2 Michael Schout .IP "\(bu" 2 Mohammad S Anwar .IP "\(bu" 2 Nick Morrott .IP "\(bu" 2 Pierre-Aymeric Masse .IP "\(bu" 2 Roy Storey .IP "\(bu" 2 Russell Jenkins .IP "\(bu" 2 Sebastian Riedel .IP "\(bu" 2 Stephan Hradek .IP "\(bu" 2 Tim Stallard .IP "\(bu" 2 Zoffix Znet