diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2023-01-01 21:59:51 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2023-01-02 18:04:23 -0300 |
commit | 5b785aca521b1a38446819da3544a4e0b4c76779 (patch) | |
tree | fd1b98100c0c77cd86c33619510f000c7eb7d984 | |
parent | ce3481d8c2fb48e7ced5260ffdddbb30d024f11c (diff) | |
download | highline-5b785aca521b1a38446819da3544a4e0b4c76779.tar.gz |
Document custom class validation in Readme
-rw-r--r-- | README.md | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -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: |