blueprinter_schema 1.3.0 → 1.5.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.
- checksums.yaml +4 -4
- data/README.md +44 -1
- data/lib/blueprinter_schema/generator.rb +35 -12
- data/lib/blueprinter_schema/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 392b3344dd71f597ca8c1bfc35e1e3e47c7d01417ba38cd5bf170d6f7e63f1cc
|
|
4
|
+
data.tar.gz: 7aadb18ef24453084f6f23fcb3b0e681c13b7d82b5e12c2d56572e3365f13e3f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 96b8ebe6804c446804b240e1962f223e9130990144421217139e019c1db8e12a891100b1cdb37423e67bc27afd38eb85e1106103a02439032a527511f701d2f0
|
|
7
|
+
data.tar.gz: 0e244fdb17fac6bb6d1ac367f77d97511d5736625850f2a55189dd3da58cceb114dd485b8d7a8f25eab79fd4c79b85339e0128334681cc1100fb07124febe3c7
|
data/README.md
CHANGED
|
@@ -159,7 +159,7 @@ BlueprinterSchema.generate(serializer: UserSerializer)
|
|
|
159
159
|
"additionalProperties" => false
|
|
160
160
|
}
|
|
161
161
|
},
|
|
162
|
-
"required" => ["email", "addresses", "profile"],
|
|
162
|
+
"required" => ["email", "addresses", "profile", "account"],
|
|
163
163
|
"additionalProperties" => false
|
|
164
164
|
}
|
|
165
165
|
```
|
|
@@ -177,6 +177,49 @@ BlueprinterSchema.generate(
|
|
|
177
177
|
)
|
|
178
178
|
```
|
|
179
179
|
|
|
180
|
+
### Active Model Support
|
|
181
|
+
|
|
182
|
+
```rb
|
|
183
|
+
class Restrictions
|
|
184
|
+
include ActiveModel::Model
|
|
185
|
+
include ActiveModel::Attributes
|
|
186
|
+
|
|
187
|
+
attribute :min_units, :integer
|
|
188
|
+
attribute :max_units, :integer
|
|
189
|
+
attribute :label, :string
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
class RestrictionsSerializer < Blueprinter::Base
|
|
193
|
+
field :min_units
|
|
194
|
+
field :max_units
|
|
195
|
+
field :label, type: [:string, :null]
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
BlueprinterSchema.generate(serializer: RestrictionsSerializer, model: Restrictions)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```rb
|
|
202
|
+
{
|
|
203
|
+
"type" => "object",
|
|
204
|
+
"title" => "Restrictions",
|
|
205
|
+
"properties" => {
|
|
206
|
+
"min_units" => {
|
|
207
|
+
"type" => "integer"
|
|
208
|
+
},
|
|
209
|
+
"max_units" => {
|
|
210
|
+
"type" => "integer"
|
|
211
|
+
},
|
|
212
|
+
"label" => {
|
|
213
|
+
"type" => ["string", "null"]
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
"required" => ["max_units", "min_units", "label"],
|
|
217
|
+
"additionalProperties" => false
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Note: Attributes are assumed to be non-null. If an attribute is nullable, specify it explicitly in the serializer.
|
|
222
|
+
|
|
180
223
|
## Development
|
|
181
224
|
|
|
182
225
|
Devcontainer / Codespaces / Native
|
|
@@ -58,10 +58,21 @@ module BlueprinterSchema
|
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
def build_required_fields
|
|
61
|
+
(required_field_names + required_association_names).map(&:to_s)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def required_field_names
|
|
61
65
|
fields
|
|
62
66
|
.reject { |_, field| field.options[:exclude_if_nil] }
|
|
63
67
|
.reject { |_, field| skip_field?(field) }
|
|
64
|
-
.keys
|
|
68
|
+
.keys
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def required_association_names
|
|
72
|
+
associations
|
|
73
|
+
.reject { |_, association| association.options[:exclude_if_nil] }
|
|
74
|
+
.reject { |_, association| skip_field?(association) }
|
|
75
|
+
.keys
|
|
65
76
|
end
|
|
66
77
|
|
|
67
78
|
def field_to_json_schema(field)
|
|
@@ -70,13 +81,23 @@ module BlueprinterSchema
|
|
|
70
81
|
if field.options[:type]
|
|
71
82
|
type_definition['type'] = ensure_valid_json_schema_types!(field)
|
|
72
83
|
elsif @model
|
|
73
|
-
|
|
74
|
-
type_definition = ar_column_to_json_schema(column)
|
|
84
|
+
type_definition = model_attribute_to_json_schema(field.name.to_s)
|
|
75
85
|
end
|
|
76
86
|
|
|
77
87
|
merge_field_options(type_definition, field.options)
|
|
78
88
|
end
|
|
79
89
|
|
|
90
|
+
def model_attribute_to_json_schema(name)
|
|
91
|
+
if @model.respond_to?(:columns_hash)
|
|
92
|
+
column = @model.columns_hash[name]
|
|
93
|
+
type_to_json_schema(column&.type, column&.null)
|
|
94
|
+
elsif @model.respond_to?(:type_for_attribute)
|
|
95
|
+
type_to_json_schema(@model.type_for_attribute(name)&.type, false)
|
|
96
|
+
else
|
|
97
|
+
@fallback_definition.dup
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
80
101
|
def merge_field_options(type_definition, options)
|
|
81
102
|
type_definition['enum'] = options[:enum] if options[:enum]
|
|
82
103
|
type_definition['items'] = options[:items].deep_stringify_keys if options[:items]
|
|
@@ -97,22 +118,24 @@ module BlueprinterSchema
|
|
|
97
118
|
|
|
98
119
|
# rubocop:disable Metrics/MethodLength
|
|
99
120
|
# rubocop:disable Metrics/CyclomaticComplexity
|
|
100
|
-
def
|
|
101
|
-
case
|
|
121
|
+
def type_to_json_schema(type, null)
|
|
122
|
+
case type
|
|
102
123
|
when :string, :text
|
|
103
|
-
build_json_schema_type('string',
|
|
124
|
+
build_json_schema_type('string', null)
|
|
104
125
|
when :integer
|
|
105
|
-
build_json_schema_type('integer',
|
|
126
|
+
build_json_schema_type('integer', null)
|
|
106
127
|
when :float, :decimal
|
|
107
|
-
build_json_schema_type('number',
|
|
128
|
+
build_json_schema_type('number', null)
|
|
108
129
|
when :boolean
|
|
109
|
-
build_json_schema_type('boolean',
|
|
130
|
+
build_json_schema_type('boolean', null)
|
|
110
131
|
when :date
|
|
111
|
-
build_json_schema_type('string',
|
|
132
|
+
build_json_schema_type('string', null, 'date')
|
|
112
133
|
when :datetime, :timestamp
|
|
113
|
-
build_json_schema_type('string',
|
|
134
|
+
build_json_schema_type('string', null, 'date-time')
|
|
114
135
|
when :uuid
|
|
115
|
-
build_json_schema_type('string',
|
|
136
|
+
build_json_schema_type('string', null, 'uuid')
|
|
137
|
+
else
|
|
138
|
+
@fallback_definition.dup
|
|
116
139
|
end
|
|
117
140
|
end
|
|
118
141
|
# rubocop:enable Metrics/MethodLength
|