Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
update JSD2
Browse files Browse the repository at this point in the history
  • Loading branch information
karenetheridge committed Dec 8, 2020
1 parent b23f652 commit 105035d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cpanfile
Expand Up @@ -29,7 +29,7 @@ requires 'Email::Sender::Simple';
requires 'Email::Sender::Transport::SMTP';
requires 'Net::DNS'; # not used directly, but Email::Valid sometimes demands it
requires 'experimental', '0.020';
requires 'JSON::Schema::Draft201909', '0.017';
requires 'JSON::Schema::Draft201909', '0.019';
requires 'Email::Address::XS', '1.01';
requires 'YAML::PP';
requires 'next::XS';
Expand Down
32 changes: 16 additions & 16 deletions cpanfile.snapshot
Expand Up @@ -2283,22 +2283,22 @@ DISTRIBUTIONS
JSON::PP 2.27300
Scalar::Util 0
perl 5.006
JSON-Schema-Draft201909-0.017
pathname: E/ET/ETHER/JSON-Schema-Draft201909-0.017.tar.gz
provides:
JSON::Schema::Draft201909 0.017
JSON::Schema::Draft201909::Annotation 0.017
JSON::Schema::Draft201909::Document 0.017
JSON::Schema::Draft201909::Error 0.017
JSON::Schema::Draft201909::Result 0.017
JSON::Schema::Draft201909::Utilities 0.017
JSON::Schema::Draft201909::Vocabulary 0.017
JSON::Schema::Draft201909::Vocabulary::Applicator 0.017
JSON::Schema::Draft201909::Vocabulary::Content 0.017
JSON::Schema::Draft201909::Vocabulary::Core 0.017
JSON::Schema::Draft201909::Vocabulary::Format 0.017
JSON::Schema::Draft201909::Vocabulary::MetaData 0.017
JSON::Schema::Draft201909::Vocabulary::Validation 0.017
JSON-Schema-Draft201909-0.019
pathname: E/ET/ETHER/JSON-Schema-Draft201909-0.019.tar.gz
provides:
JSON::Schema::Draft201909 0.019
JSON::Schema::Draft201909::Annotation 0.019
JSON::Schema::Draft201909::Document 0.019
JSON::Schema::Draft201909::Error 0.019
JSON::Schema::Draft201909::Result 0.019
JSON::Schema::Draft201909::Utilities 0.019
JSON::Schema::Draft201909::Vocabulary 0.019
JSON::Schema::Draft201909::Vocabulary::Applicator 0.019
JSON::Schema::Draft201909::Vocabulary::Content 0.019
JSON::Schema::Draft201909::Vocabulary::Core 0.019
JSON::Schema::Draft201909::Vocabulary::Format 0.019
JSON::Schema::Draft201909::Vocabulary::MetaData 0.019
JSON::Schema::Draft201909::Vocabulary::Validation 0.019
requirements:
B 0
Carp 0
Expand Down
7 changes: 3 additions & 4 deletions lib/Conch/Plugin/JSONValidator.pm
Expand Up @@ -3,7 +3,7 @@ package Conch::Plugin::JSONValidator;
use Mojo::Base 'Mojolicious::Plugin', -signatures;

use feature 'unicode_strings';
use JSON::Schema::Draft201909 '0.017';
use JSON::Schema::Draft201909 '0.019';
use YAML::PP;
use Mojo::JSON 'to_json';
use Path::Tiny;
Expand Down Expand Up @@ -71,7 +71,7 @@ Returns a boolean.

$app->helper(validate_query_params => sub ($c, $schema_name, $data = $c->req->query_params->to_hash) {
my $validator = $c->json_schema_validator;
my $result = $validator->evaluate($data, 'query_params.yaml#/$defs/'.$schema_name);
my $result = $validator->evaluate($data, 'query_params.yaml#/$defs/'.$schema_name, { collect_annotations => 1 });
if (not $result) {
my @errors = $c->normalize_evaluation_result($result);
$c->log->warn("FAILED query_params validation for schema $schema_name: ".to_json(\@errors));
Expand Down Expand Up @@ -139,7 +139,6 @@ Returns a L<JSON::Schema::Draft201909> object with all JSON Schemas pre-loaded.
$_validator = JSON::Schema::Draft201909->new(
output_format => 'terse',
validate_formats => 1,
collect_annotations => 1,
);
# TODO: blocked on https://github.com/ingydotnet/yaml-libyaml-pm/issues/68
# local $YAML::XS::Boolean = 'JSON::PP'; ... YAML::XS::LoadFile(...)
Expand All @@ -151,7 +150,7 @@ Returns a L<JSON::Schema::Draft201909> object with all JSON Schemas pre-loaded.
catch {
require Data::Dumper;
die "problems adding schema (YAML is not parseable?) - ",
Data::Dumper->new([ [ map $_->TO_JSON, @$_ ] ])->Indent(0)->Terse(1)->Dump;
Data::Dumper->new([ $@->TO_JSON ])->Indent(0)->Terse(1)->Dump;
};

$_validator;
Expand Down
3 changes: 1 addition & 2 deletions misc/validate
Expand Up @@ -4,7 +4,7 @@ use warnings;
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8

use Getopt::Long;
use JSON::Schema::Draft201909;
use JSON::Schema::Draft201909 0.019;
use Mojo::File 'path';
use JSON::MaybeXS 'decode_json';
use Pod::Usage;
Expand All @@ -25,7 +25,6 @@ pod2usage(1) if $help;
my $js = JSON::Schema::Draft201909->new(
output_format => 'basic',
validate_formats => 1,
collect_annotations => 1,
);

my $schema = $schema_file ? $schema_file : "$schema_url/$name";
Expand Down
5 changes: 2 additions & 3 deletions t/02-json-schemas.t
Expand Up @@ -6,7 +6,7 @@ use Test::More;
use YAML::PP;
use Path::Tiny;
use Try::Tiny;
use JSON::Schema::Draft201909;
use JSON::Schema::Draft201909 0.019;
use JSON::Schema::Draft201909::Utilities 'canonical_schema_uri';

diag 'using JSON::Schema::Draft201909 '.JSON::Schema::Draft201909->VERSION;
Expand All @@ -15,7 +15,6 @@ my $yaml = YAML::PP->new(boolean => 'JSON::PP');
my $js = JSON::Schema::Draft201909->new(
output_format => 'terse',
validate_formats => 1,
collect_annotations => 1,
max_traversal_depth => 67, # needed for other.yaml
);

Expand All @@ -29,7 +28,7 @@ foreach my $filename (split /\n/, `git ls-files json-schema`) {
$js->add_schema($path->basename, $yaml->load_file($filename));
}
catch {
die "$filename is not parseable: ", explain(map $_->TO_JSON, @$_);
die "$filename is not parseable: ", explain($@->TO_JSON);
};
push @files, $path->basename;
}
Expand Down
12 changes: 6 additions & 6 deletions t/integration/crud/hardware-product.t
Expand Up @@ -68,19 +68,19 @@ my %hw_fields = (
$t->post_ok('/hardware_product', json => { %hw_fields, specification => 'not json!' } )
->status_is(400)
->json_schema_is('RequestValidationError')
->json_cmp_deeply('/details', superbagof(superhashof({ data_location => '/specification', error => 'wrong type (expected object)' })));
->json_cmp_deeply('/details', [ superhashof({ data_location => '/specification', error => 'wrong type (expected object)' }) ]);

$t->post_ok('/hardware_product', json => { %hw_fields, specification => '{"disk_size":"not an object"}' } )
->status_is(400)
->json_schema_is('RequestValidationError')
->json_cmp_deeply('/details', superbagof(superhashof({ data_location => '/specification', error => 'wrong type (expected object)' })));
->json_cmp_deeply('/details', [ superhashof({ data_location => '/specification', error => 'wrong type (expected object)' }) ]);

$t->post_ok('/hardware_product', json => { %hw_fields, specification => { disk_size => 'not an object' } })
->status_is(400)
->json_schema_is('RequestValidationError')
->json_cmp_deeply({
error => 'request did not match required format',
details => superbagof(superhashof({ data_location => '/specification/disk_size', error => 'wrong type (expected object)' })),
details => [ superhashof({ data_location => '/specification/disk_size', error => 'wrong type (expected object)' }) ],
schema => $base_uri.'json_schema/request/HardwareProductCreate',
});

Expand Down Expand Up @@ -181,19 +181,19 @@ $t->post_ok('/hardware_product', json => {
$t->post_ok("/hardware_product/$new_hw_id", json => { specification => 'not json!' })
->status_is(400)
->json_schema_is('RequestValidationError')
->json_cmp_deeply('/details', superbagof(superhashof({ data_location => '/specification', error => 'wrong type (expected object)' })));
->json_cmp_deeply('/details', [ superhashof({ data_location => '/specification', error => 'wrong type (expected object)' }) ]);

$t->post_ok("/hardware_product/$new_hw_id", json => { specification => '{"disk_size":"not an object"}' })
->status_is(400)
->json_schema_is('RequestValidationError')
->json_cmp_deeply('/details', superbagof(superhashof({ data_location => '/specification', error => 'wrong type (expected object)' })));
->json_cmp_deeply('/details', [ superhashof({ data_location => '/specification', error => 'wrong type (expected object)' }) ]);

$t->post_ok("/hardware_product/$new_hw_id", json => { specification => { disk_size => 'not an object' } })
->status_is(400)
->json_schema_is('RequestValidationError')
->json_cmp_deeply({
error => 'request did not match required format',
details => superbagof(superhashof({ data_location => '/specification/disk_size', error => 'wrong type (expected object)' })),
details => [ superhashof({ data_location => '/specification/disk_size', error => 'wrong type (expected object)' }) ],
schema => $base_uri.'json_schema/request/HardwareProductUpdate',
});

Expand Down

0 comments on commit 105035d

Please sign in to comment.