File: meta.rb

package info (click to toggle)
ruby-json-schemer 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 7,428; makefile: 4; sh: 4
file content (136 lines) | stat: -rw-r--r-- 4,570 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true
module JSONSchemer
  module OpenAPI31
    BASE_URI = URI('https://spec.openapis.org/oas/3.1/dialect/base')
    # https://spec.openapis.org/oas/v3.1.0#data-types
    FORMATS = {
      'int32' => proc { |instance, _format| !Draft202012::Vocab::Validation::Type.valid_integer?(instance) || instance.floor.bit_length < 32 },
      'int64' => proc { |instance, _format| !Draft202012::Vocab::Validation::Type.valid_integer?(instance) || instance.floor.bit_length < 64 },
      'float' => proc { |instance, _format| !instance.is_a?(Numeric) || instance.is_a?(Float) },
      'double' => proc { |instance, _format| !instance.is_a?(Numeric) || instance.is_a?(Float) },
      'password' => proc { |_instance, _format| true }
    }
    SCHEMA = {
      '$id' => 'https://spec.openapis.org/oas/3.1/dialect/base',
      '$schema' => 'https://json-schema.org/draft/2020-12/schema',

      'title' => 'OpenAPI 3.1 Schema Object Dialect',
      'description' => 'A JSON Schema dialect describing schemas found in OpenAPI documents',

      '$vocabulary' => {
        'https://json-schema.org/draft/2020-12/vocab/core' => true,
        'https://json-schema.org/draft/2020-12/vocab/applicator' => true,
        'https://json-schema.org/draft/2020-12/vocab/unevaluated' => true,
        'https://json-schema.org/draft/2020-12/vocab/validation' => true,
        'https://json-schema.org/draft/2020-12/vocab/meta-data' => true,
        'https://json-schema.org/draft/2020-12/vocab/format-annotation' => true,
        'https://json-schema.org/draft/2020-12/vocab/content' => true,
        'https://spec.openapis.org/oas/3.1/vocab/base' => false
      },

      '$dynamicAnchor' => 'meta',

      'allOf' => [
        { '$ref' => 'https://json-schema.org/draft/2020-12/schema' },
        { '$ref' => 'https://spec.openapis.org/oas/3.1/meta/base' }
      ]
    }


    module Meta
      BASE = {
        '$id' => 'https://spec.openapis.org/oas/3.1/meta/base',
        '$schema' => 'https://json-schema.org/draft/2020-12/schema',

        'title' => 'OAS Base vocabulary',
        'description' => 'A JSON Schema Vocabulary used in the OpenAPI Schema Dialect',

        '$vocabulary' => {
          'https://spec.openapis.org/oas/3.1/vocab/base' => true
        },

        '$dynamicAnchor' => 'meta',

        'type' => ['object', 'boolean'],
        'properties' => {
          'example' => true,
          'discriminator' => { '$ref' => '#/$defs/discriminator' },
          'externalDocs' => { '$ref' => '#/$defs/external-docs' },
          'xml' => { '$ref' => '#/$defs/xml' }
        },

        '$defs' => {
          'extensible' => {
            'patternProperties' => {
              '^x-' => true
            }
          },

          'discriminator' => {
            '$ref' => '#/$defs/extensible',
            'type' => 'object',
            'properties' => {
              'propertyName' => {
                'type' => 'string'
              },
              'mapping' => {
                'type' => 'object',
                'additionalProperties' => {
                  'type' => 'string'
                }
              }
            },
            'required' => ['propertyName'],
            'unevaluatedProperties' => false
          },

          'external-docs' => {
            '$ref' => '#/$defs/extensible',
            'type' => 'object',
            'properties' => {
              'url' => {
                'type' => 'string',
                'format' => 'uri-reference'
              },
              'description' => {
                'type' => 'string'
              }
            },
            'required' => ['url'],
            'unevaluatedProperties' => false
          },

          'xml' => {
            '$ref' => '#/$defs/extensible',
            'type' => 'object',
            'properties' => {
              'name' => {
                'type' => 'string'
              },
              'namespace' => {
                'type' => 'string',
                'format' => 'uri'
              },
              'prefix' => {
                'type' => 'string'
              },
              'attribute' => {
                'type' => 'boolean'
              },
              'wrapped' => {
                'type' => 'boolean'
              }
            },
            'unevaluatedProperties' => false
          }
        }
      }


      SCHEMAS = Draft202012::Meta::SCHEMAS.merge(
        Draft202012::BASE_URI => Draft202012::SCHEMA,
        URI('https://spec.openapis.org/oas/3.1/meta/base') => BASE
      )
    end
  end
end