1JSON::Validator::SchemaU(s3e)r Contributed Perl DocumentaJtSiOoNn::Validator::Schema(3)
2
3
4
6 JSON::Validator::Schema - Base class for JSON::Validator schemas
7
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
15 JSON::Validator::Schema is the base class for
16 JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6,
17 JSON::Validator::Schema::Draft7 and
18 JSON::Validator::Schema::Draft201909.
19
20 JSON::Validator::Schema is currently EXPERIMENTAL, and most probably
21 will change over the next versions as
22 <https://github.com/mojolicious/json-validator/pull/189> (or a
23 competing PR) evolves.
24
26 errors
27 my $array_ref = $schema->errors;
28
29 Holds the errors after checking "data" against "specification".
30 $array_ref containing no elements means "data" is valid. Each element
31 in the array-ref is a JSON::Validator::Error object.
32
33 This attribute is not changed by "validate". It only reflects if the
34 $schema is valid.
35
36 id
37 my $str = $schema->id;
38 my $schema = $schema->id($str);
39
40 Holds the ID for this schema. Usually extracted from "$id" or "id" in
41 "data".
42
43 moniker
44 $str = $schema->moniker;
45 $schema = $self->moniker("some_name");
46
47 Used to get/set the moniker for the given schema. Will be "draft04" if
48 "specification" points to a JSON Schema draft URL, and fallback to
49 empty string if unable to guess a moniker name.
50
51 This attribute will (probably) detect more monikers from a given
52 "specification" or "/id" in the future.
53
54 specification
55 my $str = $schema->specification;
56 my $schema = $schema->specification($str);
57
58 The URL to the specification used when checking for "errors". Usually
59 extracted from "$schema" or "schema" in "data".
60
62 bundle
63 my $bundled = $schema->bundle;
64
65 $bundled is a new JSON::Validator::Schema object where none of the
66 "$ref" will point to external resources. This can be useful, if you
67 want to have a bunch of files locally, but hand over a single file to a
68 client.
69
70 Mojo::File->new("client.json")
71 ->spurt(Mojo::JSON::to_json($schema->bundle->data));
72
73 coerce
74 my $schema = $schema->coerce("booleans,defaults,numbers,strings");
75 my $schema = $schema->coerce({booleans => 1});
76 my $hash_ref = $schema->coerce;
77
78 Set the given type to coerce. Before enabling coercion this module is
79 very strict when it comes to validating types. Example: The string "1"
80 is not the same as the number 1. Note that it will also change the
81 internal data-structure of the validated data: Example:
82
83 $schema->coerce({numbers => 1});
84 $schema->data({properties => {age => {type => "integer"}}});
85
86 my $input = {age => "42"};
87 $schema->validate($input);
88 # $input->{age} is now an integer 42 and not the string "42"
89
90 contains
91 See "contains" in Mojo::JSON::Pointer.
92
93 data
94 my $hash_ref = $schema->data;
95 my $schema = $schema->data($bool);
96 my $schema = $schema->data($hash_ref);
97 my $schema = $schema->data($url);
98
99 Will set a structure representing the schema. In most cases you want to
100 use "resolve" instead of "data".
101
102 get
103 my $data = $schema->get($json_pointer);
104 my $data = $schema->get($json_pointer, sub { my ($data, $json_pointer) = @_; });
105
106 Called with one argument, this method acts like "get" in
107 Mojo::JSON::Pointer, while if called with two arguments it will work
108 like "schema_extract" in JSON::Validator::Util instead:
109
110 JSON::Validator::Util::schema_extract($schema->data, sub { ... });
111
112 The second argument can be "undef()", if you don't care about the
113 callback.
114
115 See "get" in Mojo::JSON::Pointer.
116
117 new
118 my $schema = JSON::Validator::Schema->new($data);
119 my $schema = JSON::Validator::Schema->new($data, %attributes);
120 my $schema = JSON::Validator::Schema->new(%attributes);
121
122 Construct a new JSON::Validator::Schema object. Passing on $data as the
123 first argument will cause "resolve" to be called, meaning the
124 constructor might throw an exception if the schema could not be
125 successfully resolved.
126
127 resolve
128 $schema = $schema->resolve;
129 $schema = $schema->resolve($data);
130
131 Used to resolve "data" or $data and store the resolved schema in
132 "data". If $data is an $url on contains "$ref" pointing to an URL,
133 then these schemas will be downloaded and resolved as well.
134
135 validate
136 my @errors = $schema->validate($any);
137
138 Will validate $any against the schema defined in "data". Each element
139 in @errors is a JSON::Validator::Error object.
140
142 JSON::Validator.
143
144
145
146perl v5.32.1 2021-01-31 JSON::Validator::Schema(3)