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 prefix_errors
58 @errors = prefix_errors $prefix, @errors;
59
60 Consider this internal for now.
61
62 schema_extract
63 $data = schema_extract $any, $json_pointer;
64 $data = schema_extract $any, "/x/cool_beans/y";
65 $collection = schema_extract $any, ["x", undef, "y"];
66 schema_extract $any, $json_pointer, sub { my ($data, $json_pointer) = @_ };
67
68 The basic usage is to extract data from $any, using a $json_pointer -
69 RFC 6901 <http://tools.ietf.org/html/rfc6901>. It can however be used
70 in a more complex way by passing in an array-ref, instead of a plain
71 string. The array-ref can contain "undef()" values, will result in
72 extracting any element on that point, regardsless of value. In that
73 case a Mojo::Collection will be returned.
74
75 A callback can also be given. This callback will be called each time
76 the $json_pointer matches some data, and will pass in the $json_pointer
77 at that place.
78
79 In addition, if the $json_pointer points to a JSON::Validator::Ref at
80 any point, the "$ref" will be followed, while if you used
81 Mojo::JSON::Pointer, it would return either the JSON::Validator::Ref or
82 "undef()".
83
84 Even though "schema_extract" has special capabilities for handling a
85 JSON-Schema, it can be used for any data-structure, just like
86 Mojo::JSON::Pointer.
87
88 schema_type
89 $str = schema_type $hash_ref;
90 $str = schema_type $hash_ref, $any;
91
92 Looks at $hash_ref and tries to figure out what kind of type the schema
93 represents. $str can be "array", "const", "number", "object", "string",
94 or fallback to empty string if the correct type could not be figured
95 out.
96
97 $any can be provided to double check the type, so if $hash_ref
98 describes an "object", but $any is an array-ref, then $str will become
99 an empty string. Example:
100
101 # $str = "";
102 $str = schema {additionalProperties => false}, [];
103
104 # $str = "object"
105 $str = schema {additionalProperties => false};
106 $str = schema {additionalProperties => false}, {};
107
108 Note that this process is relatively slow, so it will make your
109 validation faster if you specify "type". Both of the two below is
110 valid, but the one with "type" will be faster.
111
112 {"type": "object", "properties": {}} # Faster
113 {"properties": {}} # Slower
114
116 JSON::Validator.
117
118
119
120perl v5.32.0 2020-07-28 JSON::Validator::Util(3)