|
NAMEJSON::Validator - Validate data against a JSON schemaSYNOPSISUsing a schema objectJSON::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.Basicsuse 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), }), ); DESCRIPTIONJSON::Validator is a data structure validation library based around JSON Schema <https://json-schema.org/>. 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 formatsJSON::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.ResourcesHere are some resources that are related to JSON schemas and validation:
Bundled specificationsThis 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
ATTRIBUTEScache_pathsProxy attribute for "cache_paths" in JSON::Validator::Store.formatsThis 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_protectionThis 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. uaProxy attribute for "ua" in JSON::Validator::Store.METHODSbundleThis 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; coerceThis 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 getProxy 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_schemaThis 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.
validateProxy method for "validate" in JSON::Validator::Schema.SEE ALSO
COPYRIGHT AND LICENSECopyright (C) 2014-2021, Jan Henning ThorsenThis program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. AUTHORJan Henning Thorsen - "jhthorsen@cpan.org"Daniel Böhmer - "post@daniel-boehmer.de" Ed J - "mohawk2@users.noreply.github.com" Karen Etheridge - "ether@cpan.org" Kevin Goess - "cpan@goess.org" Martin Renvoize - "martin.renvoize@gmail.com"
Visit the GSP FreeBSD Man Page Interface. |