Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #170 from peterjanes/master
Browse files Browse the repository at this point in the history
Don't allow non-numeric characters when parsing the `number` type.
  • Loading branch information
JamesMessinger authored Aug 1, 2020
2 parents e9327e6 + 2a8c1b7 commit cbac12d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function parseNumber (schema, value, propPath) {
value = getValueToValidate(schema, value);

// Make sure it's a properly-formatted number
let parsedValue = parseFloat(value);
let parsedValue = value === "" ? NaN : Number(value);
if (_.isNaN(parsedValue) || !_.isFinite(parsedValue)) {
throw ono({ status: 400 }, '"%s" is not a valid numeric value', propPath || value);
}
Expand Down
15 changes: 15 additions & 0 deletions test/specs/json-schema/parse/parse-number.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,19 @@ describe("JSON Schema - parse number params", () => {
expect(err.message).to.contain('Missing required header parameter "Test"');
}));
});

it("should be more strict than parseFloat()", (done) => {
let schema = {
type: "number",
required: true
};

let express = helper.parse(schema, "1z", done);

express.use("/api/test", helper.spy((err, req, res, next) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.status).to.equal(400);
expect(err.message).to.contain('"1z" is not a valid numeric value');
}));
});
});

0 comments on commit cbac12d

Please sign in to comment.