File: openapiv3-coerce-array.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 (55 lines) | stat: -rw-r--r-- 1,279 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
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

my $schema = JSON::Validator->new->schema('data://main/openapi.yaml')->schema;
my ($body, @errors);

subtest 'number to array' => sub {
  $body   = {exists => 1, value => {id => 42}};
  @errors = $schema->validate_request([post => '/test'], {body => \&body});
  is "@errors", "", "valid";
};

subtest 'string to array' => sub {
  $body   = {exists => 1, value => {id => '42'}};
  @errors = $schema->validate_request([post => '/test'], {body => \&body});
  is "@errors", "", "valid";
};

subtest 'already an array' => sub {
  $body   = {exists => 1, value => {id => [42, '43']}};
  @errors = $schema->validate_request([post => '/test'], {body => \&body});
  is "@errors", "", "valid";
};

done_testing;

sub body {$body}

__DATA__
@@ openapi.yaml
---
openapi: 3.0.0
info:
  title: Upload test
  version: 1.0.0
servers:
- url: http://example.com/api
paths:
  /test:
    post:
      operationId: testPost
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              properties:
                id:
                  type: array
                  items:
                    type: integer
      responses:
        200:
          description: OK