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

Add allowString and unsigned options to integer assert #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

madeiras
Copy link

@madeiras madeiras commented Dec 19, 2016

This PR adds a few options to the Integer assert:

  • allowString: This option allows the assert to validate integers represented as strings.
  • unsigned: This option forces only simple unsigned integers.

The question here is if we want to allow integer assert to be a string and the impact for users confronted with an integer assert that forces the input to be a string. Also it would be nice to come to a conclusion on distinguishing between integers with leading zeros or not and if an option should be presented for that matter.

@madeiras madeiras added the enhancement New feature or request label Dec 19, 2016
@madeiras madeiras self-assigned this Dec 19, 2016
@madeiras madeiras force-pushed the feature/add-unsigned-assert branch 2 times, most recently from 0e9179a to 5dc5759 Compare December 19, 2016 13:00
@madeiras madeiras changed the title Add unsigned integer assert Add allowString and unsigned options to integer assert Dec 19, 2016
@madeiras madeiras added the question Further information is requested label Dec 19, 2016
Copy link
Member

@ruimarinho ruimarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR solves some interesting problems of the Integer assert at the cost of a increased complexity and some decisions we need to make.

  1. Do we want to allow strings at all on this assert considering we're validating integers? Could be a contradictory statement, but not unlikely in javascript land.

  2. The current regexp only accepts integers without leading zeros because a number like 01 will be interpreted by javascript as 1, so it wouldn't make sense to accept any other form. The new unsigned regexp, as it is, accepts any form of integer (with or without leading zero), so there is a duality in behaviour which will likely confuse developers.

  3. Would a separate assert make more sense? I.e. something in the line of Numeric?

@@ -168,6 +168,10 @@ Tests if the value is a valid hash.
### Integer
Tests if the value is an integer.

#### Arguments
- `allowString` (optional) - whether the assert accepts string inputs or not.
- `unsigned` (optional) - whether the assert should consider only unsigned values or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should accept?

const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
const int = {
signed: /^(?:[-+]?(?:0|[1-9][0-9]*))$/,
unsigned: /^(?:\d+)$/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep same style? [0-9] instead of \d+?

@@ -168,6 +168,10 @@ Tests if the value is a valid hash.
### Integer
Tests if the value is an integer.

#### Arguments
- `allowString` (optional) - whether the assert accepts string inputs or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strict instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants