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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0cafd732029c0e4f3cd09d13dd6809e3e68f599c194a24247774e7d18f6233ca
4
- data.tar.gz: 1bf4791a3812e9f16bd3c8d7f8ed0bc2a91095130b91286fa75d3b72954b1482
3
+ metadata.gz: 392b3344dd71f597ca8c1bfc35e1e3e47c7d01417ba38cd5bf170d6f7e63f1cc
4
+ data.tar.gz: 7aadb18ef24453084f6f23fcb3b0e681c13b7d82b5e12c2d56572e3365f13e3f
5
5
  SHA512:
6
- metadata.gz: 02577f5a4da542def621be3544ff1c82d4509addfc2b8b7a1264a9a6e7d079943ae75c0574f160354f48ebf3c6b1481eeb09d899a35fb3ed83b169e25671a482
7
- data.tar.gz: 6690022f62b1a2ad817a362ae2322300e0013c22494b1bb2efe195478c0a6036063c3c31ae422ab92b1745591cacdf4d6aba0356e386df32d1419f28c8ecb717
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.map(&:to_s)
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
- column = @model.columns_hash[field.name.to_s]
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 ar_column_to_json_schema(column)
101
- case column&.type
121
+ def type_to_json_schema(type, null)
122
+ case type
102
123
  when :string, :text
103
- build_json_schema_type('string', column.null)
124
+ build_json_schema_type('string', null)
104
125
  when :integer
105
- build_json_schema_type('integer', column.null)
126
+ build_json_schema_type('integer', null)
106
127
  when :float, :decimal
107
- build_json_schema_type('number', column.null)
128
+ build_json_schema_type('number', null)
108
129
  when :boolean
109
- build_json_schema_type('boolean', column.null)
130
+ build_json_schema_type('boolean', null)
110
131
  when :date
111
- build_json_schema_type('string', column.null, 'date')
132
+ build_json_schema_type('string', null, 'date')
112
133
  when :datetime, :timestamp
113
- build_json_schema_type('string', column.null, 'date-time')
134
+ build_json_schema_type('string', null, 'date-time')
114
135
  when :uuid
115
- build_json_schema_type('string', column.null, 'uuid')
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterSchema
4
- VERSION = '1.3.0'
4
+ VERSION = '1.5.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign