Class: Explicit::Type::Record
Instance Attribute Summary collapse
#auth_type, #default, #description, #nilable, #param_location
Instance Method Summary
collapse
#auth_basic?, #auth_bearer?, build, #error_i18n, #mcp_schema, #merge_base_json_schema, #param_location_body?, #param_location_path?, #param_location_query?, #required?, #swagger_i18n, #swagger_schema
Constructor Details
#initialize(attributes:) ⇒ Record
Returns a new instance of Record.
6
7
8
9
10
11
12
|
# File 'lib/explicit/type/record.rb', line 6
def initialize(attributes:)
@attributes = attributes.to_h do |attribute_name, type|
type = Explicit::Type.build(type) if !type.is_a?(Explicit::Type)
[ attribute_name, type ]
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
4
5
6
|
# File 'lib/explicit/type/record.rb', line 4
def attributes
@attributes
end
|
Instance Method Details
#body_params_type ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/explicit/type/record.rb', line 54
def body_params_type
body_params = @attributes.filter do |name, type|
type.param_location_body?
end
self.class.new(attributes: body_params)
end
|
#json_schema(flavour) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/explicit/type/record.rb', line 76
def json_schema(flavour)
properties = attributes.to_h do |name, type|
[ name, flavour == :mcp ? type.mcp_schema : type.swagger_schema ]
end
required = attributes.filter_map do |name, type|
type.required? ? name.to_s : nil
end
{
type: "object",
properties:,
required:,
additionalProperties: false
}.compact
end
|
#path_params_type ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/explicit/type/record.rb', line 38
def path_params_type
path_params = @attributes.filter do |name, type|
type.param_location_path?
end
self.class.new(attributes: path_params)
end
|
#query_params_type ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/explicit/type/record.rb', line 46
def query_params_type
query_params = @attributes.filter do |name, type|
type.param_location_query?
end
self.class.new(attributes: query_params)
end
|
#validate(data) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/explicit/type/record.rb', line 14
def validate(data)
if !data.respond_to?(:[]) || data.is_a?(::String) || data.is_a?(::Array)
return error_i18n("hash")
end
validated_data = {}
errors = {}
@attributes.each do |attribute_name, type|
value = data[attribute_name]
case type.validate(value)
in [:ok, validated_value]
validated_data[attribute_name] = validated_value
in [:error, err]
errors[attribute_name] = err
end
end
return [ :error, errors ] if errors.any?
[ :ok, validated_data ]
end
|