File: validate-recursive.t

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 (52 lines) | stat: -rw-r--r-- 1,238 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
52
use Mojo::Base -strict;
use JSON::Validator;
use Test::Mojo;
use Test::More;

use Mojolicious::Lite;
post '/' => sub {
  my $c      = shift;
  my @errors = JSON::Validator->new->schema('data://main/spec.json')->validate($c->req->json);
  $c->render(status => @errors ? 400 : 200, json => \@errors);
};

my $t = Test::Mojo->new;

$t->post_ok('/', json => {})->status_is(400)->content_like(qr{/person});
$t->post_ok('/', json => {person => {name => 'superwoman'}})->status_is(200);
$t->post_ok('/', json => {person => {name => 'superwoman', children => [{name => 'batboy'}]}})->status_is(200);
$t->post_ok('/', json => {person => {name => 'superwoman', children => [{}]}})->status_is(400)
  ->json_is('/0/path' => '/person/children/0/name');

done_testing;

__DATA__
@@ spec.json
{
  "type": "object",
  "properties": {
    "person": {
      "$ref": "#/definitions/person"
    }
  },
  "required": [
    "person"
  ],
  "definitions": {
    "person": {
      "type": "object",
      "required": [ "name" ],
      "properties": {
        "name": {
          "type": "string"
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/person"
          }
        }
      }
    }
  }
}