| 1 | package JSON::Schema::AsType::Draft3::Types; | ||||||||||
| 2 | 7 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | # ABSTRACT: JSON-schema v3 keywords as types | ||||||||||
| 4 | 7 | $JSON::Schema::AsType::Draft3::Types::VERSION = '0.4.2'; | |||||||||
| 5 | |||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 7 | ▶ | use Type::Utils -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 7 | use Types::Standard qw/ | |||||||||
| 11 | InstanceOf | ||||||||||
| 12 | Str StrictNum HashRef ArrayRef | ||||||||||
| 13 | Int | ||||||||||
| 14 | Dict slurpy Optional Any | ||||||||||
| 15 | Tuple | ||||||||||
| 16 | 7 | ▶ | /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | use Type::Library | ||||||||||
| 19 | 7 | -base, | |||||||||
| 20 | -declare => qw( | ||||||||||
| 21 | Disallow | ||||||||||
| 22 | Extends | ||||||||||
| 23 | DivisibleBy | ||||||||||
| 24 | |||||||||||
| 25 | Properties | ||||||||||
| 26 | |||||||||||
| 27 | Dependencies Dependency | ||||||||||
| 28 | |||||||||||
| 29 | Schema | ||||||||||
| 30 | 7 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 7 | ▶ | use List::MoreUtils qw/ all any zip none /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 7 | ▶ | use List::Util qw/ pairs pairmap reduce uniq /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 7 | ▶ | use JSON qw/ to_json from_json /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 7 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 7 | use JSON::Schema::AsType::Draft4::Types 'Not', 'Integer', 'MultipleOf', | |||||||||
| 40 | 7 | ▶ | 'Boolean', 'Number', 'String', 'Null', 'Object', 'Array', 'LaxNumber', 'LaxInteger', 'LaxString'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 7 | __PACKAGE__->meta->add_type( $_ ) for Integer, Boolean, Number, String, Null, Object, Array, LaxNumber, LaxInteger, LaxString; | |||||||||
| 43 | |||||||||||
| 44 | declare Dependencies, | ||||||||||
| 45 | constraint_generator => sub { | ||||||||||
| 46 | 8 | ▶ | my %deps = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | |||||||||||
| 48 | 8 | ▶ | return reduce { $a & $b } pairmap { Dependency[$a => $b] } %deps; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 7 | }; | |||||||||
| 50 | |||||||||||
| 51 | declare Dependency, | ||||||||||
| 52 | constraint_generator => sub { | ||||||||||
| 53 | 13 | ▶ | my( $property, $dep) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 54 | |||||||||||
| 55 | sub { | ||||||||||
| 56 | 30 | ▶ | return 1 unless Object->check($_); | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||
| 57 | 29 | ▶ | return 1 unless exists $_->{$property}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 10 | my $obj = $_; | |||||||||
| 60 | |||||||||||
| 61 | 10 | ▶ | return all { exists $obj->{$_} } @$dep if ref $dep eq 'ARRAY'; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 62 | 6 | ▶ | return exists $obj->{$dep} unless ref $dep; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 4 | return $dep->check($_); | |||||||||
| 65 | } | ||||||||||
| 66 | 13 | }; | |||||||||
| 67 | |||||||||||
| 68 | declare Properties => | ||||||||||
| 69 | constraint_generator => sub { | ||||||||||
| 70 | 29 | ▶ | my $type = Dict[@_, slurpy Any]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | sub { | ||||||||||
| 73 | 83 | ▶ | ! Object->check($_) or $type->check($_) | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 74 | } | ||||||||||
| 75 | 29 | }; | |||||||||
| 76 | |||||||||||
| 77 | declare Disallow => | ||||||||||
| 78 | constraint_generator => sub { | ||||||||||
| 79 | 4 | ▶ | Not[ shift ]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 7 | }; | |||||||||
| 81 | |||||||||||
| 82 | declare Extends => | ||||||||||
| 83 | constraint_generator => sub { | ||||||||||
| 84 | 1 | ▶ | reduce { $a & $b } @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 7 | }; | |||||||||
| 86 | |||||||||||
| 87 | declare DivisibleBy => | ||||||||||
| 88 | constraint_generator => sub { | ||||||||||
| 89 | 3 | ▶ | MultipleOf[shift]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 7 | }; | |||||||||
| 91 | |||||||||||
| 92 | 7 | declare Schema, as InstanceOf['Type::Tiny']; | |||||||||
| 93 | |||||||||||
| 94 | coerce Schema, | ||||||||||
| 95 | from HashRef, | ||||||||||
| 96 | via { | ||||||||||
| 97 | 1 | ▶ | my $schema = JSON::Schema::AsType->new( draft_version => 3, schema => $_ ); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | if ( $schema->validate_schema ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | die "not a valid draft3 json schema\n"; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | $schema->type | ||||||||||
| 104 | 1 | }; | |||||||||
| 105 | |||||||||||
| 106 | 7 | 1; | |||||||||
| 107 | |||||||||||
| 108 | __END__ | ||||||||||
| 109 | |||||||||||
| 110 | =pod | ||||||||||
| 111 | |||||||||||
| 112 | =encoding UTF-8 | ||||||||||
| 113 | |||||||||||
| 114 | =head1 NAME | ||||||||||
| 115 | |||||||||||
| 116 | JSON::Schema::AsType::Draft3::Types - JSON-schema v3 keywords as types | ||||||||||
| 117 | |||||||||||
| 118 | =head1 VERSION | ||||||||||
| 119 | |||||||||||
| 120 | version 0.4.2 | ||||||||||
| 121 | |||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||
| 123 | |||||||||||
| 124 | use JSON::Schema::AsType::Draft3::Types '-all'; | ||||||||||
| 125 | |||||||||||
| 126 | my $type = Object & | ||||||||||
| 127 | Properties[ | ||||||||||
| 128 | foo => Minimum[3] | ||||||||||
| 129 | ]; | ||||||||||
| 130 | |||||||||||
| 131 | $type->check({ foo => 5 }); # => 1 | ||||||||||
| 132 | $type->check({ foo => 1 }); # => 0 | ||||||||||
| 133 | |||||||||||
| 134 | =head1 EXPORTED TYPES | ||||||||||
| 135 | |||||||||||
| 136 | Null Boolean Array Object String Integer Pattern Number Enum | ||||||||||
| 137 | |||||||||||
| 138 | OneOf AllOf AnyOf | ||||||||||
| 139 | |||||||||||
| 140 | Not | ||||||||||
| 141 | |||||||||||
| 142 | Minimum ExclusiveMinimum Maximum ExclusiveMaximum MultipleOf | ||||||||||
| 143 | |||||||||||
| 144 | MaxLength MinLength | ||||||||||
| 145 | |||||||||||
| 146 | Items AdditionalItems MaxItems MinItems UniqueItems | ||||||||||
| 147 | |||||||||||
| 148 | PatternProperties AdditionalProperties MaxProperties MinProperties | ||||||||||
| 149 | |||||||||||
| 150 | Dependencies Dependency | ||||||||||
| 151 | |||||||||||
| 152 | =head2 Schema | ||||||||||
| 153 | |||||||||||
| 154 | Only verifies that the variable is a L<Type::Tiny>. | ||||||||||
| 155 | |||||||||||
| 156 | Can coerce the value from a hashref defining the schema. | ||||||||||
| 157 | |||||||||||
| 158 | my $schema = Schema->coerce( \%schema ); | ||||||||||
| 159 | |||||||||||
| 160 | # equivalent to | ||||||||||
| 161 | |||||||||||
| 162 | $schema = JSON::Schema::AsType::Draft4->new( | ||||||||||
| 163 | draft_version => 3, | ||||||||||
| 164 | schema => \%schema; | ||||||||||
| 165 | )->type; | ||||||||||
| 166 | |||||||||||
| 167 | =head1 AUTHOR | ||||||||||
| 168 | |||||||||||
| 169 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||
| 170 | |||||||||||
| 171 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 172 | |||||||||||
| 173 | This software is copyright (c) 2015 by Yanick Champoux. | ||||||||||
| 174 | |||||||||||
| 175 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 176 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 177 | |||||||||||
| 178 | =cut | ||||||||||