rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Add new number's rules for validation

Open RomainLanz opened this issue 5 years ago • 10 comments

Rendered

Introducing new rules for number validation.

Name Description
integer Verify that the number is an integer
decimal Verify that the number has a maximum of X decimal
positive Verify that the number is positive
negative Verify that the number is negative

RomainLanz avatar Sep 07 '20 10:09 RomainLanz

Couple of questions.

Decimal

You proposed to use Regex for matching the decimal places. But what will be the value after casting the value? For example:

  • Request sends in a string representation of a number '22.00'.
  • We use the regex to validate the value has 2 decimals.
  • We cast the value to a number using Number('22.00')
  • The user after validation gets 22 and not 22.00.

Regarding positive/negative over signed/unsigned

Don't you think developers are very much aware of signed/unsigned values? Databases use them and even Javascript own API uses Math.sign to know if the value is signed or unsigned

thetutlage avatar Sep 25 '20 09:09 thetutlage

signed/unsigned does not usually mean the same as positive/negative. In a database, a signed value can be positive (when the sign bit is off).

targos avatar Sep 25 '20 09:09 targos

@targos Can you link me to some doc explaining this a bit in depth? Really interested to know the difference between them 😄

thetutlage avatar Sep 25 '20 10:09 thetutlage

You proposed to use Regex for matching the decimal places. But what will be the value after casting the value?

The rule will verify that the number of decimal don't go over the given number. Using your example, the rule would pass because 22 is valid since it doesn't have more than 2 decimals.

In fact, 22 and 22.00 are exactly the same number for the JavaScript engine.

22 === 22.00 // true

Don't you think developers are very much aware of signed/unsigned values? Databases use them and even Javascript own API uses Math.sign to know if the value is signed or unsigned

As @targos said, a signed number can be both positive and negative (we know it's one or the other because of the signed bit), unsigned means it's always positive because there's no signed bit to determine if the number can be something else.

Here's the Wikipedia article on signed bit and unsigned integer

RomainLanz avatar Sep 25 '20 11:09 RomainLanz

In fact, 22 and 22.00 are exactly the same number for the JavaScript engine.

What's the use case for validating decimal places when the casted value can be different. I am struggling to see when I will say I want 22.00 and 22

thetutlage avatar Sep 25 '20 11:09 thetutlage

As @targos said, a signed number can be both positive and negative (we know it's one or the other because of the signed bit), unsigned means it's always positive because there's no signed bit to determine if the number can be something else.

I get it now. So yes, having a positive or negative seems more helpful than having signed or unsigned.

thetutlage avatar Sep 25 '20 11:09 thetutlage

In fact, 22 and 22.00 are exactly the same number for the JavaScript engine.

What's the use case for validating decimal places when the casted value can be different. I am struggling to see when I will say I want 22.00 and 22

This rule will be useful when people need to do math with the received number.

Let's say you don't want to store decimal number inside your database, therefore you're doing something like xxx * 100 to convert a two decimals number to an integer.

22.00 * 100 // 2200
22 * 100 // 2200
22.22 * 100 // 2222
22.222 * 100 // 2222.2(000000000003) 

If we have more than two decimals, our code will fail. There are two ways to avoid this failure:

  1. We use Math.floor() or Math.ceil() before
  2. We don't allow more than X decimals

RomainLanz avatar Sep 25 '20 11:09 RomainLanz

Was planning to start the work on this. Do you guys think, following is the right representation

isInteger

  • 22.00
  • 22
  • -1
  • -1.00
  • 22.10
  • .3

isFloat

  • 22.10
  • -22.10
  • .3
  • 22.00
  • -22.00
  • -22

isDecimal

  • 22.10
  • .3
  • -22.10
  • 22.00
  • -22.00
  • -22
  • 22

positive and negative can be used with decimal, float and integer

thetutlage avatar Feb 07 '21 09:02 thetutlage

What would be the use case for isFloat? I can't think of a case where you absolutely want something after the comma that is not zero

targos avatar Feb 07 '21 09:02 targos

What would be the use case for isFloat? I can't think of a case where you absolutely want something after the comma that is not zero

My personal use cases with numbers are very minimal, so I don't have to good answer for that. I added this to handle another use case (which is not personally aware of)

Also, we use this library in AdonisJS validator and they have two different methods called isDecimal and isFloat. So that was also an inspiration.

thetutlage avatar Feb 07 '21 09:02 thetutlage