Skip to content

Commit 8f0cc15

Browse files
author
Beatriz Rizental
authored
Bug 1710827 - Fail validation of counter and quantity if value is not int (#319)
1 parent a11f407 commit 8f0cc15

File tree

6 files changed

+47
-12
lines changed

6 files changed

+47
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[Full changelog](https://github.com/mozilla/glean.js/compare/v0.12.0...main)
44

55
* [#313](https://github.com/mozilla/glean.js/pull/313): Implement error recording mechanism and error checking testing API.
6+
* [#319](https://github.com/mozilla/glean.js/pull/319): BUGFIX: Do not allow recording floats with the quantity and counter metric types.
67

78
# v0.12.0 (2021-05-11)
89

glean/src/core/error/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import type { MetricType } from "../metrics";
66
import type { ErrorType } from "./error_type.js";
7-
import CounterMetricType from "../metrics/types/counter";
8-
import { combineIdentifierAndLabel, stripLabel } from "../metrics/types/labeled";
7+
import CounterMetricType from "../metrics/types/counter.js";
8+
import { combineIdentifierAndLabel, stripLabel } from "../metrics/types/labeled.js";
99

1010
/**
1111
* For a given metric, get the metric in which to record errors.

glean/src/core/metrics/types/counter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import type { CommonMetricData } from "../index.js";
66
import type { JSONValue } from "../../utils.js";
77
import { MetricType } from "../index.js";
8-
import { isUndefined, isNumber } from "../../utils.js";
8+
import { isUndefined, isInteger } from "../../utils.js";
99
import { Context } from "../../context.js";
1010
import { Metric } from "../metric.js";
1111
import { ErrorType } from "../../error/error_type.js";
@@ -16,7 +16,7 @@ export class CounterMetric extends Metric<number, number> {
1616
}
1717

1818
validate(v: unknown): v is number {
19-
if (!isNumber(v)) {
19+
if (!isInteger(v)) {
2020
return false;
2121
}
2222

glean/src/core/metrics/types/quantity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import type { CommonMetricData } from "../index.js";
66
import { MetricType } from "../index.js";
7-
import { isNumber } from "../../utils.js";
7+
import { isInteger } from "../../utils.js";
88
import { Context } from "../../context.js";
99
import { Metric } from "../metric.js";
1010
import { ErrorType } from "../../error/error_type.js";
@@ -15,7 +15,7 @@ export class QuantityMetric extends Metric<number, number> {
1515
}
1616

1717
validate(v: unknown): v is number {
18-
if (!isNumber(v)) {
18+
if (!isInteger(v)) {
1919
return false;
2020
}
2121

glean/src/core/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function isUndefined(v: unknown): v is undefined {
7575
* stating whether `v` is a string.
7676
*/
7777
export function isString(v: unknown): v is string {
78-
return (typeof v === "string" || (typeof v === "object" && v !== null && v.constructor === String));
78+
return typeof v === "string";
7979
}
8080

8181
/**
@@ -87,7 +87,7 @@ export function isString(v: unknown): v is string {
8787
* stating whether `v` is a boolean.
8888
*/
8989
export function isBoolean(v: unknown): v is boolean {
90-
return (typeof v === "boolean" || (typeof v === "object" && v !== null && v.constructor === Boolean));
90+
return typeof v === "boolean";
9191
}
9292

9393
/**
@@ -99,7 +99,19 @@ export function isBoolean(v: unknown): v is boolean {
9999
* stating whether `v` is a number.
100100
*/
101101
export function isNumber(v: unknown): v is number {
102-
return ((typeof v === "number" || (typeof v === "object" && v !== null && v.constructor === Number)) && !isNaN(v));
102+
return typeof v === "number" && !isNaN(v);
103+
}
104+
105+
/**
106+
* Checks whether or not `v` is an integer.
107+
*
108+
* @param v The value to verify.
109+
*
110+
* @returns A special Typescript value (which compiles down to a boolean)
111+
* stating whether `v` is a number.
112+
*/
113+
export function isInteger(v: unknown): v is number {
114+
return isNumber(v) && Number.isInteger(v);
103115
}
104116

105117
/**

glean/tests/core/utils.spec.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ describe("utils", function() {
5050
assert.strictEqual(utils.isString(""), true);
5151
assert.strictEqual(utils.isString("something else"), true);
5252
assert.strictEqual(utils.isString(obj["test"]), true);
53-
assert.strictEqual(utils.isString(new String("check")), true);
5453
});
5554

5655
it("isBoolean validates correctly", function() {
@@ -62,7 +61,6 @@ describe("utils", function() {
6261
// Valid values
6362
assert.strictEqual(utils.isBoolean(true), true);
6463
assert.strictEqual(utils.isBoolean(false), true);
65-
assert.strictEqual(utils.isBoolean(new Boolean(true)), true);
6664
assert.strictEqual(utils.isBoolean(!!"something else"), true);
6765
});
6866

@@ -76,7 +74,31 @@ describe("utils", function() {
7674
// Valid values
7775
assert.strictEqual(utils.isNumber(10), true);
7876
assert.strictEqual(utils.isNumber(-10), true);
79-
assert.strictEqual(utils.isNumber(new Number(10)), true);
77+
});
78+
79+
it("isInteger validates correctly", function() {
80+
// Invalid values
81+
assert.strictEqual(utils.isInteger(undefined), false);
82+
assert.strictEqual(utils.isInteger("10"), false);
83+
assert.strictEqual(utils.isInteger({}), false);
84+
assert.strictEqual(utils.isInteger(NaN), false);
85+
assert.strictEqual(utils.isInteger(0.1), false);
86+
assert.strictEqual(utils.isInteger(Math.PI), false);
87+
assert.strictEqual(utils.isInteger(Infinity), false);
88+
assert.strictEqual(utils.isInteger(-Infinity), false);
89+
assert.strictEqual(utils.isInteger(true), false);
90+
assert.strictEqual(utils.isInteger(false), false);
91+
assert.strictEqual(utils.isInteger([1]), false);
92+
assert.strictEqual(utils.isInteger(5.000000000000001), false);
93+
94+
// Valid values
95+
assert.strictEqual(utils.isInteger(10), true);
96+
assert.strictEqual(utils.isInteger(-10), true);
97+
assert.strictEqual(utils.isInteger(0), true);
98+
assert.strictEqual(utils.isInteger(-100000), true);
99+
assert.strictEqual(utils.isInteger(99999999999999999999999), true);
100+
assert.strictEqual(utils.isInteger(5.0), true);
101+
assert.strictEqual(utils.isInteger(5.0000000000000001), true);
80102
});
81103

82104
it("sanitizeApplicationId works correctly", function() {

0 commit comments

Comments
 (0)