boto-rb 0.7.0

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.
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Types
5
+ # Restrictions checked after coercion. Options are validated and messages built once, in initialize.
6
+ class Constraints
7
+ Rule = Data.define(:keywords, :min_message, :max_message)
8
+
9
+ RULES = {
10
+ number: Rule.new(
11
+ keywords: %w[minimum maximum].freeze,
12
+ min_message: "must be greater than or equal to %s",
13
+ max_message: "must be less than or equal to %s"
14
+ ),
15
+ characters: Rule.new(
16
+ keywords: %w[minLength maxLength].freeze,
17
+ min_message: "must have at least %s characters",
18
+ max_message: "must have at most %s characters"
19
+ ),
20
+ items: Rule.new(
21
+ keywords: %w[minItems maxItems].freeze,
22
+ min_message: "must have at least %s items",
23
+ max_message: "must have at most %s items"
24
+ )
25
+ }.freeze
26
+
27
+ PATTERN_MESSAGE = "must match the pattern"
28
+ ANCHOR_START = '\A'
29
+ ANCHOR_END = '\z'
30
+ JSON_ANCHOR_START = "^"
31
+ JSON_ANCHOR_END = "$"
32
+
33
+ def initialize(unit:, min: nil, max: nil, pattern: nil, enum: nil)
34
+ @unit = unit
35
+ @rule = RULES[unit]
36
+ validate_bounds!(min, max)
37
+ validate_pattern!(pattern)
38
+ @min = min
39
+ @max = max
40
+ @min_message = min && format(@rule.min_message, min)
41
+ @max_message = max && format(@rule.max_message, max)
42
+ @pattern = pattern
43
+ @enum = enum
44
+ @json_keywords = build_json_keywords.freeze
45
+ freeze
46
+ end
47
+
48
+ attr_reader :json_keywords
49
+
50
+ def violation(value)
51
+ bound_violation(value) || enum_violation(value) || pattern_violation(value)
52
+ end
53
+
54
+ private
55
+
56
+ def validate_bounds!(min, max)
57
+ return if min.nil? && max.nil?
58
+ raise ArgumentError, "min and max are not supported for this type" if @rule.nil?
59
+ return if [min, max].all? { |bound| bound.nil? || bound.is_a?(Numeric) }
60
+
61
+ raise ArgumentError, "min and max must be Numeric"
62
+ end
63
+
64
+ def validate_pattern!(pattern)
65
+ return if pattern.nil?
66
+ raise ArgumentError, "pattern is only supported for String" unless @unit == :characters
67
+ raise ArgumentError, "pattern must be a Regexp" unless pattern.is_a?(Regexp)
68
+ return if anchored?(pattern.source)
69
+
70
+ raise ArgumentError, "pattern must be anchored with \\A and \\z"
71
+ end
72
+
73
+ def anchored?(source)
74
+ source.start_with?(ANCHOR_START) && source.end_with?(ANCHOR_END)
75
+ end
76
+
77
+ def bound_violation(value)
78
+ return unless @min || @max
79
+
80
+ size = measure(value)
81
+ return @min_message if @min && size < @min
82
+
83
+ @max_message if @max && size > @max
84
+ end
85
+
86
+ def enum_violation(value)
87
+ return if @enum.nil? || @enum.values.include?(value)
88
+
89
+ @enum.message
90
+ end
91
+
92
+ def pattern_violation(value)
93
+ return if @pattern.nil? || @pattern.match?(value)
94
+
95
+ PATTERN_MESSAGE
96
+ end
97
+
98
+ def measure(value)
99
+ case @unit
100
+ when :characters then value.length
101
+ when :items then value.size
102
+ else value
103
+ end
104
+ end
105
+
106
+ def build_json_keywords
107
+ keywords = {}
108
+ keywords[@rule.keywords.first] = @min if @min
109
+ keywords[@rule.keywords.last] = @max if @max
110
+ keywords["pattern"] = json_pattern if @pattern
111
+ keywords["enum"] = @enum.json if @enum
112
+ keywords
113
+ end
114
+
115
+ def json_pattern
116
+ body = @pattern.source.delete_prefix(ANCHOR_START).delete_suffix(ANCHOR_END)
117
+ "#{JSON_ANCHOR_START}#{body}#{JSON_ANCHOR_END}".freeze
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Types
5
+ # Allowed values of a scalar type. Values are coerced once, at build time.
6
+ class Enum
7
+ attr_reader :values, :json, :message
8
+
9
+ def initialize(options, definition)
10
+ raise ArgumentError, "enum must be a non-empty Array" unless options.is_a?(Array) && !options.empty?
11
+
12
+ @values = options.map { |option| coerce_option(option, definition) }.freeze
13
+ @json = @values.map(&definition.dumper).freeze
14
+ @message = "must be one of: #{@json.join(', ')}".freeze
15
+ freeze
16
+ end
17
+
18
+ private
19
+
20
+ def coerce_option(option, definition)
21
+ coerced = definition.coercer.call(option)
22
+ raise ArgumentError, "enum values must match the type" if coerced.nil?
23
+
24
+ coerced
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Types
5
+ # Raised when a value does not match its type. Each error is { path:, message: }.
6
+ class Invalid < StandardError
7
+ SEPARATOR = "; "
8
+
9
+ attr_reader :errors
10
+
11
+ def self.single(message)
12
+ new([{ path: "", message: }])
13
+ end
14
+
15
+ def initialize(errors)
16
+ @errors = errors.freeze
17
+ super(errors.map { |error| format_error(error) }.join(SEPARATOR))
18
+ end
19
+
20
+ private
21
+
22
+ def format_error(error)
23
+ error[:path].empty? ? error[:message] : "#{error[:path]}: #{error[:message]}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Types
5
+ # A list of one item type. Item errors are prefixed with their index and aggregated.
6
+ class List
7
+ MUST_BE_ARRAY = "must be an array"
8
+
9
+ attr_reader :json_schema
10
+
11
+ def self.build(type, min:, max:, enum:, pattern:)
12
+ Types.reject_options!("lists", enum:, pattern:)
13
+ raise ArgumentError, "list type must have exactly one item type" unless type.size == 1
14
+
15
+ new(Types.build(type.first), Constraints.new(unit: :items, min:, max:))
16
+ end
17
+
18
+ def initialize(item, constraints)
19
+ @item = item
20
+ @constraints = constraints
21
+ @json_schema = { "type" => "array", "items" => item.json_schema }.merge(constraints.json_keywords).freeze
22
+ freeze
23
+ end
24
+
25
+ def coerce(value)
26
+ raise Invalid.single(MUST_BE_ARRAY) unless value.is_a?(Array)
27
+
28
+ errors = []
29
+ coerced = value.each_with_index.map { |item, index| coerce_item(item, index, errors) }
30
+ raise Invalid.new(errors) if errors.any?
31
+
32
+ message = @constraints.violation(coerced)
33
+ raise Invalid.single(message) if message
34
+
35
+ coerced
36
+ end
37
+
38
+ def dump(value)
39
+ value.map { |item| @item.dump(item) }
40
+ end
41
+
42
+ private
43
+
44
+ def coerce_item(item, index, errors)
45
+ @item.coerce(item)
46
+ rescue Invalid => e
47
+ errors.concat(e.errors.map { |error| prefix_error(error, index) })
48
+ nil
49
+ end
50
+
51
+ def prefix_error(error, index)
52
+ error.merge(path: Types.join_path("[#{index}]", error[:path]))
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Types
5
+ # A scalar type: coercion, restrictions, dump and JSON Schema, driven by a DEFINITIONS entry.
6
+ class Scalar
7
+ Definition = Data.define(:coercer, :message, :json, :dumper, :unit)
8
+
9
+ IDENTITY = ->(value) { value }
10
+ DATE_DUMPER = ->(value) { value.iso8601 }
11
+ TIME_DUMPER = ->(value) { value.iso8601(3) }
12
+ STRING_JSON = { "type" => "string" }.freeze
13
+ DATE_JSON = { "type" => "string", "format" => "date" }.freeze
14
+ TIME_JSON = { "type" => "string", "format" => "date-time" }.freeze
15
+
16
+ DEFINITIONS = {
17
+ String => Definition.new(
18
+ coercer: Coercion.method(:string), message: "must be a String",
19
+ json: STRING_JSON, dumper: IDENTITY, unit: :characters
20
+ ),
21
+ Integer => Definition.new(
22
+ coercer: Coercion.method(:integer), message: "must be an Integer",
23
+ json: { "type" => "integer" }.freeze, dumper: IDENTITY, unit: :number
24
+ ),
25
+ Float => Definition.new(
26
+ coercer: Coercion.method(:float), message: "must be a Float",
27
+ json: { "type" => "number" }.freeze, dumper: IDENTITY, unit: :number
28
+ ),
29
+ Boto::Boolean => Definition.new(
30
+ coercer: Coercion.method(:boolean), message: "must be a boolean",
31
+ json: { "type" => "boolean" }.freeze, dumper: IDENTITY, unit: nil
32
+ ),
33
+ Date => Definition.new(
34
+ coercer: Coercion.method(:date), message: "must be a date (YYYY-MM-DD)",
35
+ json: DATE_JSON, dumper: DATE_DUMPER, unit: nil
36
+ ),
37
+ Time => Definition.new(
38
+ coercer: Coercion.method(:time), message: "must be a date-time (ISO 8601 with time zone)",
39
+ json: TIME_JSON, dumper: TIME_DUMPER, unit: nil
40
+ )
41
+ }.freeze
42
+
43
+ attr_reader :json_schema
44
+
45
+ def self.build(definition, min: nil, max: nil, enum: nil, pattern: nil)
46
+ enum_rules = enum.nil? ? nil : Enum.new(enum, definition)
47
+ constraints = Constraints.new(unit: definition.unit, min:, max:, pattern:, enum: enum_rules)
48
+ new(definition, constraints)
49
+ end
50
+
51
+ def initialize(definition, constraints)
52
+ @definition = definition
53
+ @constraints = constraints
54
+ @json_schema = definition.json.merge(constraints.json_keywords).freeze
55
+ freeze
56
+ end
57
+
58
+ def coerce(value)
59
+ coerced = @definition.coercer.call(value)
60
+ raise Invalid.single(@definition.message) if coerced.nil?
61
+
62
+ message = @constraints.violation(coerced)
63
+ raise Invalid.single(message) if message
64
+
65
+ coerced
66
+ end
67
+
68
+ def dump(value)
69
+ @definition.dumper.call(value)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ module Types
5
+ # Adapter for a schema class (duck typed: coerce, serialize, json_schema_ref).
6
+ class SchemaRef
7
+ SCHEMA_METHODS = %i[coerce serialize json_schema_ref].freeze
8
+
9
+ attr_reader :json_schema
10
+
11
+ def self.schema?(type)
12
+ type.is_a?(Class) && SCHEMA_METHODS.all? { |name| type.respond_to?(name) }
13
+ end
14
+
15
+ def self.build(schema, min:, max:, enum:, pattern:)
16
+ Types.reject_options!("schema types", min:, max:, enum:, pattern:)
17
+ new(schema)
18
+ end
19
+
20
+ def initialize(schema)
21
+ @schema = schema
22
+ @json_schema = frozen_copy(schema.json_schema_ref)
23
+ freeze
24
+ end
25
+
26
+ def coerce(value)
27
+ @schema.coerce(value)
28
+ end
29
+
30
+ def dump(value)
31
+ @schema.serialize(value)
32
+ end
33
+
34
+ private
35
+
36
+ # Copies the schema's hash so the caller's object is not frozen as a side effect.
37
+ def frozen_copy(value)
38
+ case value
39
+ when Hash then value.to_h { |key, item| [key, frozen_copy(item)] }.freeze
40
+ when Array then value.map { |item| frozen_copy(item) }.freeze
41
+ else value.dup.freeze
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/boto/types.rb ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ # Shared param types: coercion, restrictions, serialization and JSON Schema.
5
+ module Types
6
+ PATH_SEPARATOR = "."
7
+ INDEX_OPEN = "["
8
+
9
+ def self.build(type, min: nil, max: nil, enum: nil, pattern: nil)
10
+ definition = Scalar::DEFINITIONS[type]
11
+ return Scalar.build(definition, min:, max:, enum:, pattern:) if definition
12
+ return List.build(type, min:, max:, enum:, pattern:) if type.is_a?(Array)
13
+ return SchemaRef.build(type, min:, max:, enum:, pattern:) if SchemaRef.schema?(type)
14
+
15
+ raise ArgumentError, "unsupported type #{type.inspect}"
16
+ end
17
+
18
+ def self.join_path(prefix, path)
19
+ return prefix if path.empty?
20
+ return path if prefix.empty?
21
+
22
+ path.start_with?(INDEX_OPEN) ? "#{prefix}#{path}" : "#{prefix}#{PATH_SEPARATOR}#{path}"
23
+ end
24
+
25
+ def self.reject_options!(kind, **options)
26
+ options.each do |name, value|
27
+ raise ArgumentError, "#{name} is not supported for #{kind}" unless value.nil?
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boto
4
+ VERSION = "0.7.0"
5
+ end
data/lib/boto.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.inflector.inflect("openapi" => "OpenAPI")
6
+ # Area folders group files on disk without adding a namespace (Boto::Router stays Boto::Router).
7
+ %w[http endpoint solution].each { |area| loader.collapse("#{__dir__}/boto/#{area}") }
8
+ loader.setup
9
+
10
+ module Boto
11
+ # Called from config/app.rb. Yields the configuration of the project being booted and returns it.
12
+ def self.configure
13
+ config = Configuration.capture
14
+ yield config
15
+ config
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boto-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - Evo Tech Builder
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rack
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '3.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '3.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: zeitwerk
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.7'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.7'
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - LICENSE
45
+ - lib/boto.rb
46
+ - lib/boto/configuration.rb
47
+ - lib/boto/endpoint/boolean.rb
48
+ - lib/boto/endpoint/endpoint.rb
49
+ - lib/boto/endpoint/params.rb
50
+ - lib/boto/http/app.rb
51
+ - lib/boto/http/error.rb
52
+ - lib/boto/http/problem.rb
53
+ - lib/boto/http/request_body.rb
54
+ - lib/boto/http/router.rb
55
+ - lib/boto/openapi.rb
56
+ - lib/boto/openapi/operation.rb
57
+ - lib/boto/openapi/schema_catalog.rb
58
+ - lib/boto/plugin.rb
59
+ - lib/boto/plugin/stack.rb
60
+ - lib/boto/schema.rb
61
+ - lib/boto/schema/compiler.rb
62
+ - lib/boto/solution/boot.rb
63
+ - lib/boto/solution/botofile.rb
64
+ - lib/boto/solution/isolation.rb
65
+ - lib/boto/solution/isolation/scanner.rb
66
+ - lib/boto/solution/isolation_error.rb
67
+ - lib/boto/solution/project.rb
68
+ - lib/boto/solution/project_loader.rb
69
+ - lib/boto/solution/solution.rb
70
+ - lib/boto/types.rb
71
+ - lib/boto/types/coercion.rb
72
+ - lib/boto/types/constraints.rb
73
+ - lib/boto/types/enum.rb
74
+ - lib/boto/types/invalid.rb
75
+ - lib/boto/types/list.rb
76
+ - lib/boto/types/scalar.rb
77
+ - lib/boto/types/schema_ref.rb
78
+ - lib/boto/version.rb
79
+ homepage: https://github.com/EvoTechBuilder/boto-rb
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ rubygems_mfa_required: 'true'
84
+ source_code_uri: https://github.com/EvoTechBuilder/boto-rb
85
+ changelog_uri: https://github.com/EvoTechBuilder/boto-rb/blob/main/CHANGELOG.md
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '3.4'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 4.0.20
101
+ specification_version: 4
102
+ summary: Ruby framework for APIs and microservices organized as solutions.
103
+ test_files: []