json-schema_builder 0.0.2 → 0.0.3
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/Gemfile.lock +1 -1
- data/README.md +105 -3
- data/lib/json/schema_builder.rb +6 -0
- data/lib/json/schema_builder/version.rb +1 -1
- data/spec/integration/context_delegation_spec.rb +44 -0
- data/spec/integration/schema_builder_spec.rb +7 -0
- data/spec/support/examples/context_delegation.rb +19 -0
- data/spec/support/integration_helper.rb +2 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e166709b6daba83a54ba1774b1bbae0207b8480
|
4
|
+
data.tar.gz: 0af74a28bd06737381ad31e8aae3ca5f94c692f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9750809f3276af4d79e4eae367efbaa2a4434e25d8c07d788ab9b778569f6bf3e84581432e7890f990e53245a87c354c3a24f05ff44cf78c4a7ffca377f8556a
|
7
|
+
data.tar.gz: 79fbfcccad68fb29152e13886c169895ae0499ca5f6253f4cd45c6b13ec8ef28200b9eb966f69bf7e3c7c6de5d5a1574f60141cac5e374f8317ba2ebd6595496
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# JSON::SchemaBuilder
|
2
2
|
|
3
|
+
[](https://travis-ci.org/parrish/json-schema_builder)
|
3
4
|
[](https://codeclimate.com/github/parrish/json-schema_builder)
|
4
5
|
[](https://codeclimate.com/github/parrish/json-schema_builder)
|
5
6
|
[](http://badge.fury.io/rb/json-schema_builder)
|
6
7
|
|
7
|
-
Build JSON
|
8
|
+
##### Build [JSON Schemas](http://json-schema.org) with Ruby. Validates JSON using [json-schema](https://github.com/ruby-json-schema/json-schema)
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
@@ -24,11 +25,112 @@ Or install it yourself as:
|
|
24
25
|
|
25
26
|
## Usage
|
26
27
|
|
27
|
-
|
28
|
+
### Building schemas
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'json/schema_builder'
|
32
|
+
|
33
|
+
class YourSchema
|
34
|
+
include JSON::SchemaBuilder
|
35
|
+
|
36
|
+
def schema
|
37
|
+
object do
|
38
|
+
string :name, min_length: 1, required: true
|
39
|
+
array :your_ids do
|
40
|
+
max_items 10
|
41
|
+
unique_items true
|
42
|
+
items type: :integer
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
builder = YourSchema.new
|
49
|
+
```
|
50
|
+
|
51
|
+
`builder.schema.as_json` will return a valid JSON schema:
|
52
|
+
|
53
|
+
```json
|
54
|
+
{
|
55
|
+
"type": "object",
|
56
|
+
"required": "name",
|
57
|
+
"properties": {
|
58
|
+
"name": {
|
59
|
+
"type": "string",
|
60
|
+
"minLength": 1
|
61
|
+
},
|
62
|
+
"your_ids": {
|
63
|
+
"type": "array",
|
64
|
+
"maxItems": 10,
|
65
|
+
"uniqueItems": true,
|
66
|
+
"items": { "type": "integer" }
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
[More examples](https://github.com/parrish/json-schema_builder/tree/master/spec/support/examples) can be found in the specs.
|
73
|
+
|
74
|
+
### Validating data against a schema
|
75
|
+
|
76
|
+
`builder.schema.validate(data)` will validate your object against the schema:
|
77
|
+
```ruby
|
78
|
+
builder.schema.validate({ }) # => false
|
79
|
+
builder.schema.validate name: 'Your Name' # => true
|
80
|
+
```
|
81
|
+
|
82
|
+
`builder.schema.validate!(data)` validates your object, raising an exception on failure:
|
83
|
+
```ruby
|
84
|
+
builder.schema.validate!({ })
|
85
|
+
# JSON::Schema::ValidationError: The property '#/' did not contain a required property of 'name'
|
86
|
+
|
87
|
+
builder.schema.validate! name: 'Your Name', your_ids: [1, 1, 2]
|
88
|
+
# JSON::Schema::ValidationError: The property '#/your_ids' contained duplicated array values
|
89
|
+
```
|
90
|
+
|
91
|
+
`builder.schema.fully_validate(data)` validates the object and reports all invalid properties:
|
92
|
+
```ruby
|
93
|
+
builder.schema.fully_validate your_ids: 'fail'
|
94
|
+
# [
|
95
|
+
# "The property '#/your_ids' of type String did not match the following type: array ",
|
96
|
+
# "The property '#/' did not contain a required property of 'name'"
|
97
|
+
# ]
|
98
|
+
```
|
99
|
+
|
100
|
+
Fragment validation can be accomplished by specifying the schema fragment:
|
101
|
+
```ruby
|
102
|
+
builder.schema.validate [1], fragment: '#/properties/your_ids' # => true
|
103
|
+
```
|
104
|
+
|
105
|
+
### Configuring
|
106
|
+
|
107
|
+
[json-schema](https://github.com/ruby-json-schema/json-schema) validation options can be specified in several contexts:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
# As a global default
|
111
|
+
JSON::SchemaBuilder.configure do |opts|
|
112
|
+
opts.validate_schema = true
|
113
|
+
end
|
114
|
+
|
115
|
+
# As a class-level default
|
116
|
+
YourSchema.configure do |opts|
|
117
|
+
opts.insert_defaults = true
|
118
|
+
end
|
119
|
+
|
120
|
+
# Or at validation
|
121
|
+
builder.schema.validate data, strict: true, validate_schema: false
|
122
|
+
|
123
|
+
# Results in
|
124
|
+
{
|
125
|
+
validate_schema: false,
|
126
|
+
insert_defaults: true,
|
127
|
+
strict: true
|
128
|
+
}
|
129
|
+
```
|
28
130
|
|
29
131
|
## Contributing
|
30
132
|
|
31
|
-
1. Fork it ( https://github.com/
|
133
|
+
1. Fork it ( https://github.com/parrish/json-schema_builder/fork )
|
32
134
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
135
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
136
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/json/schema_builder.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Examples::ContextDelegation, type: :integration do
|
4
|
+
context 'with an admin' do
|
5
|
+
it_behaves_like 'a builder' do
|
6
|
+
let(:schema_context) do
|
7
|
+
{
|
8
|
+
user: Examples::ContextDelegation::User.new(role: :admin)
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:expected_json) do
|
13
|
+
{
|
14
|
+
type: :object,
|
15
|
+
additionalProperties: false,
|
16
|
+
properties: {
|
17
|
+
name: { type: :string },
|
18
|
+
role: { type: :string }
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with a user' do
|
26
|
+
it_behaves_like 'a builder' do
|
27
|
+
let(:schema_context) do
|
28
|
+
{
|
29
|
+
user: Examples::ContextDelegation::User.new(role: :user)
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:expected_json) do
|
34
|
+
{
|
35
|
+
type: :object,
|
36
|
+
additionalProperties: false,
|
37
|
+
properties: {
|
38
|
+
name: { type: :string }
|
39
|
+
}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -17,6 +17,13 @@ RSpec.describe Examples::SchemaBuilder, type: :integration do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
describe '#initialize' do
|
21
|
+
it 'should assign context' do
|
22
|
+
instance = Examples::SchemaBuilder.new a: 1, b: 2
|
23
|
+
expect(instance.as_json).to eql 'a' => 1, 'b' => 2
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
20
27
|
describe 'Configuration' do
|
21
28
|
subject{ Examples::SchemaBuilder }
|
22
29
|
after(:all) do
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Examples
|
2
|
+
class ContextDelegation
|
3
|
+
include JSON::SchemaBuilder
|
4
|
+
delegate :admin?, to: :@user
|
5
|
+
|
6
|
+
def example
|
7
|
+
object additional_properties: false do
|
8
|
+
string :name
|
9
|
+
string :role if admin?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class User < OpenStruct
|
14
|
+
def admin?
|
15
|
+
role == :admin
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema_builder
|
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
|
- Michael Parrish
|
@@ -161,6 +161,7 @@ files:
|
|
161
161
|
- lib/json/schema_builder/validation.rb
|
162
162
|
- lib/json/schema_builder/version.rb
|
163
163
|
- spec/integration/array_definitions_spec.rb
|
164
|
+
- spec/integration/context_delegation_spec.rb
|
164
165
|
- spec/integration/entity_literals_spec.rb
|
165
166
|
- spec/integration/mixed_arrays_spec.rb
|
166
167
|
- spec/integration/mixed_objects_spec.rb
|
@@ -174,6 +175,7 @@ files:
|
|
174
175
|
- spec/support/.keep
|
175
176
|
- spec/support/attribute_matcher.rb
|
176
177
|
- spec/support/examples/array_definitions.rb
|
178
|
+
- spec/support/examples/context_delegation.rb
|
177
179
|
- spec/support/examples/entity_literals.rb
|
178
180
|
- spec/support/examples/mixed_arrays.rb
|
179
181
|
- spec/support/examples/mixed_objects.rb
|
@@ -222,6 +224,7 @@ specification_version: 4
|
|
222
224
|
summary: Build JSON schemas with Ruby
|
223
225
|
test_files:
|
224
226
|
- spec/integration/array_definitions_spec.rb
|
227
|
+
- spec/integration/context_delegation_spec.rb
|
225
228
|
- spec/integration/entity_literals_spec.rb
|
226
229
|
- spec/integration/mixed_arrays_spec.rb
|
227
230
|
- spec/integration/mixed_objects_spec.rb
|
@@ -235,6 +238,7 @@ test_files:
|
|
235
238
|
- spec/support/.keep
|
236
239
|
- spec/support/attribute_matcher.rb
|
237
240
|
- spec/support/examples/array_definitions.rb
|
241
|
+
- spec/support/examples/context_delegation.rb
|
238
242
|
- spec/support/examples/entity_literals.rb
|
239
243
|
- spec/support/examples/mixed_arrays.rb
|
240
244
|
- spec/support/examples/mixed_objects.rb
|