hapijs / hapijs/joi

Parsing number incorrectly flagged as unsafe

Open
#2,693 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Support plan

* *is this issue currently blocking your project?* (no):
* *is this issue affecting a production system?* (yes):

#### Context

* *node version*: 16.6.1
* *module version with issue*: 17.4.2
* *last module version without issue*: unknown
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): standalone
* *any other relevant information*:

#### What are you trying to achieve or the steps to reproduce?

Validating and parsing a value from a string to a number. Specifically, the value "873.19999999999993".
The result of the validator is that the field value is "unsafe". We have been able to work around this issue by using the unsafe() flag, however the documentation for the unsafe() flag notes that it is to be used to work around numbers that are outside of the Min/Max JavaScript Number range (which this number is not).

```js
const schema = Joi.object({
value: Joi.number().positive().optional(),
});

const submission = {
value: '873.19999999999993',
};

const result = schema.validate(submission);
expect(result.error).toBeUndefined();
```

#### What was the result you got?

```js
Received: [ValidationError: "value" must be a safe number]
```

#### What result did you expect?

The test to pass and parse the number as a valid/safe number

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.