File: load-data.t

package info (click to toggle)
libjson-validator-perl 5.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,160 kB
  • sloc: perl: 3,015; makefile: 14
file content (51 lines) | stat: -rw-r--r-- 1,585 bytes parent folder | download | duplicates (3)
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
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

my $jv     = JSON::Validator->new;
my @errors = $jv->schema('data://main/spec.json')->validate({firstName => 'yikes!'});

is int(@errors), 1, 'one error';
is $errors[0]->path,    '/lastName',         'lastName';
is $errors[0]->message, 'Missing property.', 'required';
is_deeply $errors[0]->TO_JSON, {path => '/lastName', message => 'Missing property.'}, 'TO_JSON';

use Mojo::File 'path';
push @INC, path(path(__FILE__)->dirname, 'stack')->to_string;
require Some::Module;

eval { Some->validate_age1({age => 1}) };
like $@, qr{age1\.json}, 'could not find age1.json';

ok !Some->validate_age0({age => 1}), 'validate_age0';
ok !Some::Module->validate_age0({age => 1}), 'validate_age0';
ok !Some::Module->validate_age1({age => 1}), 'validate_age1';

eval { Mojolicious::Plugin::TestX->validate('data:///spec.json', {}) };
ok !$@, 'found spec.json in main' or diag $@;

@errors = $jv->schema('data://main/spec.json')->validate({});
like "@errors", qr{firstName.*lastName}, 'required is sorted';

package Mojolicious::Plugin::TestX;
sub validate { $jv->schema($_[1])->validate($_[2]) }

package main;
is_deeply [sort keys %{$jv->store->schemas}], [qw(data:///spec.json data://main/spec.json)], 'schemas in store';

done_testing;

__DATA__
@@ spec.json

{
  "title": "Example Schema",
  "type": "object",
  "required": ["lastName", "firstName"],
  "properties": {
      "firstName": { "type": "string" },
      "lastName": { "type": "string" },
      "age": { "type": "integer", "minimum": 0, "description": "トシ" }
  }
}