Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve composition, add
.rule
macro
Also allow to override `#validate` in modules. Closes #1
- Loading branch information
Showing
20 changed files
with
467 additions
and
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "../../spec_helper" | ||
|
||
record GTObject, x : Int32 | ||
|
||
struct GTObject | ||
include Validations | ||
validate x, gt: 5 | ||
end | ||
|
||
describe ":gt" do | ||
context "when valid" do | ||
o = GTObject.new(6) | ||
|
||
it do | ||
o.valid?.should be_true | ||
o.invalid_attributes.empty?.should be_true | ||
end | ||
end | ||
|
||
context "when invalid" do | ||
o = GTObject.new(5) | ||
|
||
it do | ||
o.valid?.should be_false | ||
o.invalid_attributes.should eq ({"x" => ["must be greater than 5"]}) | ||
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "../../spec_helper" | ||
|
||
record GTEObject, x : Int32 | ||
|
||
struct GTEObject | ||
include Validations | ||
validate x, gte: 5 | ||
end | ||
|
||
describe ":gte" do | ||
context "when valid" do | ||
o = GTEObject.new(5) | ||
|
||
it do | ||
o.valid?.should be_true | ||
o.invalid_attributes.empty?.should be_true | ||
end | ||
end | ||
|
||
context "when invalid" do | ||
o = GTEObject.new(4) | ||
|
||
it do | ||
o.valid?.should be_false | ||
o.invalid_attributes.should eq ({"x" => ["must be greater than or equal to 5"]}) | ||
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "../../spec_helper" | ||
|
||
record InObject, x : Int32 | ||
|
||
struct InObject | ||
include Validations | ||
validate x, in: {1, 2} | ||
end | ||
|
||
describe ":in" do | ||
it do | ||
InObject.new(1).valid?.should be_true | ||
InObject.new(3).valid?.should be_false | ||
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "../../spec_helper" | ||
|
||
record IsObject, x : Int32 | ||
|
||
struct IsObject | ||
include Validations | ||
validate x, is: 1 | ||
end | ||
|
||
describe ":is" do | ||
it do | ||
IsObject.new(1).valid?.should be_true | ||
IsObject.new(2).valid?.should be_false | ||
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "../../spec_helper" | ||
|
||
record LTObject, x : Int32 | ||
|
||
struct LTObject | ||
include Validations | ||
validate x, lt: 5 | ||
end | ||
|
||
describe ":lt" do | ||
context "when valid" do | ||
o = LTObject.new(4) | ||
|
||
it do | ||
o.valid?.should be_true | ||
o.invalid_attributes.empty?.should be_true | ||
end | ||
end | ||
|
||
context "when invalid" do | ||
o = LTObject.new(5) | ||
|
||
it do | ||
o.valid?.should be_false | ||
o.invalid_attributes.should eq ({"x" => ["must be less than 5"]}) | ||
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "../../spec_helper" | ||
|
||
record LTEObject, x : Int32 | ||
|
||
struct LTEObject | ||
include Validations | ||
validate x, lte: 5 | ||
end | ||
|
||
describe ":lte" do | ||
context "when valid" do | ||
o = LTEObject.new(5) | ||
|
||
it do | ||
o.valid?.should be_true | ||
o.invalid_attributes.empty?.should be_true | ||
end | ||
end | ||
|
||
context "when invalid" do | ||
o = LTEObject.new(6) | ||
|
||
it do | ||
o.valid?.should be_false | ||
o.invalid_attributes.should eq ({"x" => ["must be less than or equal to 5"]}) | ||
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "../../spec_helper" | ||
|
||
record RegexObject, x : String | ||
|
||
struct RegexObject | ||
include Validations | ||
validate x, regex: /@/ | ||
end | ||
|
||
describe ":regex" do | ||
it do | ||
RegexObject.new("foo@bar").valid?.should be_true | ||
RegexObject.new("foo").valid?.should be_false | ||
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require "../../spec_helper" | ||
|
||
record NumberSizeObject, x : String | ||
record RangeSizeObject, x : String | ||
|
||
struct NumberSizeObject | ||
include Validations | ||
validate x, size: 1 | ||
end | ||
|
||
struct RangeSizeObject | ||
include Validations | ||
validate x, size: (1..2) | ||
end | ||
|
||
describe ":size" do | ||
context "with number" do | ||
context "when valid" do | ||
o = NumberSizeObject.new("a") | ||
|
||
it do | ||
o.valid?.should be_true | ||
o.invalid_attributes.empty?.should be_true | ||
end | ||
end | ||
|
||
context "when invalid" do | ||
o = NumberSizeObject.new("aa") | ||
|
||
it do | ||
o.valid?.should be_false | ||
o.invalid_attributes.should eq ({"x" => ["must have size equal to 1"]}) | ||
end | ||
end | ||
end | ||
|
||
context "with range" do | ||
context "when valid" do | ||
o = RangeSizeObject.new("a") | ||
|
||
it do | ||
o.valid?.should be_true | ||
o.invalid_attributes.empty?.should be_true | ||
end | ||
end | ||
|
||
context "when invalid" do | ||
o = RangeSizeObject.new("aaa") | ||
|
||
it do | ||
o.valid?.should be_false | ||
o.invalid_attributes.should eq ({"x" => ["must have size in 1..2"]}) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.