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 95
|
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 => {%cat, 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
{
"openapi": "3.0.0",
"info": { "title": "Discriminator", "version": "" },
"paths": {
"/pets": {
"post": {
"requestBody": {
"content": { "application/json": { "schema": { "$ref": "#/components/schemas/Pet" } } }
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"discriminator": {
"propertyName": "petType",
"mapping": {
"cat": "#/components/schemas/Cat",
"dog": "#/components/schemas/Dog"
}
},
"required": ["name", "petType"],
"properties": {
"name": {"type": "string"},
"petType": {"type": "string"}
}
},
"Cat": {
"description": "A representation of a cat",
"allOf": [
{"$ref": "#/components/schemas/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": "#/components/schemas/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}
}
}
]
}
}
}
}
|