| 1 | # ABSTRACT: JSON schema base class. Used as a dispatcher for loading JSON schema objects used by JsonSQL::Validator. | ||||||||||
| 2 | |||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use 5.014; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | package JsonSQL::Schemas::Schema; | ||||||||||
| 9 | |||||||||||
| 10 | 3 | our $VERSION = '0.4'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use Class::Load qw( try_load_class ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use JSON::Parse qw( parse_json ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 3 | ▶ | use JsonSQL::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | sub new { | ||||||||||
| 20 | 14 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | |||||||||||
| 22 | 14 | my $self = {}; | |||||||||
| 23 | |||||||||||
| 24 | 14 | bless $self, $class; | |||||||||
| 25 | 14 | return $self; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | sub load_schema { | ||||||||||
| 30 | 14 | ▶ | my ( $caller, $jsonSchema ) = @_; | ||||||||
|
Subroutine
load_schema: called CC=3 SCAR=11.5
Pod
documented
| |||||||||||
| 31 | |||||||||||
| 32 | 14 | my $class = "JsonSQL::Schemas::" . $jsonSchema; | |||||||||
| 33 | |||||||||||
| 34 | 14 | my ( $success, $err ) = try_load_class($class); | |||||||||
| 35 | 14 | ▶ | if ( $success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 14 | my $schema = $class->new; | |||||||||
| 37 | |||||||||||
| 38 | # When parsing JSON, need to trap parsing errors. | ||||||||||
| 39 | 14 | my $schemaObj = eval { | |||||||||
| 40 | 14 | return parse_json($schema->{_json}); | |||||||||
| 41 | }; | ||||||||||
| 42 | |||||||||||
| 43 | 14 | ▶ | if ( $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return JsonSQL::Error->new("json_schema", "Schema is invalid JSON at: $@"); | |||||||||
| 45 | } else { | ||||||||||
| 46 | 14 | return $schemaObj; | |||||||||
| 47 | } | ||||||||||
| 48 | } else { | ||||||||||
| 49 | 0 | return JsonSQL::Error->new("json_schema", $err); | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 3 | 1; | |||||||||
| 55 | |||||||||||
| 56 | __END__ | ||||||||||
| 57 | |||||||||||
| 58 | =pod | ||||||||||
| 59 | |||||||||||
| 60 | =encoding UTF-8 | ||||||||||
| 61 | |||||||||||
| 62 | =head1 NAME | ||||||||||
| 63 | |||||||||||
| 64 | JsonSQL::Schemas::Schema - JSON schema base class. Used as a dispatcher for loading JSON schema objects used by JsonSQL::Validator. | ||||||||||
| 65 | |||||||||||
| 66 | =head1 VERSION | ||||||||||
| 67 | |||||||||||
| 68 | version 0.4 | ||||||||||
| 69 | |||||||||||
| 70 | =head1 SYNOPSIS | ||||||||||
| 71 | |||||||||||
| 72 | This is a supporting module used by L<JsonSQL::Validator> for loading JSON schemas. | ||||||||||
| 73 | |||||||||||
| 74 | To use this: | ||||||||||
| 75 | |||||||||||
| 76 | my $schema = JsonSQL::Schemas::Schema->load_schema(<schema_name>); | ||||||||||
| 77 | if ( eval { $schema->is_error } ) { | ||||||||||
| 78 | return "Could not load JSON schema: $schema->{message}"; | ||||||||||
| 79 | } else { | ||||||||||
| 80 | ... | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | <schemaname> must be a module residing in JsonSQL::Schemas that is a subclass of this one. See, for example, | ||||||||||
| 84 | |||||||||||
| 85 | =over | ||||||||||
| 86 | |||||||||||
| 87 | =item * L<JsonSQL::Schemas::select> | ||||||||||
| 88 | |||||||||||
| 89 | =item * L<JsonSQL::Schemas::insert> | ||||||||||
| 90 | |||||||||||
| 91 | =back | ||||||||||
| 92 | |||||||||||
| 93 | If you desire other JSON schemas you can create your own... | ||||||||||
| 94 | |||||||||||
| 95 | =head1 METHODS | ||||||||||
| 96 | |||||||||||
| 97 | =head2 Constructor new -> JsonSQL::Schemas::Schema | ||||||||||
| 98 | |||||||||||
| 99 | An inherited constructor for creating the blessed object reference. This should not be called directly. Instead use load_schema. | ||||||||||
| 100 | |||||||||||
| 101 | =head2 Dispatcher load_schema($jsonSchema) -> JsonSQL::Schemas<schema> | ||||||||||
| 102 | |||||||||||
| 103 | Serves as a dispatcher method to load the appropriate subclass for the specified $jsonSchema. | ||||||||||
| 104 | |||||||||||
| 105 | $jsonSchema => The name of the JSON schema to load. | ||||||||||
| 106 | |||||||||||
| 107 | =head1 AUTHOR | ||||||||||
| 108 | |||||||||||
| 109 | Chris Hoefler <bhoefler@draper.com> | ||||||||||
| 110 | |||||||||||
| 111 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 112 | |||||||||||
| 113 | This software is copyright (c) 2017 by Chris Hoefler. | ||||||||||
| 114 | |||||||||||
| 115 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 116 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 117 | |||||||||||
| 118 | =cut | ||||||||||