Skip to content

Commit

Permalink
Refactor infinity. Closes #2007
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 1, 2019
1 parent 5c1840a commit 1396405
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 30 deletions.
10 changes: 7 additions & 3 deletions API.md
Expand Up @@ -8,7 +8,6 @@
- [`assert(value, schema, [message], [options])` - aliases: `attempt`](#assertvalue-schema-message-options---aliases-attempt)
- [`cache.provision([options])`](#cacheprovisionoptions)
- [`compile(schema, [options])`](#compileschema-options)
- [`defaults(fn)`](#defaultsfn)
- [`expression(template, [options])` - aliases: `x`](#expressiontemplate-options---aliases-x)
- [Template syntax](#template-syntax)
- [`extend(extension)`](#extendextension)
Expand Down Expand Up @@ -176,7 +175,6 @@
- [`any.only`](#anyonly-1)
- [`any.default`](#anydefault)
- [`any.failover`](#anyfailover)
- [`any.empty`](#anyempty)
- [`any.invalid`](#anyinvalid)
- [`any.required`](#anyrequired)
- [`any.unknown`](#anyunknown)
Expand Down Expand Up @@ -225,6 +223,7 @@
- [`link.uninitialized`](#linkuninitialized)
- [`number.base`](#numberbase)
- [`number.greater`](#numbergreater)
- [`number.infinity`](#numberinfinity)
- [`number.integer`](#numberinteger-1)
- [`number.less`](#numberless)
- [`number.max`](#numbermax)
Expand Down Expand Up @@ -262,6 +261,7 @@
- [`string.dataUri`](#stringdatauri)
- [`string.domain`](#stringdomain)
- [`string.email`](#stringemail)
- [`string.empty`](#stringempty)
- [`string.guid`](#stringguid)
- [`string.hexAlign`](#stringhexalign)
- [`string.hex`](#stringhex)
Expand Down Expand Up @@ -2049,7 +2049,7 @@ const number = Joi.number();
await number.validate(5);
```

Possible validation errors: [`number.base`](#numberbase)
Possible validation errors: [`number.base`](#numberbase), [`number.infinity`](#numberinfinity)

#### `number.greater(limit)`

Expand Down Expand Up @@ -3688,6 +3688,10 @@ Additional local context properties:
}
```

#### `number.infinity`

The number is `Infinity` or `-Infinity`.

#### `number.integer`

The number is not a valid integer.
Expand Down
5 changes: 1 addition & 4 deletions lib/manifest.js
Expand Up @@ -53,10 +53,7 @@ exports.describe = function (schema) {
}

if (schema._invalids) {
const clean = schema._root[schema._type]();
if (!Hoek.deepEqual(schema._invalids, clean._invalids)) {
desc.invalid = schema._invalids.describe();
}
desc.invalid = schema._invalids.describe();
}

// Rules
Expand Down
1 change: 1 addition & 0 deletions lib/messages.js
Expand Up @@ -175,6 +175,7 @@ exports.errors = {

'number.base': '"{{#label}}" must be a number',
'number.greater': '"{{#label}}" must be greater than {{#limit}}',
'number.infinity': '"{{#label}}" cannot be infinity',
'number.integer': '"{{#label}}" must be an integer',
'number.less': '"{{#label}}" must be less than {{#limit}}',
'number.max': '"{{#label}}" must be less than or equal to {{#limit}}',
Expand Down
8 changes: 6 additions & 2 deletions lib/types/number.js
Expand Up @@ -4,7 +4,6 @@ const Hoek = require('@hapi/hoek');

const Any = require('./any');
const Common = require('../common');
const Values = require('../values');


const internals = {
Expand All @@ -22,7 +21,6 @@ internals.Number = Any.extend({
initialize: function () {

this._flags.unsafe = false;
this._invalids = new Values([Infinity, -Infinity]);
},

// Coerce
Expand Down Expand Up @@ -68,6 +66,12 @@ internals.Number = Any.extend({

validate: function (value, state, prefs) {

if (value === Infinity ||
value === -Infinity) {

return { value, errors: this.createError('number.infinity', value, null, state, prefs) };
}

if (!Common.isNumber(value)) {
return { value, errors: this.createError('number.base', value, null, state, prefs) };
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Expand Up @@ -1718,7 +1718,7 @@ describe('Joi', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 5, invalids: [Infinity, -Infinity, 5], label: 'value' }
context: { value: 5, invalids: [5], label: 'value' }
}]
}],
['5', false, null, {
Expand All @@ -1727,7 +1727,7 @@ describe('Joi', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 5, invalids: [Infinity, -Infinity, 5], label: 'value' }
context: { value: 5, invalids: [5], label: 'value' }
}]
}]
]);
Expand Down
2 changes: 1 addition & 1 deletion test/ref.js
Expand Up @@ -1326,7 +1326,7 @@ describe('ref', () => {
type: 'number',
flags: { only: true, default: { ref: { path: ['a', 'b'] } } },
allow: [{ ref: { path: ['a', 'b'] } }],
invalid: [{ ref: { type: 'global', path: ['b', 'c'] } }, Infinity, -Infinity],
invalid: [{ ref: { type: 'global', path: ['b', 'c'] } }],
rules: [
{ name: 'min', args: { limit: { ref: { path: ['a', 'b'] } } } },
{ name: 'max', args: { limit: { ref: { path: ['a', 'b'] } } } },
Expand Down
8 changes: 7 additions & 1 deletion test/types/any.js
Expand Up @@ -1741,6 +1741,12 @@ describe('any', () => {
expect(() => Joi.any().valid(1).invalid(1)).to.throw('Setting invalid value 1 leaves schema rejecting all values due to previous valid rule');
expect(() => Joi.any().allow(1).invalid(1)).to.not.throw();
});

it('appends invalid values', () => {

const schema = Joi.any().invalid(1).invalid(2);
expect(schema.describe()).to.equal({ type: 'any', invalid: [1, 2] });
});
});

describe('keep()', () => {
Expand Down Expand Up @@ -2728,7 +2734,7 @@ describe('any', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 2, invalids: [Infinity, -Infinity, 2], label: 'value' }
context: { value: 2, invalids: [2], label: 'value' }
}]);
});
});
Expand Down
34 changes: 17 additions & 17 deletions test/types/number.js
Expand Up @@ -1119,7 +1119,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 50, invalids: [Infinity, -Infinity, 50], label: 'value' }
context: { value: 50, invalids: [50], label: 'value' }
}]);
});

Expand Down Expand Up @@ -1165,21 +1165,21 @@ describe('number', () => {
const t = Joi.number();
Helper.validate(t, [
[Infinity, false, null, {
message: '"value" contains an invalid value',
message: '"value" cannot be infinity',
details: [{
message: '"value" contains an invalid value',
message: '"value" cannot be infinity',
path: [],
type: 'any.invalid',
context: { value: Infinity, invalids: [Infinity, -Infinity], label: 'value' }
type: 'number.infinity',
context: { value: Infinity, label: 'value' }
}]
}],
[-Infinity, false, null, {
message: '"value" contains an invalid value',
message: '"value" cannot be infinity',
details: [{
message: '"value" contains an invalid value',
message: '"value" cannot be infinity',
path: [],
type: 'any.invalid',
context: { value: -Infinity, invalids: [Infinity, -Infinity], label: 'value' }
type: 'number.infinity',
context: { value: -Infinity, label: 'value' }
}]
}]
]);
Expand Down Expand Up @@ -1594,7 +1594,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 1, invalids: [Infinity, -Infinity, 1], label: 'value' }
context: { value: 1, invalids: [1], label: 'value' }
}]
}],
[-1, true],
Expand Down Expand Up @@ -1624,7 +1624,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: -5, invalids: [Infinity, -Infinity, -5], label: 'value' }
context: { value: -5, invalids: [-5], label: 'value' }
}]
}],
[8, false, null, {
Expand Down Expand Up @@ -1718,7 +1718,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 9, invalids: [Infinity, -Infinity, 9], label: 'value' }
context: { value: 9, invalids: [9], label: 'value' }
}]
}],
[null, false, null, {
Expand Down Expand Up @@ -1754,7 +1754,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 9, invalids: [Infinity, -Infinity, 9], label: 'value' }
context: { value: 9, invalids: [9], label: 'value' }
}]
}],
[null, true]
Expand Down Expand Up @@ -1880,7 +1880,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 8, invalids: [Infinity, -Infinity, 8], label: 'value' }
context: { value: 8, invalids: [8], label: 'value' }
}]
}],
[9, true],
Expand Down Expand Up @@ -1934,7 +1934,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 8, invalids: [Infinity, -Infinity, 8], label: 'value' }
context: { value: 8, invalids: [8], label: 'value' }
}]
}],
[9, true],
Expand Down Expand Up @@ -2026,7 +2026,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 8, invalids: [Infinity, -Infinity, 8], label: 'value' }
context: { value: 8, invalids: [8], label: 'value' }
}]
}],
[9, true],
Expand Down Expand Up @@ -2273,7 +2273,7 @@ describe('number', () => {
message: '"value" contains an invalid value',
path: [],
type: 'any.invalid',
context: { value: 6, invalids: [Infinity, -Infinity, 6], label: 'value' }
context: { value: 6, invalids: [6], label: 'value' }
}]
}],
[8, true],
Expand Down

0 comments on commit 1396405

Please sign in to comment.