File: openapiv2-discriminator.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 (94 lines) | stat: -rw-r--r-- 3,060 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

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

my %cat = (name => 'kit-e-cat', petType => 'Cat', huntingSkill => 'adventurous');
my %dog = (name => 'dog-e-dog', petType => 'Dog', packSize     => 4);

$body   = sub { {exists => 1, value => {%cat, petType => 'Dog'}} };
@errors = $schema->validate_request([post => '/pets'], {body => $body});
is "@errors", '/body/packSize: /allOf/1 Missing property.', 'invalid dog';

$body   = sub { {exists => 1, value => {%cat}} };
@errors = $schema->validate_request([post => '/pets'], {body => $body});
is "@errors", '', 'valid cat';

$body   = sub { {exists => 1, value => {%dog, petType => 'Cat'}} };
@errors = $schema->validate_request([post => '/pets'], {body => $body});
is "@errors", '/body/huntingSkill: /allOf/1 Missing property.', 'invalid cat';

$body   = sub { {exists => 1, value => {%dog}} };
@errors = $schema->validate_request([post => '/pets'], {body => $body});
is "@errors", '', 'valid dog';

$body   = sub { {exists => 1, value => {%dog, petType => ''}} };
@errors = $schema->validate_request([post => '/pets'], {body => $body});
is "@errors", '/body: Discriminator petType has no value.', 'discriminator is required';

$body   = sub { {exists => 1, value => {%dog, petType => 'Hamster'}} };
@errors = $schema->validate_request([post => '/pets'], {body => $body});
is "@errors", '/body: No definition for discriminator Hamster.', 'invalid discriminator';

done_testing;

__DATA__
@@ spec.json
{
  "swagger": "2.0",
  "info": {"version": "", "title": "Test discriminator"},
  "basePath": "/api",
  "paths": {
    "/pets": {
      "post": {
        "parameters": [
          {"in": "body", "name": "body", "schema": {"$ref": "#/definitions/Pet"}}
        ],
        "responses": {
          "200": {
            "description": "pet response",
            "schema": {"type": "object"}
          }
        }
      }
    }
  },
  "definitions": {
    "Pet": {
      "type": "object",
      "discriminator": "petType",
      "required": ["name", "petType"],
      "properties": {
        "name": {"type": "string"},
        "petType": {"type": "string"}
      }
    },
    "Cat": {
      "description": "A representation of a cat",
      "allOf": [
        {"$ref": "#/definitions/Pet"},
        {
          "type": "object",
          "required": ["huntingSkill"],
          "properties": {"huntingSkill": {"type": "string", "description": "The measured skill for hunting", "default": "lazy", "enum": ["clueless", "lazy", "adventurous", "aggressive"]}
          }
        }
      ]
    },
    "Dog": {
      "description": "A representation of a dog",
      "allOf": [
        {"$ref": "#/definitions/Pet"},
        {
          "type": "object",
          "required": ["packSize"],
          "properties": {
            "packSize": {"type": "integer", "format": "int32", "description": "the size of the pack the dog is from", "default": 0, "minimum": 0}
          }
        }
      ]
    }
  }
}