1JSON::Validator::SchemaU(s3e)r Contributed Perl DocumentaJtSiOoNn::Validator::Schema(3)
2
3
4

NAME

6       JSON::Validator::Schema - Base class for JSON::Validator schemas
7

SYNOPSIS

9         package JSON::Validator::Schema::SomeSchema;
10         use Mojo::Base "JSON::Validator::Schema";
11         has specification => "https://api.example.com/my/spec.json#";
12         1;
13

DESCRIPTION

15       JSON::Validator::Schema is the base class for
16       JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6 and
17       JSON::Validator::Schema::Draft7.
18
19       JSON::Validator::Schema is currently EXPERIMENTAL, and most probably
20       will change over the next versions as
21       <https://github.com/mojolicious/json-validator/pull/189> (or a
22       competing PR) evolves.
23

ATTRIBUTES

25   errors
26         my $array_ref = $schema->errors;
27
28       Holds the errors after checking "data" against "specification".
29       $array_ref containing no elements means "data" is valid. Each element
30       in the array-ref is a JSON::Validator::Error object.
31
32       This attribute is not changed by "validate". It only reflects if the
33       $schema is valid.
34
35   id
36         my $str    = $schema->id;
37         my $schema = $schema->id($str);
38
39       Holds the ID for this schema. Usually extracted from "$id" or "id" in
40       "data".
41
42   moniker
43         $str    = $schema->moniker;
44         $schema = $self->moniker("some_name");
45
46       Used to get/set the moniker for the given schema. Will be "draft04" if
47       "specification" points to a JSON Schema draft URL, and fallback to
48       empty string if unable to guess a moniker name.
49
50       This attribute will (probably) detect more monikers from a given
51       "specification" or "/id" in the future.
52
53   specification
54         my $str    = $schema->specification;
55         my $schema = $schema->specification($str);
56
57       The URL to the specification used when checking for "errors". Usually
58       extracted from "$schema" or "schema" in "data".
59

METHODS

61   bundle
62         my $bundled = $schema->bundle;
63
64       $bundled is a new JSON::Validator::Schema object where none of the
65       "$ref" will point to external resources. This can be useful, if you
66       want to have a bunch of files locally, but hand over a single file to a
67       client.
68
69         Mojo::File->new("client.json")
70           ->spurt(Mojo::JSON::to_json($schema->bundle->data));
71
72   coerce
73         my $schema   = $schema->coerce("booleans,defaults,numbers,strings");
74         my $schema   = $schema->coerce({booleans => 1});
75         my $hash_ref = $schema->coerce;
76
77       Set the given type to coerce. Before enabling coercion this module is
78       very strict when it comes to validating types. Example: The string "1"
79       is not the same as the number 1. Note that it will also change the
80       internal data-structure of the validated data: Example:
81
82         $schema->coerce({numbers => 1});
83         $schema->data({properties => {age => {type => "integer"}}});
84
85         my $input = {age => "42"};
86         $schema->validate($input);
87         # $input->{age} is now an integer 42 and not the string "42"
88
89   contains
90       See "contains" in Mojo::JSON::Pointer.
91
92   data
93         my $hash_ref = $schema->data;
94         my $schema   = $schema->data($bool);
95         my $schema   = $schema->data($hash_ref);
96         my $schema   = $schema->data($url);
97
98       Will set a structure representing the schema. In most cases you want to
99       use "resolve" instead of "data".
100
101   get
102         my $data = $schema->get($json_pointer);
103         my $data = $schema->get($json_pointer, sub { my ($data, $json_pointer) = @_; });
104
105       Called with one argument, this method acts like "get" in
106       Mojo::JSON::Pointer, while if called with two arguments it will work
107       like "schema_extract" in JSON::Validator::Util instead:
108
109         JSON::Validator::Util::schema_extract($schema->data, sub { ... });
110
111       The second argument can be "undef()", if you don't care about the
112       callback.
113
114       See "get" in Mojo::JSON::Pointer.
115
116   new
117         my $schema = JSON::Validator::Schema->new($data);
118         my $schema = JSON::Validator::Schema->new($data, %attributes);
119         my $schema = JSON::Validator::Schema->new(%attributes);
120
121       Construct a new JSON::Validator::Schema object. Passing on $data as the
122       first argument will cause "resolve" to be called, meaning the
123       constructor might throw an exception if the schema could not be
124       successfully resolved.
125
126   resolve
127         $schema = $schema->resolve;
128         $schema = $schema->resolve($data);
129
130       Used to resolve "data" or $data and store the resolved schema in
131       "data".  If $data is an $url on contains "$ref" pointing to an URL,
132       then these schemas will be downloaded and resolved as well.
133
134   validate
135         my @errors = $schema->validate($any);
136
137       Will validate $any against the schema defined in "data". Each element
138       in @errors is a JSON::Validator::Error object.
139

SEE ALSO

141       JSON::Validator.
142
143
144
145perl v5.32.0                      2020-07-28        JSON::Validator::Schema(3)
Impressum