blib/lib/JSON/Structure.pm

filter: Statementstmt 83.3% 30 / 36 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 83.6% 41 / 49 CC 1 SCAR 1.7
CC
1
Cov
83%
validate_schema
2.0
validate_instance
2.0
BEGIN
1.0
CRAP 1.2
« lib/JSON/Structure/Types.pm ↑ Summary lib/JSON/Structure/ErrorCodes.pm »
1 package JSON::Structure;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 our $VERSION = '0.8.2';
8  
9 12 ▶use JSON::Structure::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use JSON::Structure::ErrorCodes qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use JSON::Structure::JsonSourceLocator;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use JSON::Structure::SchemaValidator;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use JSON::Structure::InstanceValidator;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 our @EXPORT_OK = qw(
18 validate_schema
19 validate_instance
20 SchemaValidator
21 InstanceValidator
22 JsonSourceLocator
23 );
24  
25 12 our %EXPORT_TAGS = ( all => \@EXPORT_OK, );
26  
27 =head1 NAME
28  
29 JSON::Structure - JSON Structure validation library for Perl
30  
31 =head1 SYNOPSIS
32  
33 use JSON::Structure qw(:all);
34 use JSON::MaybeXS;
35
36 # Validate a schema
37 my $schema = decode_json($schema_json);
38 my $validator = JSON::Structure::SchemaValidator->new();
39 my $result = $validator->validate($schema, $schema_json);
40
41 if ($result->is_valid) {
42 say "Schema is valid!";
43 } else {
44 for my $error (@{$result->errors}) {
45 say $error->to_string;
46 }
47 }
48
49 # Validate an instance against a schema
50 my $instance_validator = JSON::Structure::InstanceValidator->new(schema => $schema);
51 my $instance = decode_json($instance_json);
52 my $result = $instance_validator->validate($instance, $instance_json);
53  
54 =head1 DESCRIPTION
55  
56 This module provides validators for JSON Structure schemas and instances,
57 conforming to the JSON Structure Core specification.
58  
59 JSON Structure is a type-oriented schema language for JSON, designed for
60 defining data structures that can be validated and mapped to programming
61 language types.
62  
63 =head1 FUNCTIONS
64  
65 =head2 validate_schema($schema, $source_text)
66  
67 Convenience function to validate a JSON Structure schema.
68  
69 my $result = validate_schema($schema, $schema_json);
70  
71 =head2 validate_instance($instance, $schema, $source_text)
72  
73 Convenience function to validate a JSON instance against a schema.
74  
75 my $result = validate_instance($instance, $schema, $instance_json);
76  
77 =head1 SEE ALSO
78  
79 =over 4
80  
81 =item * L<JSON::Structure::SchemaValidator>
82  
83 =item * L<JSON::Structure::InstanceValidator>
84  
85 =item * L<JSON::Structure::Types>
86  
87 =item * L<https://json-structure.org/>
88  
89 =back
90  
91 =head1 AUTHOR
92  
93 JSON Structure Project
94  
95 =head1 LICENSE
96  
97 MIT License
98  
99 =cut
100  
101 0 ▶sub validate_schema {
Subroutine validate_schema: not called CC=1 SCAR=6.9
Pod documented
102 0 my ( $schema, $source_text ) = @_;
103 0 my $validator = JSON::Structure::SchemaValidator->new();
104 0 return $validator->validate( $schema, $source_text );
105 }
106  
107 0 ▶sub validate_instance {
Subroutine validate_instance: not called CC=1 SCAR=6.9
Pod documented
108 0 my ( $instance, $schema, $source_text ) = @_;
109 0 my $validator =
110 JSON::Structure::InstanceValidator->new( schema => $schema );
111 0 return $validator->validate( $instance, $source_text );
112 }
113  
114 12 1;
« lib/JSON/Structure/Types.pm ↑ Summary lib/JSON/Structure/ErrorCodes.pm »