/usr/share/perl5/Swagger2/SchemaValidator.pm is in libswagger2-perl 0.73-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | package Swagger2::SchemaValidator;
use Mojo::Base 'JSON::Validator';
use Scalar::Util ();
use constant IV_SIZE => eval 'require Config;$Config::Config{ivsize}';
our %COLLECTION_RE = (pipes => qr{\|}, csv => qr{,}, ssv => qr{\s}, tsv => qr{\t});
sub validate_input {
my $self = shift;
local $self->{validate_input} = 1;
$self->validate(@_);
}
sub _validate_type_array {
my ($self, $data, $path, $schema) = @_;
if (ref $data eq 'ARRAY' and ref $schema->{items} eq 'HASH' and $schema->{items}{collectionFormat}) {
$self->_coerce_by_collection_format($data, $schema->{items});
}
return $self->SUPER::_validate_type_array(@_[1, 2, 3]);
}
# always valid
sub _validate_type_file { }
sub _validate_type_object {
return shift->SUPER::_validate_type_object(@_) unless $_[0]->{validate_input};
my ($self, $data, $path, $schema) = @_;
my $properties = $schema->{properties} || {};
my (%ro, @e);
for my $p (keys %$properties) {
next unless $properties->{$p}{readOnly};
push @e, JSON::Validator::E("$path/$p", "Read-only.") if exists $data->{$p};
$ro{$p} = 1;
}
local $schema->{required} = [grep { !$ro{$_} } @{$schema->{required} || []}];
return @e, $self->SUPER::_validate_type_object($data, $path, $schema);
}
sub _build_formats {
my $formats = shift->SUPER::_build_formats;
$formats->{byte} = \&_is_byte_string;
$formats->{date} = \&_is_date;
$formats->{double} = \&Scalar::Util::looks_like_number;
$formats->{float} = \&Scalar::Util::looks_like_number;
$formats->{int32} = sub { _is_number($_[0], 'l'); };
$formats->{int64} = IV_SIZE >= 8 ? sub { _is_number($_[0], 'q'); } : sub {1};
return $formats;
}
sub _coerce_by_collection_format {
my ($self, $data, $schema) = @_;
my $re = $COLLECTION_RE{$schema->{collectionFormat}} || ',';
my $type = $schema->{type} || '';
for my $i (0 .. @$data - 1) {
my @d = split /$re/, $data->[$i];
$data->[$i] = ($type eq 'integer' or $type eq 'number') ? [map { $_ + 0 } @d] : \@d;
}
}
sub _is_byte_string { $_[0] =~ /^[A-Za-z0-9\+\/\=]+$/ }
sub _is_date { $_[0] =~ /^(\d+)-(\d+)-(\d+)$/ }
sub _is_number {
return unless $_[0] =~ /^-?\d+(\.\d+)?$/;
return $_[0] eq unpack $_[1], pack $_[1], $_[0];
}
1;
=encoding utf8
=head1 NAME
Swagger2::SchemaValidator - Sub class of JSON::Validator
=head1 DESCRIPTION
This class is used to validate Swagger specification. It is a sub class of
L<JSON::Validator> and adds some extra functionality specific for L<Swagger2>.
=head1 ATTRIBUTES
L<Swagger2::SchemaValidator> inherits all attributes from L<JSON::Validator>.
=head2 formats
Swagger support the same formats as L<Swagger2::SchemaValidator>, but adds the
following to the set:
=over 4
=item * byte
A padded, base64-encoded string of bytes, encoded with a URL and filename safe
alphabet. Defined by RFC4648.
=item * date
An RFC3339 date in the format YYYY-MM-DD
=item * double
Cannot test double values with higher precision then what
the "number" type already provides.
=item * float
Will always be true if the input is a number, meaning there is no difference
between L</float> and L</double>. Patches are welcome.
=item * int32
A signed 32 bit integer.
=item * int64
A signed 64 bit integer. Note: This check is only available if Perl is
compiled to use 64 bit integers.
=back
=head1 METHODS
L<Swagger2::SchemaValidator> inherits all attributes from L<JSON::Validator>.
=head2 validate_input
This method will make sure "readOnly" is taken into account, when validating
data sent to your API.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014-2015, Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
=head1 AUTHOR
Jan Henning Thorsen - C<jhthorsen@cpan.org>
=cut
|