This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from mcorp/validations-0.6.0
Update to supports new version of hanami-validations
- Loading branch information
Showing
18 changed files
with
61 additions
and
244 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ And then execute: | |
Or install it yourself as: | ||
|
||
$ gem install shoulda-hanami | ||
|
||
## Configure | ||
|
||
Create file `spec/support/shoulda_hanami.rb` with: | ||
|
@@ -38,17 +38,18 @@ end | |
class Person | ||
include Hanami::Validations | ||
|
||
attribute :email, type: String, presence: true, format: /@/ | ||
attribute :name, type: String, size: 5..50 | ||
attribute :password, type: String, size: 10 | ||
attribute :birthday, type: Date | ||
attribute :created_at, type: DateTime | ||
attribute :state, type: String, inclusion: %w(PR SC SP) | ||
attribute :year, type: Integer, inclusion: 1979..1990 | ||
validations do | ||
required(:email) { format?(/@/) } | ||
required(:name) { size?(5..50) } | ||
required(:password) { size?(10) } | ||
required(:state) { included_in?(%w(PR SC SP)) } | ||
required(:year) { included_in?(1979..1990) } | ||
end | ||
end | ||
``` | ||
|
||
### Spec | ||
|
||
```ruby | ||
# allow_value | ||
it { is_expected.to allow_value("[email protected]").for(:email) } | ||
|
@@ -61,13 +62,6 @@ it { is_expected.to validate_presence_of(:email) } | |
it { is_expected.to validate_length_of(:name).is_at_least(5).is_at_most(50) } | ||
it { is_expected.to validate_length_of(:password).is_equal_to(10) } | ||
|
||
# coerces | ||
it { is_expected.to coerce_attribute(:email).to(String) } | ||
it { is_expected.to coerce_attribute(:name).to(String) } | ||
it { is_expected.to coerce_attribute(:password).to(String) } | ||
it { is_expected.to coerce_attribute(:birthday).to(Date) } | ||
it { is_expected.to coerce_attribute(:created_at).to(DateTime) } | ||
|
||
# inclusion | ||
it { is_expected.to validate_inclusion_of(:state).in_array(%w(PR SC SP)) } | ||
it { is_expected.to validate_inclusion_of(:year).in_array(1979..1990) } | ||
|
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Shoulda | ||
module Hanami | ||
VERSION = '0.1.0'.freeze | ||
VERSION = '0.2.0'.freeze | ||
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
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,7 @@ | ||
class WithValidationFormatModel | ||
include Hanami::Validations | ||
|
||
attribute :email, format: /@/ | ||
validations do | ||
optional(:email) { format?(/@/) } | ||
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,7 @@ | ||
class WithValidationInclusionModel | ||
include Hanami::Validations | ||
|
||
attribute :state, inclusion: %w(PR SC SP) | ||
validations do | ||
optional(:state).filled(inclusion?: %w(PR SC SP)) | ||
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,6 +1,8 @@ | ||
class WithValidationLengthModel | ||
include Hanami::Validations | ||
|
||
attribute :name_equal_10, size: 10 | ||
attribute :name_between_2_10, size: 2..10 | ||
validations do | ||
optional(:name_equal_10).filled(size?: 10) | ||
optional(:name_between_2_10).filled(size?: 2..10) | ||
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,7 @@ | ||
class WithValidationPresenceModel | ||
include Hanami::Validations | ||
|
||
attribute :email, presence: true | ||
validations do | ||
required(:email).filled | ||
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,17 +1,19 @@ | ||
class WithValidationTypeModel | ||
include Hanami::Validations | ||
|
||
attribute :attr_array, type: Array | ||
attribute :attr_bigdecimal, type: BigDecimal | ||
attribute :attr_boolean, type: Boolean | ||
attribute :attr_date, type: Date | ||
attribute :attr_datetime, type: DateTime | ||
attribute :attr_float, type: Float | ||
attribute :attr_hash, type: Hash | ||
attribute :attr_integer, type: Integer | ||
attribute :attr_pathname, type: Pathname | ||
attribute :attr_set, type: Set | ||
attribute :attr_string, type: String | ||
attribute :attr_symbol, type: Symbol | ||
attribute :attr_time, type: Time | ||
validations do | ||
optional(:attr_array) { array? } | ||
optional(:attr_bigdecimal) { decimal? } | ||
optional(:attr_boolean) { bool? } | ||
optional(:attr_date) { date? } | ||
optional(:attr_datetime).filled(type?: DateTime) | ||
optional(:attr_float) { float? } | ||
optional(:attr_hash) { hash? } | ||
optional(:attr_integer) { int? } | ||
optional(:attr_pathname).filled(type?: Pathname) | ||
optional(:attr_set).filled(type?: Set) | ||
optional(:attr_string) { str? } | ||
optional(:attr_symbol).filled(type?: Symbol) | ||
optional(:attr_time) { time? } | ||
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,7 @@ | ||
class WithoutValidationModel | ||
include Hanami::Validations | ||
|
||
attribute :email | ||
validations do | ||
optional(:email) | ||
end | ||
end |
File renamed without changes.
Oops, something went wrong.