blueprinter_schema 1.4.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 +43 -0
- data/lib/blueprinter_schema/generator.rb +23 -11
- 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
|
@@ -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
|
|
@@ -81,13 +81,23 @@ module BlueprinterSchema
|
|
|
81
81
|
if field.options[:type]
|
|
82
82
|
type_definition['type'] = ensure_valid_json_schema_types!(field)
|
|
83
83
|
elsif @model
|
|
84
|
-
|
|
85
|
-
type_definition = ar_column_to_json_schema(column)
|
|
84
|
+
type_definition = model_attribute_to_json_schema(field.name.to_s)
|
|
86
85
|
end
|
|
87
86
|
|
|
88
87
|
merge_field_options(type_definition, field.options)
|
|
89
88
|
end
|
|
90
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
|
+
|
|
91
101
|
def merge_field_options(type_definition, options)
|
|
92
102
|
type_definition['enum'] = options[:enum] if options[:enum]
|
|
93
103
|
type_definition['items'] = options[:items].deep_stringify_keys if options[:items]
|
|
@@ -108,22 +118,24 @@ module BlueprinterSchema
|
|
|
108
118
|
|
|
109
119
|
# rubocop:disable Metrics/MethodLength
|
|
110
120
|
# rubocop:disable Metrics/CyclomaticComplexity
|
|
111
|
-
def
|
|
112
|
-
case
|
|
121
|
+
def type_to_json_schema(type, null)
|
|
122
|
+
case type
|
|
113
123
|
when :string, :text
|
|
114
|
-
build_json_schema_type('string',
|
|
124
|
+
build_json_schema_type('string', null)
|
|
115
125
|
when :integer
|
|
116
|
-
build_json_schema_type('integer',
|
|
126
|
+
build_json_schema_type('integer', null)
|
|
117
127
|
when :float, :decimal
|
|
118
|
-
build_json_schema_type('number',
|
|
128
|
+
build_json_schema_type('number', null)
|
|
119
129
|
when :boolean
|
|
120
|
-
build_json_schema_type('boolean',
|
|
130
|
+
build_json_schema_type('boolean', null)
|
|
121
131
|
when :date
|
|
122
|
-
build_json_schema_type('string',
|
|
132
|
+
build_json_schema_type('string', null, 'date')
|
|
123
133
|
when :datetime, :timestamp
|
|
124
|
-
build_json_schema_type('string',
|
|
134
|
+
build_json_schema_type('string', null, 'date-time')
|
|
125
135
|
when :uuid
|
|
126
|
-
build_json_schema_type('string',
|
|
136
|
+
build_json_schema_type('string', null, 'uuid')
|
|
137
|
+
else
|
|
138
|
+
@fallback_definition.dup
|
|
127
139
|
end
|
|
128
140
|
end
|
|
129
141
|
# rubocop:enable Metrics/MethodLength
|