Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

@Digits fraction = 0 not validating correctly #87

Open
conalryan opened this issue Jul 8, 2015 · 2 comments
Open

@Digits fraction = 0 not validating correctly #87

conalryan opened this issue Jul 8, 2015 · 2 comments

Comments

@conalryan
Copy link

Setting annotation in Java

@digits(integer = 3, fraction = 0, message = "no decimals allowed")
private BigDecimal percent;

is not validating correctly. The valdr validation is not triggered when entering x.0.
Possible solution is:

angular.module('valdr')

.factory('valdrDigitsValidator', ['valdrUtil', function (valdrUtil) {

var isNotLongerThan = function (valueAsString, maxLengthConstraint) {
  return !valueAsString ? true : valueAsString.length <= maxLengthConstraint;
};

var doValidate = function (value, constraint) {
  var integerConstraint = constraint.integer,
    fractionConstraint = constraint.fraction,
    cleanValueAsString, integerAndFraction;

  integerAndFraction = value.toString().split('.');

  return isNotLongerThan(integerAndFraction[0], integerConstraint) &&
    isNotLongerThan(integerAndFraction[1], fractionConstraint);
};
@marcelstoer
Copy link
Collaborator

Thanks for the bug report. I added a test case to digitsValidator.spec.js and you're right, it doesn't work as expected.

constraint.fraction = 0;
expect(digitsValidator.validate(10.0, constraint)).toBe(false);

fails because Number(10.0).toString() returns 10 since Number(10.0) and Number(10) are exactly the same thing.

It's quite likely that this is a regression introduced with 81245b0. The main question is: what's the universally applicable JavaScript code to split a number into integer and fraction regardless of (locale dependent) thousand- and decimal separator?

@sandermak
Copy link

We are still running into this issue, have there been any updates?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants