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
Originally posted by mslinn May 10, 2022
I want to verify that the provided answer is within the range of allowable string lengths (min..max).
Attempt 1
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 not a user-friendly error message.
Attempt 2
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 user-unfriendly message shown?
Attempt 3
I tried validating using this regex:
q.validate(/\A.{#{min},#{max}}\Z/)
This validation works, but the error message is not user-friendly:
Your answer is invalid (must match /\A.{30,60}\Z/)
Attempt 4
Next, I tried a lambda:
error_msg=lambdado |val|
putsval.redcaseval.lengthwhenproc{ |n| n < min}"Only #{val.length} characters provided, need at least #{min}"whenproc{ |n| n > max}"#{val.length} characters provided, maximum allowable is #{max}"else''endendvalue=@prompt.ask(msg,value: value)do |q|
q.requiredtrueq.validate(/\A.{#{min},#{max}}\Z/,error_msg.call('%{value}'))end
... but the screen would tear up for input that was too long, and '%{value}' was not expanded.
The text was updated successfully, but these errors were encountered:
mslinn
changed the title
Better error message for lambda validation
Better error message for validation
May 13, 2022
Discussed in #183
Originally posted by mslinn May 10, 2022
I want to verify that the provided answer is within the range of allowable string lengths (
min..max
).Attempt 1
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 not a user-friendly error message.Attempt 2
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 user-unfriendly message shown?Attempt 3
I tried validating using this regex:
This validation works, but the error message is not user-friendly:
Attempt 4
Next, I tried a lambda:
... but the screen would tear up for input that was too long, and
'%{value}'
was not expanded.The text was updated successfully, but these errors were encountered: