File: openapiv2-collection-format.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 (74 lines) | stat: -rw-r--r-- 3,377 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

my $schema = JSON::Validator->new->schema('data://main/spec.json')->schema;
my @errors;

@errors = $schema->validate_request([get => '/pets/{id}'], {path => {id => '42'}});
is "@errors", "", 'collectionFormat csv in path';

for ([csv => ","], [pipes => "|"], [ssv => " "], [tsv => "\t"]) {
  my ($name, $sep) = @$_;
  my $empty = $name =~ m!^pipes! ? '' : '0';

  @errors = $schema->validate_request([get => '/pets'], {query => {"${name}0" => $empty, multir => ''}});
  is "@errors", "", "collectionFormat ${name}0 empty string";

  @errors = $schema->validate_request([get => '/pets'], {query => {"${name}0" => '42', multir => ''}});
  is "@errors", "", "collectionFormat ${name}0 single item";

  @errors = $schema->validate_request([get => '/pets'], {query => {"${name}0" => "4${sep}2", multir => ''}});
  is "@errors", "", "collectionFormat ${name}0 two item";

  @errors = $schema->validate_request([get => '/pets'], {query => {"${name}2" => '42', multir => ''}});
  is "@errors", "/${name}2: Not enough items: 1/2.", "collectionFormat ${name}2 single item";
}

done_testing;

__DATA__
@@ spec.json
{
  "swagger": "2.0",
  "info": {"version": "", "title": "Test collectionFormat"},
  "basePath": "/api",
  "paths": {
    "/pets/{id}": {
      "get": {
        "parameters": [
          {"name": "id", "in": "path", "type": "array", "collectionFormat": "csv", "items": {"type": "string"}, "minItems": 0, "required": true}
        ],
        "responses": {
          "200": {
            "description": "pet response",
            "schema": {"type": "object"}
          }
        }
      }
    },
    "/pets": {
      "get": {
        "parameters": [
          {"name": "csv0", "in": "query", "type": "array", "collectionFormat": "csv", "items": {"type": "number"}, "minItems": 0},
          {"name": "csv2", "in": "query", "type": "array", "collectionFormat": "csv", "items": {"type": "number"}, "minItems": 2},
          {"name": "multi0", "in": "query", "type": "array", "collectionFormat": "multi", "items": {"type": "integer"}, "minItems": 0},
          {"name": "multi2", "in": "query", "type": "array", "collectionFormat": "multi", "items": {"type": "integer"}, "minItems": 2},
          {"name": "multir", "in": "query", "type": "array", "collectionFormat": "multi", "required": true, "items": {"type": "string"}, "minItems":1},
          {"name": "pipes0", "in": "query", "type": "array", "collectionFormat": "pipes", "items": {"type": "string"}, "minItems": 0},
          {"name": "pipes2", "in": "query", "type": "array", "collectionFormat": "pipes", "items": {"type": "integer"}, "minItems": 2},
          {"name": "ssv0", "in": "query", "type": "array", "collectionFormat": "ssv", "items": {"type": "number"}, "minItems": 0},
          {"name": "ssv2", "in": "query", "type": "array", "collectionFormat": "ssv", "items": {"type": "number"}, "minItems": 2},
          {"name": "tsv0", "in": "query", "type": "array", "collectionFormat": "tsv", "items": {"type": "integer"}, "minItems": 0},
          {"name": "tsv2", "in": "query", "type": "array", "collectionFormat": "tsv", "items": {"type": "integer"}, "minItems": 2}
        ],
        "responses": {
          "200": {
            "description": "pet response",
            "schema": {"type": "object"}
          }
        }
      }
    }
  }
}