Skip to content

Commit

Permalink
Document custom class validation in Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
abinoam committed Jan 2, 2023
1 parent ce3481d commit 5b785ac
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ end
cli.ask("Age? ", Integer) { |q| q.in = 0..105 }
cli.ask("Name? (last, first) ") { |q| q.validate = /\A\w+, ?\w+\Z/ }

## Validation with custom class
class ZeroToTwentyFourValidator
def self.valid?(answer)
(0..24).include? answer.to_i
end

def self.inspect
"(0..24) rule"
end
end

cli.ask("What hour of the day is it?: ", Integer) do |q|
q.validate = ZeroToTwentyFourValidator
end

## Validation with Dry::Types
## `Dry::Types` provides a `valid?` method so it can be used effortlessly

require 'dry-type'

module Types
include Dry.Types
end

cli.ask("Type an integer:", Integer) do |q|
q.validate = Types::Coercible::Integer
end

# Type conversion for answers:

Expand Down

0 comments on commit 5b785ac

Please sign in to comment.