blueprinter_schema 0.3.0 → 0.4.1

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: bc07f2ba27532688748d728cfdb6ee4ae6d0c92a66b17d753519384ee8538f5b
4
- data.tar.gz: 2add634f63f55305245acb2fb7d64438f7017d8ce6bdce9bb7626f59f186b66a
3
+ metadata.gz: 64488524dcc942abdab6c3f96841d1619dc40abade8d3006c73c55ef035c29e1
4
+ data.tar.gz: 7a49a1189f126209721e032e5e752f1fbd7e15a4c5c0f442293627d6525afa2c
5
5
  SHA512:
6
- metadata.gz: 592881730764b7873f8ae95bc4de43942f3f8345c37327157b528b9b740706544c206b1ea84b3c41d430d8e6f389cd6699cf0a535797c07e4f2e82aa06c8aa33
7
- data.tar.gz: 88163ad100fa9e310cb42c8b1dc9240b3aa09831b048e6215ddc6636cfea57ade49d452290ca01cd91a13f001e902fe8291ff17d8da63ad85a32d1abe470e7c4
6
+ metadata.gz: '0654209ac2820114fb2807295a690787fee47b41671ddfc9ff4740939f59ef84bcb05b92d3277e553a44d99652b32d318371e00eedcd9995c9be265d49ddbb60'
7
+ data.tar.gz: bc4c26c5039d90cb5950f697cc7b242092fc92d194dc16cbfe3d075292b240bf439573645920f1b462591c5ad5f4661adc407965ebac9f181d6ea4fb838c7b2f
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # BlueprinterSchema
2
2
 
3
- ### Create JSON Schema from Blueprinter Serializers and ActiveRecord Models.
3
+ ### Create JSON Schema from [Blueprinter](https://github.com/procore-oss/blueprinter) Serializers and ActiveRecord Models.
4
+
5
+ Serializers define which fields are used. Models know the database field types. Put these together and you get a JSON Schema.
4
6
 
5
7
  ## Installation
6
8
 
@@ -81,6 +83,19 @@ BlueprinterSchema.generate(UserSerializer, User)
81
83
  }
82
84
  ```
83
85
 
86
+ ### Options and defaults
87
+
88
+ ```rb
89
+ BlueprinterSchema.generate(
90
+ serializer,
91
+ model,
92
+ {
93
+ include_conditional_fields: true, # Whether or not to include conditional fields from the serializer
94
+ fallback_type: {} # Type when no DB column is found or type is unknown. E.g. { 'type' => 'object' }
95
+ }
96
+ )
97
+ ```
98
+
84
99
  ## Development
85
100
 
86
101
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterSchema
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -4,7 +4,7 @@ require_relative 'blueprinter_schema/version'
4
4
 
5
5
  module BlueprinterSchema
6
6
  class << self
7
- def generate(serializer, model)
7
+ def generate(serializer, model, options = { include_conditional_fields: true, fallback_type: {} })
8
8
  views = serializer.reflections
9
9
  fields = views[:default].fields
10
10
  associations = views[:default].associations
@@ -12,19 +12,21 @@ module BlueprinterSchema
12
12
  {
13
13
  'type' => 'object',
14
14
  'title' => model.name,
15
- 'properties' => build_properties(fields, associations, model),
16
- 'required' => build_required_fields(fields),
15
+ 'properties' => build_properties(fields, associations, model, options),
16
+ 'required' => build_required_fields(fields, options),
17
17
  'additionalProperties' => false
18
18
  }
19
19
  end
20
20
 
21
21
  private
22
22
 
23
- def build_properties(fields, associations, model)
23
+ def build_properties(fields, associations, model, options)
24
24
  properties = {}
25
25
 
26
26
  fields.each_value do |field|
27
- properties[field.display_name.to_s] = field_to_json_schema(field, model)
27
+ next if skip_field?(field, options)
28
+
29
+ properties[field.display_name.to_s] = field_to_json_schema(field, model, options)
28
30
  end
29
31
 
30
32
  associations.each_value do |association|
@@ -34,25 +36,24 @@ module BlueprinterSchema
34
36
  properties
35
37
  end
36
38
 
37
- def build_required_fields(fields)
38
- fields.keys.map(&:to_s)
39
+ def skip_field?(field, options)
40
+ !options[:include_conditional_fields] && (field.options[:if] || field.options[:unless])
41
+ end
42
+
43
+ def build_required_fields(fields, options)
44
+ fields.reject { |_, field| skip_field?(field, options) }.keys.map(&:to_s)
39
45
  end
40
46
 
41
- def field_to_json_schema(field, model)
47
+ def field_to_json_schema(field, model, options)
42
48
  column = model.columns_hash[field.name.to_s]
43
49
 
44
- if column
45
- ar_column_to_json_schema(column)
46
- else
47
- # Non-database (e.g. computed) field, we don't know the schema type
48
- {}
49
- end
50
+ ar_column_to_json_schema(column) || options[:fallback_type]
50
51
  end
51
52
 
52
53
  # rubocop:disable Metrics/MethodLength
53
54
  # rubocop:disable Metrics/CyclomaticComplexity
54
55
  def ar_column_to_json_schema(column)
55
- case column.type
56
+ case column&.type
56
57
  when :string, :text
57
58
  build_json_schema_type('string', column.null)
58
59
  when :integer
@@ -67,9 +68,6 @@ module BlueprinterSchema
67
68
  build_json_schema_type('string', column.null, 'date-time')
68
69
  when :uuid
69
70
  build_json_schema_type('string', column.null, 'uuid')
70
- else
71
- # Unknown column type, we don't know the schema type
72
- {}
73
71
  end
74
72
  end
75
73
  # rubocop:enable Metrics/MethodLength
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: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign