[POC] Add a new Strict parser #164
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Opening this PR to start a discussion on the current approach to parsing.
Over time I have worked/consulted for a few companies that use this gem internally. However all of them had to monkey-patch the gem in order to significantly restrict string parsing (especially when it comes to user input and even developer input).
The main issues being:
29.01.2023
,29,12,12
, etc)one1two2three3
)10,000
and10,00
yield different results)I think that the current approach is "find any signs of monetary amount in the input". While this works for some use-cases, I would argue that this might not be the best default behaviour. For any company providing financial services or even consumer retail this unfortunately is straight unacceptable.
Therefore I'm proposing adding an optional strict parser (and potentially making it default). This parser takes a different approach — it finds known good partial matches (currency, symbol, amount, etc) and then checks it against allowed formats (e.g.
[:symbol, :amount]
for$99.99
). This allows gem users to ensure that only allowed formats are matched and no unwanted characters were present.The PR is a proof of concept and passes most specs with expected exceptions related specifically to:
€10,000
as 10K, instead of 10 (should be tweak-able)£.45B
as this is a rarely accepted formatnil
when parsingnil
(happy to change this toMoney.empty
)19.12.89
as this doesn't look like an amountThis is a big(ish) change, so I'd like to gather more input from others before I make further progress.
Any questions, concerns and feedback is very welcome! 🙌