adonisjs / rfcs

💬 Sharing big changes with community to add them to the AdonisJs eco-system
52 stars 6 forks source link

Add new number's rules for validation #28

Open RomainLanz opened 4 years ago

RomainLanz commented 4 years ago

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
thetutlage commented 3 years ago

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:

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

targos commented 3 years ago

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).

thetutlage commented 3 years ago

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

RomainLanz commented 3 years ago

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

thetutlage commented 3 years ago

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 commented 3 years ago

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.

RomainLanz commented 3 years ago

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
thetutlage commented 3 years ago

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

isInteger

isFloat

isDecimal

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

targos commented 3 years ago

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

thetutlage commented 3 years ago

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.