Repository: baianat/vee-validate
Pull request #2057 (2019-05-03 23:52:08)

fix: add exponential notation to decimal rule
🔎 __Overview__

This PR  adds/fixes the scientific/exponential notation in the decimal validator for very large and small numbers. e.g. 1.3e-13, 4e15, 6.5e12, etc.


codecov[bot]: # [Codecov](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=h1) Report
> Merging [#2057](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=desc) into [master](https://codecov.io/gh/baianat/vee-validate/commit/fedea704e01aaa64749c04c891b74c1be6d70c12?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/baianat/vee-validate/pull/2057/graphs/tree.svg?width=650&token=XFzlvHKFP5&height=150&src=pr)](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master    #2057   +/-   ##
=======================================
  Coverage   95.97%   95.97%           
=======================================
  Files          62       62           
  Lines        2358     2358           
  Branches      738      738           
=======================================
  Hits         2263     2263           
  Misses         86       86           
  Partials        9        9
```


| [Impacted Files](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/rules/decimal.js](https://codecov.io/gh/baianat/vee-validate/pull/2057/diff?src=pr&el=tree#diff-c3JjL3J1bGVzL2RlY2ltYWwuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=footer). Last update [fedea70...b0c41eb](https://codecov.io/gh/baianat/vee-validate/pull/2057?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/rules/decimal.js b/src/rules/decimal.js
index 7613aa02..c86c54ae 100644
--- a/src/rules/decimal.js
+++ b/src/rules/decimal.js
@@ -15,7 +15,7 @@ const validate = (value, { decimals = '*', separator = '.' } = {}) => {
   }
 
   const regexPart = decimals === '*' ? '+' : `{1,${decimals}}`;
-  const regex = new RegExp(`^[-+]?\\d*(\\${separator}\\d${regexPart})?$`);
+  const regex = new RegExp(`^[-+]?\\d*(\\${separator}\\d${regexPart})?([eE]{1}[-]?\\d+)?$`);
 
   if (! regex.test(value)) {
     return false;
diff --git a/types/vee-validate.d.ts b/types/vee-validate.d.ts
index a2e8fc1e..a48ad478 100644
--- a/types/vee-validate.d.ts
+++ b/types/vee-validate.d.ts
@@ -68,8 +68,13 @@ export interface IDictionary {
     setDateFormat(locale: string, format: string): void;
 }
 
+export type ResultObject = {
+    valid: boolean;
+    data?: object;
+}
+
 export interface RuleValidate {
-    (value: any, args: object | any[], data?: any): boolean | Promise<boolean>
+    (value: any, args: object | any[], data?: any): boolean | ResultObject | Promise<boolean | ResultObject>
 }
 
 export interface Rule {


Test patch:
diff --git a/tests/unit/rules/decimal.js b/tests/unit/rules/decimal.js
index 99177dc4..72e62d39 100644
--- a/tests/unit/rules/decimal.js
+++ b/tests/unit/rules/decimal.js
@@ -16,6 +16,10 @@ test('validates numerics with decmial numbers', () => {
   expect(validate('1', { decimails: 0 })).toBe(true);
   expect(validate('+1')).toBe(true);
   expect(validate('+1.2')).toBe(true);
+  expect(validate('1.2E-12')).toBe(true);
+  expect(validate('1.2e16')).toBe(true);
+  expect(validate('1.2E16')).toBe(true);
+  expect(validate('1.2e-12')).toBe(true);
 
   expect(validate('')).toBe(false);
   expect(validate(null)).toBe(false);