You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to verify that the provided answer is within the range of allowable string lengths (min..max). This was my first attempt:
value=@prompt.ask(msg,value: value)do |q|
q.requiredtrueq.validate->(v){returnv.length >= min && v.length <= max}end
When the input is not within the range of min characters to max characters in length, the error message is: The answer is invalid (must match #<Proc:0x00007f395836e410 /jekyll-new_post/lib/jekyll/new_post.rb:131 (lambda)>). That is a horrible error message.
Next, I tried to write the following, but found I was unable to access the current answer while preparing q.messages[:valid?]:
value=@prompt.ask(msg,value: value)do |q|
q.requiredtrueq.messages[:valid?]=check_length(min,max, ????) # Is there some way to pass the current answer to `check_length`?q.validate ->(v){returnv.length >= min && v.length <= max}enddefcheck_length(min,max,string)length=string.lengthiflength < min"#{min - length} characters too short, please edit"elsiflength > max"#{length - max} characters too long, please edit"else"#{length} characters, excellent!"endend
Is there some way to use q.messages[:valid] so that a better error message can be provided? If not, is there a way to invoke q.validate to avoid the horrible message shown?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to verify that the provided answer is within the range of allowable string lengths (
min..max
). This was my first attempt:When the input is not within the range of
min
characters tomax
characters in length, the error message is:The answer is invalid (must match #<Proc:0x00007f395836e410 /jekyll-new_post/lib/jekyll/new_post.rb:131 (lambda)>)
. That is a horrible error message.Next, I tried to write the following, but found I was unable to access the current answer while preparing
q.messages[:valid?]
:Is there some way to use
q.messages[:valid]
so that a better error message can be provided? If not, is there a way to invokeq.validate
to avoid the horrible message shown?Beta Was this translation helpful? Give feedback.
All reactions