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

Integer filter accepts non-numeric strings. #83

Open
bobjflong opened this issue Nov 20, 2014 · 9 comments
Open

Integer filter accepts non-numeric strings. #83

bobjflong opened this issue Nov 20, 2014 · 9 comments

Comments

@bobjflong
Copy link

Eg. '123foo' gets converted to 123, due to the use of to_i. I think that it would be preferable for the integer filter to do something like:

begin
  data = Integer(data)
rescue ArgumentError
  return [data, :integer]
end

Let me know what you think, I can cut a PR if you want.

@jkogara
Copy link

jkogara commented May 14, 2015

@bobjflong This would work for you I guess? #89

@bobjflong
Copy link
Author

@jkogara looks like it!

@lorcan
Copy link
Contributor

lorcan commented Apr 25, 2016

I've added a failing test case to demonstrate this issue.

The root cause of the problem is here, where we perform a to_i conversation after doing a simple regex validation (/^-?\d/):

if !data.is_a?(Fixnum)
  if data.is_a?(String) && data =~ /^-?\d/
    data = data.to_i
  else
    return [data, :integer]
  end
end

This can lead to some unexpected behaviour, as the failing test demonstrates. I think that while #89 allows us to bypass that step if we pass a strict parameter it might be better to be strict by default. What about a conversion like this:

if !data.is_a?(Fixnum)
  if data.is_a?(String)
    begin
      data = Integer(data)
    rescue ArgumentError
      return [data, :integer]
    end
  else
    return [data, :integer]
  end
end

rather than running the string through a regex and then a to_i?

@patrickod
Copy link
Collaborator

patrickod commented Apr 25, 2016

I like the idea of a strict by default integer filter, especially if it prevents strings like 10abc from being coerced in unexpected ways. I don't think it's that intuitive that you'd pass a string value with mixed content and somehow get a number out the other side as a value on which to operate. Given that this would be a backward-incompatible change, maybe we could use this as a means to go to 1.x.x ?

lorcan added a commit to lorcan/mutations that referenced this issue May 3, 2016
@lorcan
Copy link
Contributor

lorcan commented May 3, 2016

#99 now has a failing test and includes the proposed solution above. This is a breaking change so we could hold it for 1.x.x.

@abhitrivedi
Copy link

Hello from 2019!

I understand this is an old thread but just curious whether there was any traction on this issue. IOW, did we add a strict option to integers?

lorcan added a commit to lorcan/mutations that referenced this issue Sep 26, 2019
@lorcan
Copy link
Contributor

lorcan commented Sep 26, 2019

Hi @abhitrivedi, I've refreshed #99 to bring it up to date. I agree with @patrickod that rather than adding a strict option we should consider a breaking change and strictly enforce integers.

It's surprising that strings like 6iamnotanumber get converted to numbers like 6.

How would you feel about bumping mutations to 1.0 and enforcing strict number parsing? I'm happy to help :-D

(failing that we could introduce a strict option as suggested by #89, as part of a minor bump, but it feels like unnecessary complexity)

@abhitrivedi
Copy link

ping: @cypriss @eugeneius

@abhitrivedi
Copy link

How would you feel about bumping mutations to 1.0 and enforcing strict number parsing? I'm happy to help :-D

Since this is still a zero major version gem, the public API doesn't need to be backward compatible. I suggest we keep the zero major version until the authors decide that the api is stable enough.

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

5 participants