blueprinter_schema 0.3.0 → 0.4.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 +16 -1
- data/lib/blueprinter_schema/version.rb +1 -1
- data/lib/blueprinter_schema.rb +13 -15
- 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: 850dc505e2b9f65b160a2521a93c42147a68b0feaf41ff15b8c238c29c3dbccb
|
4
|
+
data.tar.gz: 4c8d1b9baa93c8d937a597ecd37deb2101753063c8509efa618fbd39a06ef41e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc611517dfab3eda9215428780ed18b68b83d75a706421681181fea05f4a2b50d93c5ca080e39471400767df618b09724b1296507b03ad4310e5f572cc060ec7
|
7
|
+
data.tar.gz: 993043647ef3b2efdf803f97e97057512b2d60296d952b39cb2289fce6dda042899cf1bc1e0d35872010e1bf4316e16ff382515a022444d045189be03a50b176
|
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.
|
data/lib/blueprinter_schema.rb
CHANGED
@@ -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,7 +12,7 @@ module BlueprinterSchema
|
|
12
12
|
{
|
13
13
|
'type' => 'object',
|
14
14
|
'title' => model.name,
|
15
|
-
'properties' => build_properties(fields, associations, model),
|
15
|
+
'properties' => build_properties(fields, associations, model, options),
|
16
16
|
'required' => build_required_fields(fields),
|
17
17
|
'additionalProperties' => false
|
18
18
|
}
|
@@ -20,11 +20,13 @@ module BlueprinterSchema
|
|
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
|
-
|
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
|
|
39
|
+
def skip_field?(field, options)
|
40
|
+
!options[:include_conditional_fields] && (field.options[:if] || field.options[:unless])
|
41
|
+
end
|
42
|
+
|
37
43
|
def build_required_fields(fields)
|
38
44
|
fields.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
|
-
|
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
|
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
|