active_model_serializers_validator 0.1.1 → 0.1.2
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.
- data/.rspec +1 -0
- data/README.md +9 -2
- data/Rakefile +8 -0
- data/active_model_serializers_validator.gemspec +2 -0
- data/lib/active_model/serializer/validator/mixin.rb +10 -4
- data/lib/active_model/serializer/validator/version.rb +1 -1
- data/lib/active_model_serializers_validator.rb +3 -0
- data/spec/fixtures/product.jsonschema +27 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/macros/fixtures.rb +5 -0
- data/spec/support/macros/serializers.rb +5 -0
- data/spec/validator_spec.rb +48 -0
- metadata +48 -5
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/README.md
CHANGED
@@ -34,8 +34,15 @@ serializer.valid?
|
|
34
34
|
|
35
35
|
## License
|
36
36
|
|
37
|
-
`ActiveModel::Serializer::Validator` is © 2013 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/
|
37
|
+
`ActiveModel::Serializer::Validator` is © 2013 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/active_model_serializers_validator/blob/master/LICENSE.md) file.
|
38
38
|
|
39
39
|
## About Mirego
|
40
40
|
|
41
|
-
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun.
|
41
|
+
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun.
|
42
|
+
We proudly built mobile applications for
|
43
|
+
[iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"),
|
44
|
+
[iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"),
|
45
|
+
[Android](http://mirego.com/en/android-app-development/ "Android application development"),
|
46
|
+
[Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"),
|
47
|
+
[Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and
|
48
|
+
[Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development").
|
data/Rakefile
CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
22
|
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 2.13'
|
24
|
+
spec.add_development_dependency 'json', '~> 1.7.7'
|
23
25
|
|
24
26
|
spec.add_dependency "activesupport", '>= 3.0.0'
|
25
27
|
spec.add_dependency "active_model_serializers", '~> 0.8'
|
@@ -4,14 +4,14 @@ module ActiveModel
|
|
4
4
|
module Mixin
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
JSON::Validator.validate(schema, self.to_json)
|
7
|
+
included do
|
8
|
+
attr_reader :errors
|
10
9
|
end
|
11
10
|
|
12
11
|
# Return whether the serializer output is valid
|
13
12
|
def valid?
|
14
|
-
valid_against_schema?(self.class.json_schema)
|
13
|
+
@errors = self.class.valid_against_schema?(self.class.json_schema, self)
|
14
|
+
@errors.empty?
|
15
15
|
end
|
16
16
|
|
17
17
|
module ClassMethods
|
@@ -24,6 +24,12 @@ module ActiveModel
|
|
24
24
|
return @_json_schema unless value
|
25
25
|
@_json_schema = value
|
26
26
|
end
|
27
|
+
|
28
|
+
# Validate the rendered data against a JSON schema file and
|
29
|
+
# return an errors array
|
30
|
+
def valid_against_schema?(schema, serializer)
|
31
|
+
JSON::Validator.fully_validate(schema, serializer.to_json)
|
32
|
+
end
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name":"Product",
|
3
|
+
"properties":{
|
4
|
+
"id":{
|
5
|
+
"type":"number",
|
6
|
+
"description":"Product identifier",
|
7
|
+
"required":true
|
8
|
+
},
|
9
|
+
"name":{
|
10
|
+
"description":"Name of the product",
|
11
|
+
"type":"string",
|
12
|
+
"required":true
|
13
|
+
},
|
14
|
+
"price":{
|
15
|
+
"required":true,
|
16
|
+
"type": "number",
|
17
|
+
"minimum":0,
|
18
|
+
"required":true
|
19
|
+
},
|
20
|
+
"tags":{
|
21
|
+
"type":"array",
|
22
|
+
"items":{
|
23
|
+
"type":"string"
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
|
2
|
+
|
3
|
+
require "rspec"
|
4
|
+
require 'ostruct'
|
5
|
+
require "active_model_serializers_validator"
|
6
|
+
|
7
|
+
# Require everything in `spec/support`
|
8
|
+
Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].map(&method(:require))
|
9
|
+
|
10
|
+
# Configure RSpec
|
11
|
+
RSpec.configure do |config|
|
12
|
+
# Modules
|
13
|
+
config.include Fixtures
|
14
|
+
config.include Serializers
|
15
|
+
|
16
|
+
# Hooks
|
17
|
+
config.before(:suite) do
|
18
|
+
class OpenStruct
|
19
|
+
def read_attribute_for_serialization(attribute)
|
20
|
+
self.send(attribute)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveModel::Serializer::Validator do
|
4
|
+
describe :valid? do
|
5
|
+
subject { ActiveModel::Serializer.new(OpenStruct.new(:foo => 'bar')) }
|
6
|
+
before { ActiveModel::Serializer.stub(:valid_against_schema?).and_return(stubbed_validation) }
|
7
|
+
|
8
|
+
context 'with empty errors' do
|
9
|
+
let(:stubbed_validation) { [] }
|
10
|
+
it { should be_valid }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with errors' do
|
14
|
+
let(:stubbed_validation) { ['First error', 'Second error'] }
|
15
|
+
it { should_not be_valid }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe :valid_against_schema? do
|
20
|
+
before do
|
21
|
+
try_remove_const(:ProductSerializer)
|
22
|
+
|
23
|
+
ProductSerializer = Class.new(ActiveModel::Serializer) do
|
24
|
+
self.root = false
|
25
|
+
attributes :id, :name, :price, :tags
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
subject { ActiveModel::Serializer.valid_against_schema?(product_schema, serializer) }
|
30
|
+
let(:serializer) { ProductSerializer.new(serializable) }
|
31
|
+
let(:schema) { product_schema }
|
32
|
+
|
33
|
+
context 'without validaton errors' do
|
34
|
+
let(:serializable) { OpenStruct.new(:id => 4, :name => "Widget", :price => 1200, :tags => %w(foo bar baz)) }
|
35
|
+
it { should be_empty }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with validaton errors' do
|
39
|
+
let(:serializable) { OpenStruct.new(:id => 4, :price => 1200, :tags => %w(foo bar baz)) }
|
40
|
+
it { should_not be_empty }
|
41
|
+
its(:length) { should eql 1 }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe :json_schema do
|
46
|
+
# TODO
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_serializers_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,6 +43,38 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.13'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.13'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: json
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.7.7
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.7.7
|
46
78
|
- !ruby/object:Gem::Dependency
|
47
79
|
name: activesupport
|
48
80
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,6 +132,7 @@ extensions: []
|
|
100
132
|
extra_rdoc_files: []
|
101
133
|
files:
|
102
134
|
- .gitignore
|
135
|
+
- .rspec
|
103
136
|
- Gemfile
|
104
137
|
- LICENSE.md
|
105
138
|
- README.md
|
@@ -108,6 +141,11 @@ files:
|
|
108
141
|
- lib/active_model/serializer/validator/mixin.rb
|
109
142
|
- lib/active_model/serializer/validator/version.rb
|
110
143
|
- lib/active_model_serializers_validator.rb
|
144
|
+
- spec/fixtures/product.jsonschema
|
145
|
+
- spec/spec_helper.rb
|
146
|
+
- spec/support/macros/fixtures.rb
|
147
|
+
- spec/support/macros/serializers.rb
|
148
|
+
- spec/validator_spec.rb
|
111
149
|
homepage: https://github.com/mirego/active_model_serializers_validator
|
112
150
|
licenses:
|
113
151
|
- MIT
|
@@ -123,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
161
|
version: '0'
|
124
162
|
segments:
|
125
163
|
- 0
|
126
|
-
hash: -
|
164
|
+
hash: -3953196887841094679
|
127
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
166
|
none: false
|
129
167
|
requirements:
|
@@ -132,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
170
|
version: '0'
|
133
171
|
segments:
|
134
172
|
- 0
|
135
|
-
hash: -
|
173
|
+
hash: -3953196887841094679
|
136
174
|
requirements: []
|
137
175
|
rubyforge_project:
|
138
176
|
rubygems_version: 1.8.23
|
@@ -140,4 +178,9 @@ signing_key:
|
|
140
178
|
specification_version: 3
|
141
179
|
summary: An extension to ActiveModel::Serializer that validates serializers output
|
142
180
|
against a JSON schema
|
143
|
-
test_files:
|
181
|
+
test_files:
|
182
|
+
- spec/fixtures/product.jsonschema
|
183
|
+
- spec/spec_helper.rb
|
184
|
+
- spec/support/macros/fixtures.rb
|
185
|
+
- spec/support/macros/serializers.rb
|
186
|
+
- spec/validator_spec.rb
|