File: openapiv3-readonly-writeonly.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 (64 lines) | stat: -rw-r--r-- 2,007 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
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

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

$body   = sub { +{exists => 1, value => {birth => '1983-02-24'}} };
@errors = $schema->validate_request([post => '/user'], {body => $body});
is "@errors", '', 'required is ignored on validate_request';

$body   = sub { +{exists => 1, value => {age => 42, birth => '1983-02-24'}} };
@errors = $schema->validate_request([post => '/user'], {body => $body});
is "@errors", '/body/age: Read-only.', 'age is read-only for request';

$body   = sub { +{exists => 1, value => {}} };
@errors = $schema->validate_response([get => '/user'], {body => $body});
is "@errors", '/body/age: Missing property.', 'age is required in response';

$body   = sub { +{exists => 1, value => {age => 42}} };
@errors = $schema->validate_response([get => '/user'], {body => $body});
is "@errors", '', 'age is present in response';

$body   = sub { +{exists => 1, value => {age => 42, birth => '1983-02-24'}} };
@errors = $schema->validate_response([get => '/user'], {body => $body});
is "@errors", '/body/birth: Write-only.', 'birth is write-only in response';

done_testing;

__DATA__
@@ spec.json
{
  "openapi": "3.0.0",
  "info": { "title": "Read/write-only", "version": "" },
  "paths": {
    "/user": {
      "get": {
        "responses": {
          "200": {
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/User" } } }
          }
        }
      },
      "post": {
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/User" } } }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "required": ["age", "birth"],
        "properties": {
          "age": {"type": "integer", "readOnly": true},
          "birth": {"type": "string", "writeOnly": true}
        }
      }
    }
  }
}