jsapi 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/jsapi/controller/base.rb +21 -0
- data/lib/jsapi/controller/error_result.rb +21 -0
- data/lib/jsapi/controller/methods.rb +144 -0
- data/lib/jsapi/controller/parameters.rb +86 -0
- data/lib/jsapi/controller/parameters_invalid.rb +25 -0
- data/lib/jsapi/controller/response.rb +84 -0
- data/lib/jsapi/controller.rb +13 -0
- data/lib/jsapi/dsl/callbacks.rb +32 -0
- data/lib/jsapi/dsl/class_methods.rb +85 -0
- data/lib/jsapi/dsl/definitions.rb +102 -0
- data/lib/jsapi/dsl/error.rb +38 -0
- data/lib/jsapi/dsl/examples.rb +30 -0
- data/lib/jsapi/dsl/node.rb +62 -0
- data/lib/jsapi/dsl/openapi/callback.rb +23 -0
- data/lib/jsapi/dsl/openapi/root.rb +12 -0
- data/lib/jsapi/dsl/openapi.rb +4 -0
- data/lib/jsapi/dsl/operation.rb +118 -0
- data/lib/jsapi/dsl/parameter.rb +10 -0
- data/lib/jsapi/dsl/request_body.rb +10 -0
- data/lib/jsapi/dsl/response.rb +33 -0
- data/lib/jsapi/dsl/schema.rb +87 -0
- data/lib/jsapi/dsl.rb +24 -0
- data/lib/jsapi/json/array.rb +35 -0
- data/lib/jsapi/json/boolean.rb +17 -0
- data/lib/jsapi/json/integer.rb +15 -0
- data/lib/jsapi/json/null.rb +27 -0
- data/lib/jsapi/json/number.rb +15 -0
- data/lib/jsapi/json/object.rb +53 -0
- data/lib/jsapi/json/string.rb +29 -0
- data/lib/jsapi/json/value.rb +47 -0
- data/lib/jsapi/json.rb +41 -0
- data/lib/jsapi/meta/attributes/class_methods.rb +112 -0
- data/lib/jsapi/meta/attributes/type_caster.rb +48 -0
- data/lib/jsapi/meta/attributes.rb +4 -0
- data/lib/jsapi/meta/base.rb +41 -0
- data/lib/jsapi/meta/base_reference.rb +33 -0
- data/lib/jsapi/meta/definitions.rb +226 -0
- data/lib/jsapi/meta/example/model.rb +44 -0
- data/lib/jsapi/meta/example/reference.rb +15 -0
- data/lib/jsapi/meta/example.rb +19 -0
- data/lib/jsapi/meta/existence.rb +69 -0
- data/lib/jsapi/meta/invalid_argument_error.rb +11 -0
- data/lib/jsapi/meta/openapi/callback/model.rb +36 -0
- data/lib/jsapi/meta/openapi/callback/reference.rb +16 -0
- data/lib/jsapi/meta/openapi/callback.rb +21 -0
- data/lib/jsapi/meta/openapi/contact.rb +34 -0
- data/lib/jsapi/meta/openapi/external_documentation.rb +28 -0
- data/lib/jsapi/meta/openapi/info.rb +52 -0
- data/lib/jsapi/meta/openapi/license.rb +28 -0
- data/lib/jsapi/meta/openapi/link/model.rb +48 -0
- data/lib/jsapi/meta/openapi/link/reference.rb +16 -0
- data/lib/jsapi/meta/openapi/link.rb +21 -0
- data/lib/jsapi/meta/openapi/oauth_flow.rb +50 -0
- data/lib/jsapi/meta/openapi/root.rb +134 -0
- data/lib/jsapi/meta/openapi/security_requirement.rb +27 -0
- data/lib/jsapi/meta/openapi/security_scheme/api_key.rb +38 -0
- data/lib/jsapi/meta/openapi/security_scheme/base.rb +16 -0
- data/lib/jsapi/meta/openapi/security_scheme/http/basic.rb +31 -0
- data/lib/jsapi/meta/openapi/security_scheme/http/bearer.rb +37 -0
- data/lib/jsapi/meta/openapi/security_scheme/http/other.rb +37 -0
- data/lib/jsapi/meta/openapi/security_scheme/http.rb +31 -0
- data/lib/jsapi/meta/openapi/security_scheme/oauth2.rb +47 -0
- data/lib/jsapi/meta/openapi/security_scheme/open_id_connect.rb +33 -0
- data/lib/jsapi/meta/openapi/security_scheme.rb +51 -0
- data/lib/jsapi/meta/openapi/server.rb +34 -0
- data/lib/jsapi/meta/openapi/server_variable.rb +34 -0
- data/lib/jsapi/meta/openapi/tag.rb +34 -0
- data/lib/jsapi/meta/openapi/version.rb +41 -0
- data/lib/jsapi/meta/openapi.rb +16 -0
- data/lib/jsapi/meta/operation.rb +186 -0
- data/lib/jsapi/meta/parameter/model.rb +170 -0
- data/lib/jsapi/meta/parameter/reference.rb +30 -0
- data/lib/jsapi/meta/parameter.rb +19 -0
- data/lib/jsapi/meta/property.rb +62 -0
- data/lib/jsapi/meta/reference_error.rb +12 -0
- data/lib/jsapi/meta/request_body/model.rb +65 -0
- data/lib/jsapi/meta/request_body/reference.rb +14 -0
- data/lib/jsapi/meta/request_body.rb +19 -0
- data/lib/jsapi/meta/rescue_handler.rb +26 -0
- data/lib/jsapi/meta/response/model.rb +72 -0
- data/lib/jsapi/meta/response/reference.rb +17 -0
- data/lib/jsapi/meta/response.rb +19 -0
- data/lib/jsapi/meta/schema/array.rb +42 -0
- data/lib/jsapi/meta/schema/base.rb +146 -0
- data/lib/jsapi/meta/schema/boolean.rb +9 -0
- data/lib/jsapi/meta/schema/boundary.rb +37 -0
- data/lib/jsapi/meta/schema/conversion.rb +28 -0
- data/lib/jsapi/meta/schema/delegator.rb +26 -0
- data/lib/jsapi/meta/schema/discriminator.rb +36 -0
- data/lib/jsapi/meta/schema/integer.rb +9 -0
- data/lib/jsapi/meta/schema/number.rb +9 -0
- data/lib/jsapi/meta/schema/numeric.rb +56 -0
- data/lib/jsapi/meta/schema/object.rb +85 -0
- data/lib/jsapi/meta/schema/reference.rb +38 -0
- data/lib/jsapi/meta/schema/string.rb +58 -0
- data/lib/jsapi/meta/schema/validation/base.rb +29 -0
- data/lib/jsapi/meta/schema/validation/enum.rb +26 -0
- data/lib/jsapi/meta/schema/validation/max_items.rb +26 -0
- data/lib/jsapi/meta/schema/validation/max_length.rb +26 -0
- data/lib/jsapi/meta/schema/validation/maximum.rb +51 -0
- data/lib/jsapi/meta/schema/validation/min_items.rb +26 -0
- data/lib/jsapi/meta/schema/validation/min_length.rb +26 -0
- data/lib/jsapi/meta/schema/validation/minimum.rb +51 -0
- data/lib/jsapi/meta/schema/validation/multiple_of.rb +24 -0
- data/lib/jsapi/meta/schema/validation/pattern.rb +30 -0
- data/lib/jsapi/meta/schema/validation.rb +12 -0
- data/lib/jsapi/meta/schema.rb +61 -0
- data/lib/jsapi/meta.rb +23 -0
- data/lib/jsapi/model/attributes.rb +22 -0
- data/lib/jsapi/model/base.rb +34 -0
- data/lib/jsapi/model/error.rb +15 -0
- data/lib/jsapi/model/errors.rb +51 -0
- data/lib/jsapi/model/naming.rb +28 -0
- data/lib/jsapi/model/nestable.rb +37 -0
- data/lib/jsapi/model/nested_error.rb +54 -0
- data/lib/jsapi/model/validations.rb +27 -0
- data/lib/jsapi/model.rb +15 -0
- data/lib/jsapi/version.rb +8 -0
- data/lib/jsapi.rb +8 -0
- metadata +162 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
class Numeric < Base
|
7
|
+
include Conversion
|
8
|
+
|
9
|
+
##
|
10
|
+
# :attr: maximum
|
11
|
+
# The (exclusive) maximum.
|
12
|
+
attribute :maximum, writer: false
|
13
|
+
|
14
|
+
##
|
15
|
+
# :attr: minimum
|
16
|
+
# The (exclusive) minimum.
|
17
|
+
attribute :minimum, writer: false
|
18
|
+
|
19
|
+
##
|
20
|
+
# :attr: multiple_of
|
21
|
+
attribute :multiple_of, writer: false
|
22
|
+
|
23
|
+
def maximum=(value) # :nodoc:
|
24
|
+
boundary = Boundary.from(value)
|
25
|
+
|
26
|
+
add_validation(
|
27
|
+
'maximum',
|
28
|
+
Validation::Maximum.new(
|
29
|
+
boundary.value,
|
30
|
+
exclusive: boundary.exclusive?
|
31
|
+
)
|
32
|
+
)
|
33
|
+
@maximum = boundary
|
34
|
+
end
|
35
|
+
|
36
|
+
def minimum=(value) # :nodoc:
|
37
|
+
boundary = Boundary.from(value)
|
38
|
+
|
39
|
+
add_validation(
|
40
|
+
'minimum',
|
41
|
+
Validation::Minimum.new(
|
42
|
+
boundary.value,
|
43
|
+
exclusive: boundary.exclusive?
|
44
|
+
)
|
45
|
+
)
|
46
|
+
@minimum = boundary
|
47
|
+
end
|
48
|
+
|
49
|
+
def multiple_of=(value) # :nodoc:
|
50
|
+
add_validation('multiple_of', Validation::MultipleOf.new(value))
|
51
|
+
@multiple_of = value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
class Object < Base
|
7
|
+
##
|
8
|
+
# :attr: all_of_references
|
9
|
+
attribute :all_of_references, [Reference], default: []
|
10
|
+
|
11
|
+
alias :all_of= :all_of_references=
|
12
|
+
alias :add_all_of :add_all_of_reference
|
13
|
+
|
14
|
+
##
|
15
|
+
# :attr: discriminator
|
16
|
+
attribute :discriminator, Discriminator
|
17
|
+
|
18
|
+
##
|
19
|
+
# :attr: model
|
20
|
+
# The model class to access nested object parameters by. The default
|
21
|
+
# model class is Model::Base.
|
22
|
+
attribute :model, Class, default: Model::Base
|
23
|
+
|
24
|
+
##
|
25
|
+
# :attr: properties
|
26
|
+
# The properties.
|
27
|
+
attribute :properties, { String => Property }, writer: false, default: {}
|
28
|
+
|
29
|
+
def add_property(name, keywords = {}) # :nodoc:
|
30
|
+
(@properties ||= {})[name.to_s] = Property.new(name, **keywords)
|
31
|
+
end
|
32
|
+
|
33
|
+
def resolve_properties(access, definitions)
|
34
|
+
properties = merge_properties(definitions, [])
|
35
|
+
|
36
|
+
case access
|
37
|
+
when :read
|
38
|
+
properties.reject { |_k, v| v.write_only? }
|
39
|
+
when :write
|
40
|
+
properties.reject { |_k, v| v.read_only? }
|
41
|
+
else
|
42
|
+
properties
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_json_schema # :nodoc:
|
47
|
+
super.merge(
|
48
|
+
allOf: all_of_references.map(&:to_json_schema).presence,
|
49
|
+
properties: properties.transform_values(&:to_json_schema),
|
50
|
+
required: properties.values.select(&:required?).map(&:name)
|
51
|
+
).compact
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_openapi(version) # :nodoc:
|
55
|
+
super.merge(
|
56
|
+
allOf: all_of_references.map do |schema|
|
57
|
+
schema.to_openapi(version)
|
58
|
+
end.presence,
|
59
|
+
discriminator: discriminator&.to_openapi(version),
|
60
|
+
properties: properties.transform_values do |property|
|
61
|
+
property.to_openapi(version)
|
62
|
+
end,
|
63
|
+
required: properties.values.select(&:required?).map(&:name)
|
64
|
+
).compact
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
|
69
|
+
def merge_properties(definitions, path)
|
70
|
+
return properties unless all_of_references.present?
|
71
|
+
|
72
|
+
{}.tap do |properties|
|
73
|
+
all_of_references.each do |reference|
|
74
|
+
schema = reference.resolve(definitions)
|
75
|
+
raise "circular reference: #{reference.schema}" if schema.in?(path)
|
76
|
+
|
77
|
+
properties.merge!(schema.merge_properties(definitions, path + [self]))
|
78
|
+
end
|
79
|
+
properties.merge!(self.properties)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
class Reference < BaseReference
|
7
|
+
alias :schema :ref
|
8
|
+
alias :schema= :ref=
|
9
|
+
|
10
|
+
##
|
11
|
+
# :attr: existence
|
12
|
+
# The level of Existence. The default level of existence
|
13
|
+
# is +ALLOW_OMITTED+.
|
14
|
+
attribute :existence, Existence, default: Existence::ALLOW_OMITTED
|
15
|
+
|
16
|
+
def resolve(definitions) # :nodoc:
|
17
|
+
schema = super
|
18
|
+
return schema if existence < Existence::ALLOW_EMPTY
|
19
|
+
|
20
|
+
Delegator.new(schema, [existence, schema.existence].max)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns a hash representing the \JSON \Schema reference object.
|
24
|
+
def to_json_schema
|
25
|
+
{ '$ref': "#/definitions/#{ref}" }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns a hash representing the \OpenAPI reference object.
|
29
|
+
def to_openapi(version)
|
30
|
+
version = OpenAPI::Version.from(version)
|
31
|
+
path = version.major == 2 ? 'definitions' : 'components/schemas'
|
32
|
+
|
33
|
+
{ '$ref': "#/#{path}/#{ref}" }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
class String < Base
|
7
|
+
include Conversion
|
8
|
+
|
9
|
+
##
|
10
|
+
# :attr: format
|
11
|
+
# The optional format of a string. Possible values are:
|
12
|
+
#
|
13
|
+
# - <code>"date"</code>
|
14
|
+
# - <code>"date-time"</code>
|
15
|
+
#
|
16
|
+
attribute :format, ::String, values: %w[date date-time]
|
17
|
+
|
18
|
+
##
|
19
|
+
# :attr: max_length
|
20
|
+
# The maximum length of a string.
|
21
|
+
attribute :max_length, writer: false
|
22
|
+
|
23
|
+
##
|
24
|
+
# :attr: min_length
|
25
|
+
# The minimum length of a string.
|
26
|
+
attribute :min_length, writer: false
|
27
|
+
|
28
|
+
##
|
29
|
+
# :attr: pattern
|
30
|
+
# The regular expression a string must match.
|
31
|
+
attribute :pattern, writer: false
|
32
|
+
|
33
|
+
def max_length=(value) # :nodoc:
|
34
|
+
add_validation('max_length', Validation::MaxLength.new(value))
|
35
|
+
@max_length = value
|
36
|
+
end
|
37
|
+
|
38
|
+
def min_length=(value) # :nodoc:
|
39
|
+
add_validation('min_length', Validation::MinLength.new(value))
|
40
|
+
@min_length = value
|
41
|
+
end
|
42
|
+
|
43
|
+
def pattern=(value) # :nodoc:
|
44
|
+
add_validation('pattern', Validation::Pattern.new(value))
|
45
|
+
@pattern = value
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_json_schema # :nodoc:
|
49
|
+
format ? super.merge(format: format) : super
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_openapi(*) # :nodoc:
|
53
|
+
format ? super.merge(format: format) : super
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class Base
|
8
|
+
def self.keyword
|
9
|
+
@keyword ||= name.demodulize.camelize(:lower).to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :value
|
13
|
+
|
14
|
+
def initialize(value)
|
15
|
+
@value = value
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_json_schema_validation
|
19
|
+
{ self.class.keyword => value }
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_openapi_validation(*)
|
23
|
+
to_json_schema_validation
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class Enum < Base
|
8
|
+
def initialize(value)
|
9
|
+
unless value.respond_to?(:include?)
|
10
|
+
raise ArgumentError, "invalid enum: #{value.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(value, errors)
|
17
|
+
return true if self.value.include?(value)
|
18
|
+
|
19
|
+
errors.add(:base, :inclusion)
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class MaxItems < Base
|
8
|
+
def initialize(value)
|
9
|
+
unless value.respond_to?(:<=)
|
10
|
+
raise ArgumentError, "invalid max items: #{value.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(value, errors)
|
17
|
+
return true if value.size <= self.value
|
18
|
+
|
19
|
+
errors.add(:base, :invalid)
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class MaxLength < Base
|
8
|
+
def initialize(value)
|
9
|
+
unless value.respond_to?(:<=)
|
10
|
+
raise ArgumentError, "invalid max length: #{value.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(value, errors)
|
17
|
+
return true if value.to_s.length <= self.value
|
18
|
+
|
19
|
+
errors.add(:base, :too_long, count: self.value)
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class Maximum < Base
|
8
|
+
attr_reader :exclusive
|
9
|
+
|
10
|
+
def initialize(value, exclusive: false)
|
11
|
+
if exclusive
|
12
|
+
raise ArgumentError, "invalid exclusive maximum: #{value.inspect}" unless value.respond_to?(:<)
|
13
|
+
else
|
14
|
+
raise ArgumentError, "invalid maximum: #{value.inspect}" unless value.respond_to?(:<=)
|
15
|
+
end
|
16
|
+
|
17
|
+
super(value)
|
18
|
+
@exclusive = exclusive
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate(value, errors)
|
22
|
+
if exclusive
|
23
|
+
return true if value < self.value
|
24
|
+
|
25
|
+
errors.add(:base, :less_than, count: self.value)
|
26
|
+
else
|
27
|
+
return true if value <= self.value
|
28
|
+
|
29
|
+
errors.add(:base, :less_than_or_equal_to, count: self.value)
|
30
|
+
end
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json_schema_validation
|
35
|
+
return super unless exclusive
|
36
|
+
|
37
|
+
{ exclusiveMaximum: value }
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_openapi_validation(version)
|
41
|
+
version = OpenAPI::Version.from(version)
|
42
|
+
return to_json_schema_validation if version.major == 3 && version.minor == 1
|
43
|
+
return super unless exclusive
|
44
|
+
|
45
|
+
{ maximum: value, exclusiveMaximum: true }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class MinItems < Base
|
8
|
+
def initialize(value)
|
9
|
+
unless value.respond_to?(:>=)
|
10
|
+
raise ArgumentError, "invalid min items: #{value.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(value, errors)
|
17
|
+
return true if value.size >= self.value
|
18
|
+
|
19
|
+
errors.add(:base, :invalid)
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class MinLength < Base
|
8
|
+
def initialize(value)
|
9
|
+
unless value.respond_to?(:>=)
|
10
|
+
raise ArgumentError, "invalid min length: #{value.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(value, errors)
|
17
|
+
return true if value.to_s.length >= self.value
|
18
|
+
|
19
|
+
errors.add(:base, :too_short, count: self.value)
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class Minimum < Base
|
8
|
+
attr_reader :exclusive
|
9
|
+
|
10
|
+
def initialize(value, exclusive: false)
|
11
|
+
if exclusive
|
12
|
+
raise ArgumentError, "invalid exclusive minimum: #{value.inspect}" unless value.respond_to?(:>)
|
13
|
+
else
|
14
|
+
raise ArgumentError, "invalid minimum: #{value.inspect}" unless value.respond_to?(:>=)
|
15
|
+
end
|
16
|
+
|
17
|
+
super(value)
|
18
|
+
@exclusive = exclusive
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate(value, errors)
|
22
|
+
if exclusive
|
23
|
+
return true if value > self.value
|
24
|
+
|
25
|
+
errors.add(:base, :greater_than, count: self.value)
|
26
|
+
else
|
27
|
+
return true if value >= self.value
|
28
|
+
|
29
|
+
errors.add(:base, :greater_than_or_equal_to, count: self.value)
|
30
|
+
end
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json_schema_validation
|
35
|
+
return super unless exclusive
|
36
|
+
|
37
|
+
{ exclusiveMinimum: value }
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_openapi_validation(version)
|
41
|
+
version = OpenAPI::Version.from(version)
|
42
|
+
return to_json_schema_validation if version.major == 3 && version.minor == 1
|
43
|
+
return super unless exclusive
|
44
|
+
|
45
|
+
{ minimum: value, exclusiveMinimum: true }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class MultipleOf < Base
|
8
|
+
def initialize(value)
|
9
|
+
raise ArgumentError, "invalid multiple of: #{value.inspect}" unless value.respond_to?(:%)
|
10
|
+
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(value, errors)
|
15
|
+
return true if (value % self.value).zero?
|
16
|
+
|
17
|
+
errors.add(:base, :invalid)
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Meta
|
5
|
+
module Schema
|
6
|
+
module Validation
|
7
|
+
class Pattern < Base
|
8
|
+
def initialize(value)
|
9
|
+
unless value.is_a?(Regexp)
|
10
|
+
raise ArgumentError, "invalid pattern: #{value.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(value, errors)
|
17
|
+
return true if value.to_s.match?(self.value)
|
18
|
+
|
19
|
+
errors.add(:base, :invalid)
|
20
|
+
false
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_json_schema_validation
|
24
|
+
{ pattern: value.source }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'validation/base'
|
4
|
+
require_relative 'validation/enum'
|
5
|
+
require_relative 'validation/max_items'
|
6
|
+
require_relative 'validation/max_length'
|
7
|
+
require_relative 'validation/maximum'
|
8
|
+
require_relative 'validation/min_items'
|
9
|
+
require_relative 'validation/min_length'
|
10
|
+
require_relative 'validation/minimum'
|
11
|
+
require_relative 'validation/multiple_of'
|
12
|
+
require_relative 'validation/pattern'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'schema/conversion'
|
4
|
+
require_relative 'schema/boundary'
|
5
|
+
require_relative 'schema/delegator'
|
6
|
+
require_relative 'schema/reference'
|
7
|
+
require_relative 'schema/discriminator'
|
8
|
+
require_relative 'schema/base'
|
9
|
+
require_relative 'schema/boolean'
|
10
|
+
require_relative 'schema/array'
|
11
|
+
require_relative 'schema/numeric'
|
12
|
+
require_relative 'schema/integer'
|
13
|
+
require_relative 'schema/number'
|
14
|
+
require_relative 'schema/object'
|
15
|
+
require_relative 'schema/string'
|
16
|
+
require_relative 'schema/validation'
|
17
|
+
|
18
|
+
module Jsapi
|
19
|
+
module Meta
|
20
|
+
module Schema
|
21
|
+
class << self
|
22
|
+
# Creates a new schema model or reference. The +:type+ keyword determines
|
23
|
+
# the type of the schema to be created. Possible types are:
|
24
|
+
#
|
25
|
+
# - <code>"array"</code>
|
26
|
+
# - <code>"boolean"</code>
|
27
|
+
# - <code>"integer"</code>
|
28
|
+
# - <code>"number"</code>
|
29
|
+
# - <code>"object"</code>
|
30
|
+
# - <code>"string"</code>
|
31
|
+
#
|
32
|
+
# The default type is <code>"object"</code>.
|
33
|
+
#
|
34
|
+
# Raises an InvalidArgumentError if the given type is invalid.
|
35
|
+
def new(keywords = {})
|
36
|
+
if keywords.key?(:ref) || keywords.key?(:schema)
|
37
|
+
return Reference.new(keywords)
|
38
|
+
end
|
39
|
+
|
40
|
+
type = keywords[:type]
|
41
|
+
case type&.to_s
|
42
|
+
when 'array'
|
43
|
+
Array
|
44
|
+
when 'boolean'
|
45
|
+
Boolean
|
46
|
+
when 'integer'
|
47
|
+
Integer
|
48
|
+
when 'number'
|
49
|
+
Number
|
50
|
+
when 'object', nil
|
51
|
+
Object
|
52
|
+
when 'string'
|
53
|
+
String
|
54
|
+
else
|
55
|
+
raise InvalidArgumentError.new('type', type, Base::TYPES)
|
56
|
+
end.new(keywords.except(:type))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/jsapi/meta.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'meta/invalid_argument_error'
|
4
|
+
require_relative 'meta/reference_error'
|
5
|
+
require_relative 'meta/attributes'
|
6
|
+
require_relative 'meta/base'
|
7
|
+
require_relative 'meta/base_reference'
|
8
|
+
require_relative 'meta/openapi'
|
9
|
+
require_relative 'meta/example'
|
10
|
+
require_relative 'meta/existence'
|
11
|
+
require_relative 'meta/property'
|
12
|
+
require_relative 'meta/schema'
|
13
|
+
require_relative 'meta/request_body'
|
14
|
+
require_relative 'meta/parameter'
|
15
|
+
require_relative 'meta/response'
|
16
|
+
require_relative 'meta/operation'
|
17
|
+
require_relative 'meta/rescue_handler'
|
18
|
+
require_relative 'meta/definitions'
|
19
|
+
|
20
|
+
module Jsapi
|
21
|
+
# The meta model.
|
22
|
+
module Meta end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Jsapi
|
4
|
+
module Model
|
5
|
+
module Attributes
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
delegate :[], :attribute?, :attributes, to: :nested
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(*args) # :nodoc:
|
13
|
+
name = args.first
|
14
|
+
attribute?(name) ? self[name] : super
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_to_missing?(param1, _param2) # :nodoc:
|
18
|
+
attribute?(param1) ? true : super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|