jsonschema_serializer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.rubocop.yml +5 -1
- data/.travis.yml +0 -18
- data/CHANGELOG.md +15 -0
- data/README.md +7 -7
- data/lib/jsonschema_serializer/active_record.rb +10 -13
- data/lib/jsonschema_serializer/builder.rb +153 -16
- data/lib/jsonschema_serializer/version.rb +1 -1
- data/lib/jsonschema_serializer.rb +1 -2
- metadata +4 -4
- data/Gemfile-pre-2 +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d65039713e6c37e41ca9c190655625b3d1ab9fc8
|
4
|
+
data.tar.gz: 204bea0aacdc61d33dedb8894f4f779ef5a63680
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 494f948e0b3b29bcaef14142952fc580a8de4bf68271a139af6eecf8dcbae99b3d925892734b7199bc8dcc10435600cc81418eb4bebcbcaac4b67d2ef9a322a3
|
7
|
+
data.tar.gz: 380403b40d52d4e7352a8258f997073900c728391d40f0f536405adbb4214fa9a6920dad3c256d7100f007659cf74456f4ab6577fa992d746094e852bbb51105
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -14,10 +14,14 @@ Metrics/LineLength:
|
|
14
14
|
Max: 90
|
15
15
|
|
16
16
|
Style/Documentation:
|
17
|
-
Enabled:
|
17
|
+
Enabled: true
|
18
18
|
|
19
19
|
Style/FrozenStringLiteralComment:
|
20
20
|
Enabled: false
|
21
21
|
|
22
|
+
Style/ModuleFunction:
|
23
|
+
Exclude:
|
24
|
+
- lib/jsonschema_serializer/active_record.rb
|
25
|
+
|
22
26
|
Style/SymbolArray:
|
23
27
|
EnforcedStyle: brackets
|
data/.travis.yml
CHANGED
@@ -3,25 +3,7 @@ sudo: false
|
|
3
3
|
language: ruby
|
4
4
|
|
5
5
|
jobs:
|
6
|
-
allow_failures:
|
7
|
-
- rvm: 1.8.7
|
8
|
-
gemfile: Gemfile-pre-2
|
9
|
-
- rvm: 1.9.3
|
10
|
-
gemfile: Gemfile-pre-2
|
11
|
-
- rvm: 2.0.0
|
12
|
-
gemfile: Gemfile-pre-2
|
13
|
-
- rvm: jruby-1.7.21
|
14
|
-
gemfile: Gemfile-pre-2
|
15
|
-
|
16
6
|
include:
|
17
|
-
- rvm: 1.8.7
|
18
|
-
gemfile: Gemfile-pre-2
|
19
|
-
- rvm: 1.9.3
|
20
|
-
gemfile: Gemfile-pre-2
|
21
|
-
- rvm: 2.0.0
|
22
|
-
gemfile: Gemfile-pre-2
|
23
|
-
- rvm: jruby-1.7.21
|
24
|
-
gemfile: Gemfile-pre-2
|
25
7
|
- rvm: 2.1.5
|
26
8
|
gemfile: Gemfile
|
27
9
|
- rvm: 2.2.7
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
# v 0.0.3 (2018-05-21)
|
4
|
+
|
5
|
+
- improved documentation on existing code base
|
6
|
+
- refactored `JsonschemaSerializer::ActiveRecord` as a `class`
|
7
|
+
|
8
|
+
# v 0.0.2 (2018-05-20)
|
9
|
+
|
10
|
+
- basic rDoc comments for documentation
|
11
|
+
|
12
|
+
# v 0.0.1 (2018-05-20)
|
13
|
+
|
14
|
+
- basic implementation of `JsonschemaSerializer::Builder`
|
15
|
+
- POC implementation of a `JsonschemaSerializer::ActiveRecord` module
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# JsonschemaSerializer
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/mberlanda/jsonschema_serializer.svg?branch=master)](https://travis-ci.org/mberlanda/jsonschema_serializer)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/jsonschema_serializer.svg)](https://badge.fury.io/rb/jsonschema_serializer)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/7312071a0865c70f5d60/maintainability)](https://codeclimate.com/github/mberlanda/jsonschema_serializer/maintainability)
|
4
6
|
|
5
7
|
This purpose of this gem is to generate [JsonSchema](http://json-schema.org/).
|
6
8
|
|
@@ -25,7 +27,7 @@ Or install it yourself as:
|
|
25
27
|
You can generate a schema as follows:
|
26
28
|
|
27
29
|
```ruby
|
28
|
-
schema =
|
30
|
+
schema = JsonschemaSerializer::Builder.build do |b|
|
29
31
|
|
30
32
|
subscriber = b._object title: :subscriber, required: [:age] do |prop|
|
31
33
|
prop.merge! b.string :first_name, title: 'First Name'
|
@@ -57,13 +59,11 @@ You can alternatively use an experimental builder for `ActiveRecord`
|
|
57
59
|
|
58
60
|
|
59
61
|
```ruby
|
60
|
-
|
61
|
-
include JsonschemaSerializer::ActiveRecord
|
62
|
-
end
|
62
|
+
serializer = JsonschemaSerializer::ActiveRecord
|
63
63
|
|
64
|
-
schema =
|
65
|
-
schema =
|
66
|
-
schema =
|
64
|
+
schema = serializer.from_active_record(MyActiveRecordClass)
|
65
|
+
schema = serializer.from_active_record(MyActiveRecordClass, only: %[desired1 desired2])
|
66
|
+
schema = serializer.from_active_record(MyActiveRecordClass, except: %[ignored1 ignored2])
|
67
67
|
|
68
68
|
# You can manipulate the resulting schema
|
69
69
|
|
@@ -1,22 +1,19 @@
|
|
1
1
|
require_relative 'builder'
|
2
2
|
|
3
|
-
# The +JsonschemaSerializer::Activerecord+ module provides
|
4
|
-
# a +from_activerecord+ class method to serialize some
|
5
|
-
# ActiveRecord classes with the minimum effort
|
6
|
-
|
7
3
|
module JsonschemaSerializer
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
# The +JsonschemaSerializer::Activerecord+ class provides
|
5
|
+
# a +from_active_record+ class method to serialize some
|
6
|
+
# ActiveRecord classes with the minimum effort
|
7
|
+
class ActiveRecord
|
8
|
+
class << self
|
13
9
|
# Serialize an ActiveRecord class into a
|
14
10
|
# JsonschemaSerializer::Builder object
|
15
11
|
#
|
16
|
-
#
|
17
|
-
# +klass+
|
18
|
-
# +only+
|
19
|
-
# +except+
|
12
|
+
# Params:
|
13
|
+
# +klass+:: +ActiveRecord::Base+ class name
|
14
|
+
# +only+:: +Array+ columns as +String+
|
15
|
+
# +except+:: +Array+ columns as +String+
|
16
|
+
|
20
17
|
def from_active_record(klass, only: nil, except: nil)
|
21
18
|
validate_arguments(only, except)
|
22
19
|
JsonschemaSerializer::Builder.build do |b|
|
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
3
|
module JsonschemaSerializer
|
4
|
+
# The +JsonschemaSerializer::Builder+ class provides
|
5
|
+
# an effective DSL to generate a valid json schema.
|
4
6
|
class Builder
|
5
7
|
class << self
|
8
|
+
# The +build+ class method create a new instance and applies the block
|
6
9
|
def build
|
7
10
|
new.tap do |builder|
|
8
11
|
yield(builder) if block_given?
|
@@ -10,53 +13,187 @@ module JsonschemaSerializer
|
|
10
13
|
end
|
11
14
|
end
|
12
15
|
|
16
|
+
# An hash representation of the +schema+
|
13
17
|
attr_reader :schema
|
14
18
|
|
19
|
+
# The +new+ method creates assigns an empty object
|
20
|
+
# to a +schema+ instance variable
|
15
21
|
def initialize
|
16
|
-
@schema ||=
|
17
|
-
type: :object,
|
18
|
-
properties: {}
|
19
|
-
}
|
22
|
+
@schema ||= _object
|
20
23
|
end
|
21
24
|
|
25
|
+
# The +to_json+ method exports the schema as a json string
|
22
26
|
def to_json
|
23
27
|
@schema.to_json
|
24
28
|
end
|
25
29
|
|
30
|
+
# Assigns the +title+ to the root schema object
|
31
|
+
#
|
32
|
+
# Params:
|
33
|
+
# +title+:: +String+ or +Symbol+ title field of the schema object
|
34
|
+
|
26
35
|
def title(title)
|
27
36
|
@schema[:title] = title
|
28
37
|
end
|
29
38
|
|
39
|
+
# Assigns the +description+ to the root schema object
|
40
|
+
#
|
41
|
+
# params:
|
42
|
+
# +description+:: +String+ description field of the schema object
|
43
|
+
|
30
44
|
def description(description)
|
31
45
|
@schema[:description] = description
|
32
46
|
end
|
33
47
|
|
48
|
+
# The +required+ method allows to provide a list of required properties
|
49
|
+
#
|
50
|
+
# params:
|
51
|
+
# +required+ [Array[String, Symbol]]
|
52
|
+
|
34
53
|
def required(*required)
|
35
54
|
@schema[:required] = required
|
36
55
|
end
|
37
56
|
|
57
|
+
# The +properties+ method allows to access object properties
|
58
|
+
#
|
59
|
+
# e.g.:
|
60
|
+
# JsonschemaSerializer::Builder.build do |b|
|
61
|
+
# b.properties.tap do |p|
|
62
|
+
# p.merge! {}
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
|
38
66
|
def properties
|
39
67
|
@schema[:properties] ||= {}
|
40
68
|
end
|
41
69
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
70
|
+
# A base representation of the +boolean+ type.
|
71
|
+
def _boolean(**opts)
|
72
|
+
{ type: :boolean }.merge(opts)
|
46
73
|
end
|
47
74
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
75
|
+
# A property representation of the +boolean+ type.
|
76
|
+
#
|
77
|
+
# Params:
|
78
|
+
# +name+:: +String+ or +Symbol+
|
79
|
+
#
|
80
|
+
# Optional Params:
|
81
|
+
# +default+:: +Boolean+ default value
|
82
|
+
# +description+:: +String+ property description
|
83
|
+
# +title+:: +String+ property title
|
84
|
+
|
85
|
+
def boolean(name, **opts)
|
86
|
+
{ name => _boolean(opts) }
|
87
|
+
end
|
52
88
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
89
|
+
# A base representation of the +integer+ type.
|
90
|
+
def _integer(**opts)
|
91
|
+
{ type: :integer }.merge(opts)
|
92
|
+
end
|
93
|
+
|
94
|
+
# A property representation of the +integer+ type.
|
95
|
+
#
|
96
|
+
# Params:
|
97
|
+
# +name+:: +String+ or +Symbol+
|
98
|
+
#
|
99
|
+
# Optional Params:
|
100
|
+
# +default+:: +Integer+ default value
|
101
|
+
# +description+:: +String+ property description
|
102
|
+
# +title+:: +String+ property title
|
103
|
+
# +enum+:: +Array+ property allowed values
|
104
|
+
# +minimum+:: +Integer+ property minimum value
|
105
|
+
# +maximum+:: +Integer+ property maximum value
|
106
|
+
# +multipleOf+:: +Integer+ property conditional constraint
|
107
|
+
|
108
|
+
def integer(name, **opts)
|
109
|
+
{ name => _integer(opts) }
|
110
|
+
end
|
111
|
+
|
112
|
+
# A base representation of the +number+ type.
|
113
|
+
def _number(**opts)
|
114
|
+
{ type: :number }.merge(opts)
|
115
|
+
end
|
116
|
+
|
117
|
+
# A property representation of the +number+ type.
|
118
|
+
#
|
119
|
+
# Params:
|
120
|
+
# +name+:: +String+ or +Symbol+
|
121
|
+
#
|
122
|
+
# Optional Params:
|
123
|
+
# +default+:: +Numeric+ default value
|
124
|
+
# +description+:: +String+ property description
|
125
|
+
# +title+:: +String+ property title
|
126
|
+
# +enum+:: +Array+ property allowed values
|
127
|
+
# +minimum+:: +Numeric+ property minimum value
|
128
|
+
# +maximum+:: +Numeric+ property maximum value
|
129
|
+
# +multipleOf+:: +Numeric+ property conditional constraint
|
130
|
+
|
131
|
+
def number(name, **opts)
|
132
|
+
{ name => _number(opts) }
|
133
|
+
end
|
134
|
+
|
135
|
+
# A base representation of the +string+ type.
|
136
|
+
def _string(**opts)
|
137
|
+
{ type: :string }.merge(opts)
|
138
|
+
end
|
139
|
+
|
140
|
+
# A property representation of the +string+ type.
|
141
|
+
#
|
142
|
+
# Params:
|
143
|
+
# +name+:: +String+ or +Symbol+
|
144
|
+
#
|
145
|
+
# Optional Params:
|
146
|
+
# +default+:: +String+ default value
|
147
|
+
# +description+:: +String+ property description
|
148
|
+
# +title+:: +String+ property title
|
149
|
+
# +format+:: +String+ property format for validation
|
150
|
+
# +minLength+:: +Int+ property minimum length
|
151
|
+
|
152
|
+
def string(name, **opts)
|
153
|
+
{ name => _string(opts) }
|
154
|
+
end
|
155
|
+
|
156
|
+
# A base representation of the +object+ type.
|
157
|
+
#
|
158
|
+
# JsonschemaSerializer::Builder.build do |b|
|
159
|
+
# subscriber = b._object title: :subscriber, required: [:age] do |prop|
|
160
|
+
# prop.merge! b.string :first_name, title: 'First Name'
|
161
|
+
# prop.merge! b.string :last_name, title: 'Last Name'
|
162
|
+
# prop.merge! b.integer :age, title: 'Age'
|
163
|
+
# end
|
164
|
+
# end
|
165
|
+
|
166
|
+
def _object(**opts)
|
167
|
+
{ type: :object, properties: {} }.merge(opts).tap do |h|
|
168
|
+
yield(h[:properties]) if block_given?
|
57
169
|
end
|
58
170
|
end
|
59
171
|
|
172
|
+
# A property representation of the +array+ type.
|
173
|
+
#
|
174
|
+
# Params:
|
175
|
+
# +name+:: +String+ or +Symbol+
|
176
|
+
# +items+:: an object representation or an array of objects
|
177
|
+
#
|
178
|
+
# Optional Params:
|
179
|
+
# +default+:: +Array+ default value
|
180
|
+
# +description+:: +String+ property description
|
181
|
+
# +title+:: +String+ property title
|
182
|
+
# +minItems+:: +Int+ property minimum length
|
183
|
+
# +maxItems+:: +Int+ property maximum length
|
184
|
+
#
|
185
|
+
# JsonschemaSerializer::Builder.build do |b|
|
186
|
+
# b.array :integers, items: {type: :integer}, minItems:5
|
187
|
+
# b.array :strings, items: b._string, default: []
|
188
|
+
#
|
189
|
+
# subscriber = b._object title: :subscriber, required: [:age] do |prop|
|
190
|
+
# prop.merge! b.string :first_name, title: 'First Name'
|
191
|
+
# prop.merge! b.string :last_name, title: 'Last Name'
|
192
|
+
# prop.merge! b.integer :age, title: 'Age'
|
193
|
+
# end
|
194
|
+
# b.array :subscribers, items: subscriber
|
195
|
+
# end
|
196
|
+
#
|
60
197
|
def array(name, items:, **opts)
|
61
198
|
{
|
62
199
|
name => { type: :array, items: items }.merge(opts)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonschema_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mauro Berlanda
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,8 +69,8 @@ files:
|
|
69
69
|
- ".rspec"
|
70
70
|
- ".rubocop.yml"
|
71
71
|
- ".travis.yml"
|
72
|
+
- CHANGELOG.md
|
72
73
|
- Gemfile
|
73
|
-
- Gemfile-pre-2
|
74
74
|
- README.md
|
75
75
|
- Rakefile
|
76
76
|
- bin/console
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.5.1
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: The purpose of this gem is to generate JsonSchema
|
data/Gemfile-pre-2
DELETED