active_model_serializers_validator 1.0.1 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 1.9.3
6
+
7
+ script: "echo 'DO IT' && bundle exec rake spec"
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # ActiveModel::Serializer::Validator
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/active_model_serializers_validator.png)](https://rubygems.org/gems/active_model_serializers_validator)
4
+ [![Build Status](https://travis-ci.org/mirego/active_model_serializers_validator.png?branch=master)](https://travis-ci.org/mirego/active_model_serializers_validator)
5
+
3
6
  This gem adds JSON schema validations for the JSON output generated by an `ActiveModel::Serializer`.
4
7
 
5
8
  ## Installation
@@ -57,11 +60,6 @@ serializer.errors
57
60
 
58
61
  ## About Mirego
59
62
 
60
- Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun.
61
- We proudly built mobile applications for
62
- [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"),
63
- [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"),
64
- [Android](http://mirego.com/en/android-app-development/ "Android application development"),
65
- [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"),
66
- [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and
67
- [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development").
63
+ Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development") in beautiful Quebec City.
64
+
65
+ We also love [open-source software](http://open.mirego.com/) and we try to extract as much code as possible from our projects to give back to the community.
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- task :default => :spec
4
+ task default: :spec
5
5
 
6
6
  desc "Run all specs"
7
7
  RSpec::Core::RakeTask.new(:spec) do |task|
@@ -0,0 +1,8 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module Validator
4
+ class InvalidSchemaError < StandardError
5
+ end
6
+ end
7
+ end
8
+ end
@@ -22,7 +22,14 @@ module ActiveModel
22
22
  end
23
23
 
24
24
  return @_json_schema unless value
25
- @_json_schema = value
25
+
26
+ if !value.is_a?(String) && !value.is_a?(Hash)
27
+ raise InvalidSchemaError.new('Schema must be a path to a file or a Hash.')
28
+ elsif value.is_a?(String) && !File.exists?(value)
29
+ raise InvalidSchemaError.new('Schema file does not exist.')
30
+ else
31
+ @_json_schema = value
32
+ end
26
33
  end
27
34
 
28
35
  # Validate the rendered data against a JSON schema file and
@@ -1,7 +1,7 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
3
  module Validator
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1'
5
5
  end
6
6
  end
7
7
  end
@@ -2,6 +2,7 @@ require 'json-schema'
2
2
  require 'active_support'
3
3
  require 'active_support/core_ext/object/to_json'
4
4
  require 'active_model/serializer'
5
+ require 'active_model/serializer/validator/errors'
5
6
  require 'active_model/serializer/validator/mixin'
6
7
  require 'active_model/serializer/validator/version'
7
8
 
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe ActiveModel::Serializer::Validator do
4
4
  describe :valid? do
5
- subject { ActiveModel::Serializer.new(OpenStruct.new(:foo => 'bar')) }
5
+ subject { ActiveModel::Serializer.new(OpenStruct.new(foo: 'bar')) }
6
6
  before { ActiveModel::Serializer.stub(:valid_against_schema?).and_return(stubbed_validation) }
7
7
 
8
8
  context 'with empty errors' do
@@ -28,21 +28,56 @@ describe ActiveModel::Serializer::Validator do
28
28
 
29
29
  subject { ActiveModel::Serializer.valid_against_schema?(product_schema, serializer) }
30
30
  let(:serializer) { ProductSerializer.new(serializable) }
31
- let(:schema) { product_schema }
31
+ let(:schema) { product_schema }
32
32
 
33
33
  context 'without validaton errors' do
34
- let(:serializable) { OpenStruct.new(:id => 4, :name => "Widget", :price => 1200, :tags => %w(foo bar baz)) }
34
+ let(:serializable) { OpenStruct.new(id: 4, name: "Widget", price: 1200, tags: %w(foo bar baz)) }
35
35
  it { should be_empty }
36
36
  end
37
37
 
38
38
  context 'with validaton errors' do
39
- let(:serializable) { OpenStruct.new(:id => 4, :price => 1200, :tags => %w(foo bar baz)) }
39
+ let(:serializable) { OpenStruct.new(id: 4, price: 1200, tags: %w(foo bar baz)) }
40
40
  it { should_not be_empty }
41
41
  its(:length) { should eql 1 }
42
42
  end
43
43
  end
44
44
 
45
45
  describe :json_schema do
46
- # TODO
46
+ subject { ProductSerializer.new(serializable) }
47
+ let(:set_schema!) { ProductSerializer.json_schema schema_file }
48
+
49
+ before do
50
+ try_remove_const(:ProductSerializer)
51
+
52
+ ProductSerializer = Class.new(ActiveModel::Serializer) do
53
+ self.root = false
54
+ attributes :id, :name, :price, :tags
55
+ end
56
+ end
57
+
58
+ context 'with existing file' do
59
+ let(:schema_file) { File.expand_path("../fixtures/product.jsonschema", __FILE__) }
60
+ before { set_schema! }
61
+
62
+ context 'with valid serializable' do
63
+ let(:serializable) { OpenStruct.new(id: 4, name: "Widget", price: 1200, tags: %w(foo bar baz)) }
64
+ it { should be_valid }
65
+ end
66
+
67
+ context 'with invalid serializable' do
68
+ let(:serializable) { OpenStruct.new }
69
+ it { should_not be_valid }
70
+ end
71
+ end
72
+
73
+ context 'with unknown file as schema' do
74
+ let(:schema_file) { File.expand_path("../fixtures/non-existing-file.jsonschema", __FILE__) }
75
+ it { expect { set_schema! }.to raise_error ActiveModel::Serializer::Validator::InvalidSchemaError, 'Schema file does not exist.' }
76
+ end
77
+
78
+ context 'with a non-supported type as schema' do
79
+ let(:schema_file) { true }
80
+ it { expect { set_schema! }.to raise_error ActiveModel::Serializer::Validator::InvalidSchemaError, 'Schema must be a path to a file or a Hash.' }
81
+ end
47
82
  end
48
83
  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: 1.0.1
4
+ version: '1.1'
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-07-09 00:00:00.000000000 Z
12
+ date: 2013-08-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -133,11 +133,13 @@ extra_rdoc_files: []
133
133
  files:
134
134
  - .gitignore
135
135
  - .rspec
136
+ - .travis.yml
136
137
  - Gemfile
137
138
  - LICENSE.md
138
139
  - README.md
139
140
  - Rakefile
140
141
  - active_model_serializers_validator.gemspec
142
+ - lib/active_model/serializer/validator/errors.rb
141
143
  - lib/active_model/serializer/validator/mixin.rb
142
144
  - lib/active_model/serializer/validator/version.rb
143
145
  - lib/active_model_serializers_validator.rb
@@ -161,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
163
  version: '0'
162
164
  segments:
163
165
  - 0
164
- hash: -3800885852508204265
166
+ hash: -3032059615404729066
165
167
  required_rubygems_version: !ruby/object:Gem::Requirement
166
168
  none: false
167
169
  requirements:
@@ -170,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
172
  version: '0'
171
173
  segments:
172
174
  - 0
173
- hash: -3800885852508204265
175
+ hash: -3032059615404729066
174
176
  requirements: []
175
177
  rubyforge_project:
176
178
  rubygems_version: 1.8.23