File: Draft6.pm

package info (click to toggle)
libjson-validator-perl 4.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 828 kB
  • sloc: perl: 2,816; makefile: 14
file content (136 lines) | stat: -rw-r--r-- 4,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package JSON::Validator::Schema::Draft6;
use Mojo::Base 'JSON::Validator::Schema::Draft4';

use JSON::Validator::Util qw(E data_type is_type prefix_errors);

has id => sub {
  my $data = shift->data;
  return is_type($data, 'HASH') ? $data->{'$id'} || '' : '';
};

has specification => 'http://json-schema.org/draft-06/schema#';

sub _build_formats {
  return {
    'date-time'             => JSON::Validator::Formats->can('check_date_time'),
    'email'                 => JSON::Validator::Formats->can('check_email'),
    'hostname'              => JSON::Validator::Formats->can('check_hostname'),
    'ipv4'                  => JSON::Validator::Formats->can('check_ipv4'),
    'ipv6'                  => JSON::Validator::Formats->can('check_ipv6'),
    'json-pointer'          => JSON::Validator::Formats->can('check_json_pointer'),
    'regex'                 => JSON::Validator::Formats->can('check_regex'),
    'relative-json-pointer' => JSON::Validator::Formats->can('check_relative_json_pointer'),
    'uri'                   => JSON::Validator::Formats->can('check_uri'),
    'uri-reference'         => JSON::Validator::Formats->can('check_uri_reference'),
    'uri-template'          => JSON::Validator::Formats->can('check_uri_template'),
  };
}

sub _id_key {'$id'}

sub _validate_number_max {
  my ($self, $value, $path, $schema, $expected) = @_;

  my $cmp_with = $schema->{maximum};
  return E $path, [$expected => maximum => $value, $cmp_with] if defined $cmp_with and $value > $cmp_with;

  $cmp_with = $schema->{exclusiveMaximum};
  return E $path, [$expected => ex_maximum => $value, $cmp_with] if defined $cmp_with and $value >= $cmp_with;

  return;
}

sub _validate_number_min {
  my ($self, $value, $path, $schema, $expected) = @_;

  my $cmp_with = $schema->{minimum};
  return E $path, [$expected => minimum => $value, $cmp_with] if defined $cmp_with and $value < $cmp_with;

  $cmp_with = $schema->{exclusiveMinimum};
  return E $path, [$expected => ex_minimum => $value, $cmp_with] if defined $cmp_with and $value <= $cmp_with;

  return;
}

sub _validate_type_array {
  my ($self, $data, $path, $schema) = @_;
  return E $path, [array => type => data_type $data] if ref $data ne 'ARRAY';

  return (
    $self->_validate_type_array_min_max($_[1], $path, $schema),
    $self->_validate_type_array_unique($_[1], $path, $schema),
    $self->_validate_type_array_contains($_[1], $path, $schema),
    $self->_validate_type_array_items($_[1], $path, $schema),
  );
}

sub _validate_type_array_contains {
  my ($self, $data, $path, $schema) = @_;
  return unless exists $schema->{contains};

  my (@e, @errors);
  for my $i (0 .. @$data - 1) {
    my @tmp = $self->_validate($data->[$i], "$path/$i", $schema->{contains});
    push @e, \@tmp if @tmp;
  }

  push @errors, map {@$_} @e if @e >= @$data;
  push @errors, E $path, [array => 'contains'] if not @$data;
  return @errors;
}

sub _validate_type_object {
  my ($self, $data, $path, $schema) = @_;
  return E $path, [object => type => data_type $data] if ref $data ne 'HASH';

  return (
    $self->_validate_type_object_min_max($_[1], $path, $schema),
    $self->_validate_type_object_names($_[1], $path, $schema),
    $self->_validate_type_object_properties($_[1], $path, $schema),
    $self->_validate_type_object_dependencies($_[1], $path, $schema),
  );
}

sub _validate_type_object_names {
  my ($self, $data, $path, $schema) = @_;
  return unless exists $schema->{propertyNames};

  my @errors;
  for my $name (keys %$data) {
    next unless my @e = $self->_validate($name, $path, $schema->{propertyNames});
    push @errors, prefix_errors propertyName => map [$name, $_], @e;
  }

  return @errors;
}

1;

=encoding utf8

=head1 NAME

JSON::Validator::Schema::Draft6 - JSON-Schema Draft 6

=head1 SYNOPSIS

See L<JSON::Validator::Schema/SYNOPSIS>.

=head1 DESCRIPTION

This class represents
L<https://json-schema.org/specification-links.html#draft-6>.

=head1 ATTRIBUTES

=head2 specification

  my $str = $schema->specification;

Defaults to "L<http://json-schema.org/draft-06/schema#>".

=head1 SEE ALSO

L<JSON::Validator::Schema>.

=cut