Skip to content

Commit

Permalink
Formula cast to number. Closes #1917
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jun 25, 2019
1 parent 5d8ea13 commit c8b444d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ internals.constants = {

true: true,
false: false,
null: null
null: null,

second: 1000,
minute: 60 * 1000,
hour: 60 * 60 * 1000,
day: 24 * 60 * 60 * 1000
};


Expand All @@ -320,5 +325,26 @@ internals.functions = {
if: function (condition, then, otherwise) {

return condition ? then : otherwise;
},

number: function (value) {

if (typeof value === 'number') {
return value;
}

if (typeof value === 'string') {
return parseFloat(value);
}

if (typeof value === 'boolean') {
return value ? 1 : 0;
}

if (value instanceof Date) {
return value.getTime();
}

return null;
}
};
13 changes: 13 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,17 @@ describe('Template', () => {
expect(() => Joi.var(source)).to.throw('Invalid template variable "x +" fails due to: Formula contains invalid trailing operator');
});
});

describe('functions', () => {

describe('number()', () => {

it('casts values to numbers', () => {

const schema = Joi.valid(Joi.var('{number(1) + number(true) + number(false) + number("1") + number($x)}'));
expect(schema.validate(3, { context: { x: {} } }).error).to.not.exist();
expect(schema.validate(4, { context: { x: {} } }).error).to.be.an.error('"value" must be one of [{number(1) + number(true) + number(false) + number("1") + number($x)}]');
});
});
});
});
27 changes: 26 additions & 1 deletion test/types/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ describe('date', () => {
]);
});

it('accepts references as max date', () => {
it('accepts context references as max date', () => {

const ref = Joi.ref('$a');
const schema = Joi.object({ b: Joi.date().max(ref) });
Expand All @@ -886,6 +886,31 @@ describe('date', () => {
]);
});

it('supports template operations', () => {

const ref = Joi.var('{number(from) + 364 * day}');
const schema = Joi.object({
annual: Joi.boolean().required(),
from: Joi.date().required(),
to: Joi.date().required()
.when('annual', { is: true, then: Joi.date().max(ref) })
});

Helper.validate(schema, [
[{ annual: false, from: '2000-01-01', to: '2010-01-01' }, true],
[{ annual: true, from: '2000-01-01', to: '2000-12-30' }, true],
[{ annual: true, from: '2000-01-01', to: '2010-01-01' }, false, null, {
message: '"to" must be less than or equal to "{number(from) + 364 * day}"',
details: [{
message: '"to" must be less than or equal to "{number(from) + 364 * day}"',
path: ['to'],
type: 'date.max',
context: { limit: ref, label: 'to', key: 'to', value: new Date('2010-01-01') }
}]
}]
]);
});

it('errors if reference is not a date', () => {

const ref = Joi.ref('a');
Expand Down

0 comments on commit c8b444d

Please sign in to comment.