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   Basics
10         # Create a new schema from a file on disk
11         # It is also possible to create the object from JSON::Validator::Schema,
12         # but you most likely want to use one of the subclasses.
13         my $schema = JSON::Validator::Schema::Draft7->new('file:///cool/beans.yaml');
14
15         # Validate the schema
16         die $schema->errors->[0] if $schema->is_invalid;
17
18         # Validate data
19         my @errors = $schema->validate({some => 'data'});
20         die $errors[0] if @errors;
21
22   Shared store
23         my $store = JSON::Validator::Store->new;
24         my $schema = JSON::Validator::Schema::Draft7->new(store => $store);
25
26         # Will not fetch the fike from web, if the $store has already retrived
27         # the schema
28         $schema->resolve('https://api.example.com/cool/beans.json');
29
30   Make a new validation class
31         package JSON::Validator::Schema::SomeSchema;
32         use Mojo::Base 'JSON::Validator::Schema';
33         has specification => 'https://api.example.com/my/spec.json#';
34         1;
35

DESCRIPTION

37       JSON::Validator::Schema is the base class for
38       JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6,
39       JSON::Validator::Schema::Draft7, JSON::Validator::Schema::Draft201909,
40       JSON::Validator::Schema::OpenAPIv2 and
41       JSON::Validator::Schema::OpenAPIv3.
42
43       Any of the classes above can be used instead of JSON::Validator if you
44       know which draft/version you are working with up front.
45

ATTRIBUTES

47   errors
48         my $array_ref = $schema->errors;
49
50       Holds the errors after checking "data" against "specification".
51       $array_ref containing no elements means "data" is valid. Each element
52       in the array-ref is a JSON::Validator::Error object.
53
54       This attribute is not changed by "validate". It only reflects if the
55       $schema is valid.
56
57   id
58         my $str    = $schema->id;
59         my $schema = $schema->id($str);
60
61       Holds the ID for this schema. Usually extracted from "$id" or "id" in
62       "data".
63
64   moniker
65         $str    = $schema->moniker;
66         $schema = $self->moniker("some_name");
67
68       Used to get/set the moniker for the given schema. Will be "draft04" if
69       "specification" points to a JSON Schema draft URL, and fallback to
70       empty string if unable to guess a moniker name.
71
72       This attribute will (probably) detect more monikers from a given
73       "specification" or "/id" in the future.
74
75   specification
76         my $str    = $schema->specification;
77         my $schema = $schema->specification($str);
78
79       The URL to the specification used when checking for "errors". Usually
80       extracted from "$schema" or "schema" in "data".
81
82   store
83         $store = $jv->store;
84
85       Holds a JSON::Validator::Store object that caches the retrieved
86       schemas.  This object can be shared amongst different schema objects to
87       prevent a schema from having to be downloaded again.
88

METHODS

90   bundle
91         my $bundled = $schema->bundle;
92
93       $bundled is a new JSON::Validator::Schema object where none of the
94       "$ref" will point to external resources. This can be useful, if you
95       want to have a bunch of files locally, but hand over a single file to a
96       client.
97
98         Mojo::File->new("client.json")
99           ->spurt(Mojo::JSON::to_json($schema->bundle->data));
100
101   coerce
102         my $schema   = $schema->coerce("booleans,defaults,numbers,strings");
103         my $schema   = $schema->coerce({booleans => 1});
104         my $hash_ref = $schema->coerce;
105
106       Set the given type to coerce. Before enabling coercion this module is
107       very strict when it comes to validating types. Example: The string "1"
108       is not the same as the number 1. Note that it will also change the
109       internal data-structure of the validated data: Example:
110
111         $schema->coerce({numbers => 1});
112         $schema->data({properties => {age => {type => "integer"}}});
113
114         my $input = {age => "42"};
115         $schema->validate($input);
116         # $input->{age} is now an integer 42 and not the string "42"
117
118   contains
119       See "contains" in Mojo::JSON::Pointer.
120
121   data
122         my $hash_ref = $schema->data;
123         my $schema   = $schema->data($bool);
124         my $schema   = $schema->data($hash_ref);
125         my $schema   = $schema->data($url);
126
127       Will set a structure representing the schema. In most cases you want to
128       use "resolve" instead of "data".
129
130   get
131         my $data = $schema->get($json_pointer);
132         my $data = $schema->get($json_pointer, sub { my ($data, $json_pointer) = @_; });
133
134       Called with one argument, this method acts like "get" in
135       Mojo::JSON::Pointer, while if called with two arguments it will work
136       like "schema_extract" in JSON::Validator::Util instead:
137
138         JSON::Validator::Util::schema_extract($schema->data, sub { ... });
139
140       The second argument can be "undef()", if you don't care about the
141       callback.
142
143       See "get" in Mojo::JSON::Pointer.
144
145   is_invalid
146         my $bool = $schema->is_invalid;
147
148       Returns true if the schema in "data" is invalid. Internally this method
149       calls "errors" which will validate "data" agains "specification".
150
151   load_and_validate_schema
152       This method will be removed in a future release.
153
154   new
155         my $schema = JSON::Validator::Schema->new($data);
156         my $schema = JSON::Validator::Schema->new($data, %attributes);
157         my $schema = JSON::Validator::Schema->new(%attributes);
158
159       Construct a new JSON::Validator::Schema object. Passing on $data as the
160       first argument will cause "resolve" to be called, meaning the
161       constructor might throw an exception if the schema could not be
162       successfully resolved.
163
164   resolve
165         $schema = $schema->resolve;
166         $schema = $schema->resolve($data);
167
168       Used to resolve "data" or $data and store the resolved schema in
169       "data".  If $data is an $url on contains "$ref" pointing to an URL,
170       then these schemas will be downloaded and resolved as well.
171
172   schema
173       This method will be removed in a future release.
174
175   validate
176         my @errors = $schema->validate($any);
177
178       Will validate $any against the schema defined in "data". Each element
179       in @errors is a JSON::Validator::Error object.
180

SEE ALSO

182       JSON::Validator.
183
184
185
186perl v5.34.0                      2021-07-22        JSON::Validator::Schema(3)
Impressum