Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"index.test.js",
"error.test.js",
"override.test.js",
"unit.test.js"
"unit.test.js",
"joi.test.js"
]
}]
}
2 changes: 1 addition & 1 deletion create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const merge = require('ut-function.merge');
const path = require('path');
const {Broker, ServiceBus} = require('ut-bus');
const through2 = require('through2');
const joi = require('joi');
const joi = require('./joi');

function getDataDirectory() {
if (process.browser) return '/';
Expand Down
11 changes: 11 additions & 0 deletions joi/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const bigintRegex = /^[1-9]{1}[0-9]{0,18}$/;
module.exports = joi => ({
type: 'bigint',
base: joi.any(),
messages: {
'bigint.base': '{{#label}} must be a bigint'
},
validate(value, helpers) {
if (!bigintRegex.test(String(value))) return { value, errors: helpers.error('bigint.base') };
}
});
3 changes: 3 additions & 0 deletions joi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = require('joi').extend(
require('./bigint')
);
26 changes: 26 additions & 0 deletions test/joi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const tap = require('tap');
const joi = require('../joi');

tap.test('bigint', t => {
const test = (name, schema, input, ok = true) => t.test(name, assert => {
assert.comment(`schema: ${JSON.stringify(schema.describe())}`);
assert.comment(`input: ${JSON.stringify(input)}`);
const { error = '' } = schema.validate(input);
if (ok) assert.notOk(error, 'pass');
else assert.ok(error, error.message || 'error expected');
assert.end();
});
test('number', joi.bigint(), 1);
test('random string', joi.bigint(), 'asd', false);
test('stringified number', joi.bigint(), '11');
test('stringified number overflow', joi.bigint(), '1'.repeat(20), false);
test('zero', joi.bigint(), 0, false);
test('negative number', joi.bigint(), -1, false);
test('boolean', joi.bigint(), false, false);
test('null', joi.bigint(), null, false);
test('null explicitly allowed', joi.bigint().allow(null), null);
test('object int value', joi.object({test: joi.bigint()}), {test: 1});
test('object random string value', joi.object({test: joi.bigint()}), {test: 'asd'}, false);
test('array int item', joi.array().items(joi.bigint()), [123]);
t.end();
});