Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for validation #184

Open
mslinn opened this issue May 10, 2022 Discussed in #183 · 0 comments
Open

Better error message for validation #184

mslinn opened this issue May 10, 2022 Discussed in #183 · 0 comments

Comments

@mslinn
Copy link

mslinn commented May 10, 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

value = @prompt.ask(msg, value: value) do |q|
  q.required true
  q.validate ->(v) { return v.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.required true
  q.messages[:valid?] = check_length(min, max, ????) # Is there some way to pass the current answer to `check_length`?
  q.validate ->(v) { return v.length >= min && v.length <= max }
end

def check_length(min, max, string)
  length = string.length
  if length < min
    "#{min - length} characters too short, please edit"
  elsif length > max
    "#{length - max} characters too long, please edit"
  else
    "#{length} characters, excellent!"
  end
end

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 = lambda do |val|
      puts val.red
      case val.length
      when proc { |n| n < min }
        "Only #{val.length} characters provided, need at least #{min}"
      when proc { |n| n > max }
        "#{val.length} characters provided, maximum allowable is #{max}"
      else
        ''
      end
    end
    value = @prompt.ask(msg, value: value) do |q|
      q.required true
      q.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.

@mslinn mslinn changed the title Better error message for lambda validation Better error message for validation May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant