json-schema_builder 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 082f36b316c2da4de5d21d907ed6cbf6f6856054
4
- data.tar.gz: 9210568145ac172ce05396f3bb529b88e9aa82a1
3
+ metadata.gz: 3e166709b6daba83a54ba1774b1bbae0207b8480
4
+ data.tar.gz: 0af74a28bd06737381ad31e8aae3ca5f94c692f9
5
5
  SHA512:
6
- metadata.gz: 92ea3b1d9739cd6f7eff349d5f67a2706a9897925089bf3ed4efcb39dae9b1a83d8b8dfafe21cfb1921f886fcd0a6e24b83792c12b6b1b23fd549519d754eea7
7
- data.tar.gz: 9cf96de81566436992fa384ca0cf3be326956581d36b0beb4bd006776b5846047881e61d2521b628750f3406c0e1fd5d12c51b0ce13c3b4b84ecc706b115a980
6
+ metadata.gz: 9750809f3276af4d79e4eae367efbaa2a4434e25d8c07d788ab9b778569f6bf3e84581432e7890f990e53245a87c354c3a24f05ff44cf78c4a7ffca377f8556a
7
+ data.tar.gz: 79fbfcccad68fb29152e13886c169895ae0499ca5f6253f4cd45c6b13ec8ef28200b9eb966f69bf7e3c7c6de5d5a1574f60141cac5e374f8317ba2ebd6595496
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-schema_builder (0.0.2)
4
+ json-schema_builder (0.0.3)
5
5
  activesupport (~> 4.0, = 4.2.0.rc2)
6
6
  json-schema (~> 2.5)
7
7
 
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # JSON::SchemaBuilder
2
2
 
3
+ [![Build Status](https://travis-ci.org/parrish/json-schema_builder.svg)](https://travis-ci.org/parrish/json-schema_builder)
3
4
  [![Code Climate](https://codeclimate.com/github/parrish/json-schema_builder/badges/gpa.svg)](https://codeclimate.com/github/parrish/json-schema_builder)
4
5
  [![Test Coverage](https://codeclimate.com/github/parrish/json-schema_builder/badges/coverage.svg)](https://codeclimate.com/github/parrish/json-schema_builder)
5
6
  [![Gem Version](https://badge.fury.io/rb/json-schema_builder.svg)](http://badge.fury.io/rb/json-schema_builder)
6
7
 
7
- Build JSON schemas with Ruby
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
- TODO: Write usage instructions here
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/[my-github-username]/json-schema_builder/fork )
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`)
@@ -21,5 +21,11 @@ module JSON
21
21
  def self.default_options
22
22
  @options || { }
23
23
  end
24
+
25
+ def initialize(context = { })
26
+ context.each_pair do |key, value|
27
+ instance_variable_set "@#{ key }", value
28
+ end
29
+ end
24
30
  end
25
31
  end
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module SchemaBuilder
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
@@ -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
@@ -1,6 +1,7 @@
1
1
  module IntegrationHelper
2
2
  extend RSpec::SharedContext
3
- let(:schema){ described_class.new.example }
3
+ let(:schema_context){ { } }
4
+ let(:schema){ described_class.new(schema_context).example }
4
5
  let(:json){ schema.as_json }
5
6
  let(:expected_json){ { } }
6
7
 
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.2
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