1JSON::Validator::Util(3U)ser Contributed Perl DocumentatiJoSnON::Validator::Util(3)
2
3
4
6 JSON::Validator::Util - Utility functions for JSON::Validator
7
9 JSON::Validator::Util is a package containing utility functions for
10 JSON::Validator. Each of the "FUNCTIONS" can be imported.
11
13 data_checksum
14 $str = data_checksum $any;
15
16 Will create a checksum for any data structure stored in $any.
17
18 data_section
19 $str = data_section "Some::Module", "file.json";
20 $str = data_section "Some::Module", "file.json", {encode => 'UTF-8'};
21
22 Same as "data_section" in Mojo::Loader, but will also look up the file
23 in any inherited class.
24
25 data_type
26 $str = data_type $any;
27 $str = data_type $any, [@schemas];
28 $str = data_type $any, [{type => "integer", ...}];
29
30 Returns the JSON type for $any. $str can be array, boolean, integer,
31 null, number object or string. Note that a list of schemas need to be
32 provided to differentiate between "integer" and "number".
33
34 is_type
35 $bool = is_type $any, $class;
36 $bool = is_type $any, $type; # $type = "ARRAY", "BOOL", "HASH", "NUM" ...
37
38 Checks if $any is a, or inherits from, $class or $type. Two special
39 types can be checked:
40
41 • BOOL
42
43 Checks if $any is a boolean value. $any is considered boolean if it
44 is an object inheriting from JSON::PP::Boolean or is another object
45 that stringifies to "1" or "0".
46
47 • NUM
48
49 Checks if $any is indeed a number.
50
51 json_pointer
52 $str = json_pointer $path, $append;
53
54 Will concat $append on to $path, but will also escape the two special
55 characters "~" and "/" in $append.
56
57 negotiate_content_type
58 $content_type = negotiate_content_type($header, \@content_types);
59
60 This method can take a "Content-Type" or "Accept" header and find the
61 closest matching content type in @content_types. @content_types can
62 contain wildcards, meaning "*/*" will match anything.
63
64 prefix_errors
65 @errors = prefix_errors $prefix, @errors;
66
67 Consider this internal for now.
68
69 schema_extract
70 $data = schema_extract $any, $json_pointer;
71 $data = schema_extract $any, "/x/cool_beans/y";
72 $collection = schema_extract $any, ["x", undef, "y"];
73 schema_extract $any, $json_pointer, sub { my ($data, $json_pointer) = @_ };
74
75 The basic usage is to extract data from $any, using a $json_pointer -
76 RFC 6901 <http://tools.ietf.org/html/rfc6901>. It can however be used
77 in a more complex way by passing in an array-ref, instead of a plain
78 string. The array-ref can contain "undef()" values, will result in
79 extracting any element on that point, regardsless of value. In that
80 case a Mojo::Collection will be returned.
81
82 A callback can also be given. This callback will be called each time
83 the $json_pointer matches some data, and will pass in the $json_pointer
84 at that place.
85
86 In addition, if the $json_pointer points to a JSON::Validator::Ref at
87 any point, the "$ref" will be followed, while if you used
88 Mojo::JSON::Pointer, it would return either the JSON::Validator::Ref or
89 "undef()".
90
91 Even though "schema_extract" has special capabilities for handling a
92 JSON-Schema, it can be used for any data-structure, just like
93 Mojo::JSON::Pointer.
94
95 schema_type
96 $str = schema_type $hash_ref;
97 $str = schema_type $hash_ref, $any;
98
99 Looks at $hash_ref and tries to figure out what kind of type the schema
100 represents. $str can be "array", "const", "number", "object", "string",
101 or fallback to empty string if the correct type could not be figured
102 out.
103
104 $any can be provided to double check the type, so if $hash_ref
105 describes an "object", but $any is an array-ref, then $str will become
106 an empty string. Example:
107
108 # $str = "";
109 $str = schema {additionalProperties => false}, [];
110
111 # $str = "object"
112 $str = schema {additionalProperties => false};
113 $str = schema {additionalProperties => false}, {};
114
115 Note that this process is relatively slow, so it will make your
116 validation faster if you specify "type". Both of the two below is
117 valid, but the one with "type" will be faster.
118
119 {"type": "object", "properties": {}} # Faster
120 {"properties": {}} # Slower
121
123 JSON::Validator.
124
125
126
127perl v5.32.1 2021-01-31 JSON::Validator::Util(3)