-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from sergiobayona/use_activemodel
Use activemodel
- Loading branch information
Showing
8 changed files
with
193 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module EasyTalk | ||
class SchemaErrorsMapper | ||
def initialize(errors) | ||
@errors = errors.to_a | ||
end | ||
|
||
def errors | ||
@errors.each_with_object({}) do |error, hash| | ||
if error['data_pointer'].present? | ||
key = error['data_pointer'].split('/').compact_blank.join('.') | ||
hash[key] = error['error'] | ||
else | ||
error['details']['missing_keys'].each do |missing_key| | ||
message = "#{error['error'].split(':').first}: #{missing_key}" | ||
hash[missing_key] = message | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module EasyTalk | ||
VERSION = '0.1.10' | ||
VERSION = '0.2.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe 'validing json' do | ||
let(:user) do | ||
Class.new do | ||
include EasyTalk::Model | ||
|
||
validates :age, comparison: { greater_than: 21 } | ||
|
||
validate do |person| | ||
MyValidator.new(person).validate | ||
end | ||
|
||
class MyValidator | ||
def initialize(person) | ||
@person = person | ||
end | ||
|
||
def validate | ||
@person.errors.add(:email, 'must end with @test.com') unless @person.email[:address].ends_with?('@test.com') | ||
end | ||
end | ||
|
||
def self.name | ||
'User' | ||
end | ||
|
||
define_schema do | ||
property :name, String | ||
property :age, Integer | ||
property :height, Float | ||
property :email, :object do | ||
property :address, String | ||
property :verified, T::Boolean | ||
end | ||
end | ||
end | ||
end | ||
|
||
it 'errors on missing email' do | ||
jim = user.new(name: 'Jim', age: 30, height: 5.9, email: { address: '[email protected]', verified: false }) | ||
expect(jim.valid?).to be true | ||
end | ||
|
||
it 'errors on invalid age' do | ||
jim = user.new(name: 'Jim', age: 18, height: 5.9, email: { address: '[email protected]', verified: false }) | ||
expect(jim.valid?).to be false | ||
expect(jim.errors.size).to eq(1) | ||
expect(jim.errors[:age]).to eq(['must be greater than 21']) | ||
end | ||
|
||
it 'errors on invalid email' do | ||
jim = user.new(name: 'Jim', age: 30, height: 5.9, email: { address: '[email protected]', verified: false }) | ||
expect(jim.valid?).to be false | ||
expect(jim.errors.size).to eq(1) | ||
expect(jim.errors[:email]).to eq(['must end with @test.com']) | ||
end | ||
|
||
it 'runs after validation callback' do | ||
jim = user.new(name: 'Jim', age: 30, height: 5.9, email: { address: '[email protected]', verified: false }) | ||
expect(jim.age).to eq(30) | ||
expect(jim.valid?).to be true | ||
expect(jim.age).to eq(500) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,16 +79,6 @@ def self.name | |
end | ||
end | ||
|
||
describe 'validating a JSON object' do | ||
it 'validates the JSON object against the schema' do | ||
expect(user.validate_json({ name: 'John', age: 21, email: { address: '[email protected]', verified: 'false' } })).to eq(true) | ||
end | ||
|
||
it 'fails validation of the JSON object against the schema' do | ||
expect(user.validate_json({ name: 'John', age: '21' })).to eq(false) | ||
end | ||
end | ||
|
||
context 'when the class name is nil' do | ||
let(:user) do | ||
Class.new do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe 'validing json' do | ||
let(:user) do | ||
Class.new do | ||
include EasyTalk::Model | ||
|
||
def self.name | ||
'User' | ||
end | ||
|
||
define_schema do | ||
property :name, String | ||
property :age, Integer | ||
property :height, Float | ||
property :email, :object do | ||
property :address, String | ||
property :verified, String | ||
end | ||
end | ||
end | ||
end | ||
|
||
it 'errors on missing email' do | ||
jim = user.new(name: 'Jim', age: 30, height: 5.9) | ||
expect(jim.valid?).to be false | ||
expect(jim.errors.size).to eq(1) | ||
expect(jim.errors[:email]).to eq(['object at root is missing required properties: email']) | ||
end | ||
|
||
it 'errors on invalid age, missing email' do | ||
jim = user.new(name: 'Jim', age: 'thirty', height: 4.5, email: { address: '[email protected]', verified: 'true' }) | ||
expect(jim.valid?).to be false | ||
expect(jim.errors.size).to eq(1) | ||
expect(jim.errors[:age]).to eq(['value at `/age` is not an integer']) | ||
end | ||
|
||
it 'errors on missing age email and height' do | ||
jim = user.new(name: 'Jim', email: { address: '[email protected]', verified: 'true' }) | ||
expect(jim.valid?).to be false | ||
expect(jim.errors.size).to eq(2) | ||
expect(jim.errors[:age]).to eq(['object at root is missing required properties: age']) | ||
expect(jim.errors[:height]).to eq(['object at root is missing required properties: height']) | ||
end | ||
|
||
it 'errors on invalid name, email and age' do | ||
jim = user.new(name: nil, email: 'test@jim', age: 'thirty') | ||
expect(jim.valid?).to be false | ||
expect(jim.errors[:name]).to eq(['value at `/name` is not a string']) | ||
expect(jim.errors[:email]).to eq(['value at `/email` is not an object']) | ||
expect(jim.errors[:age]).to eq(['value at `/age` is not an integer']) | ||
end | ||
|
||
it 'errors on verified' do | ||
jim = user.new(name: 'Jim', email: { address: '[email protected]', verified: false }, age: 21, height: 5.9) | ||
expect(jim.valid?).to be(false) | ||
expect(jim.errors['email.verified']).to eq(['value at `/email/verified` is not a string']) | ||
end | ||
end |