How to use assert-plus

Comprehensive assert-plus code examples:

How to use assert-plus.buffer:

486
487
488
489
490
491
492
493
494
495
496
 *
 */
/* END JSSTYLED */
function _jwsFromManifest(manifest, body) {
    assert.object(manifest, 'manifest');
    assert.buffer(body, 'body');


    var formatLength;
    var formatTail;
    var jws = {

How to use assert-plus.date:

273
274
275
276
277
278
279
280
281
282
283
 * @param {Date} now (optional) - a date object to compare to
 * @return {String} - printable string
 */
function longAgo(when, now) {
    now = now || new Date();
    assert.date(now, 'now');


    var seconds = Math.round((now - when) / 1000);
    var times = [
        seconds / 60 / 60 / 24 / 365, // years

How to use assert-plus.deepEqual:

104
105
106
107
108
109
110
111
112
113

var idField = opts.idField;

var differ = function (a, b) {
    try {
        assert.deepEqual(a, b);
    } catch (err) {
        return true;
    }
    return false;

How to use assert-plus.stream:

1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
 *  - opts: see above
 *  - cb: callback of the form f(err)
 */
MantaClient.prototype.put = function put(p, input, opts, cb) {
    assert.string(p, 'path');
    assert.stream(input, 'input');
    if (typeof (opts) === 'function') {
        cb = opts;
        opts = {};
    }

How to use assert-plus.arrayOfNumber:

1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
 * @param {arrayOfNumber} relativeTo: an array representing the starting time as
 *        returned by `process.hrtime()` from which to compute the
 *        time difference.
 */
function monotonicTimeDiffMs(relativeTo) {
    assert.arrayOfNumber(relativeTo, 'relativeTo');


    var diff = process.hrtime(relativeTo);
    var ms = (diff[0] * 1e3) + (diff[1] / 1e6); // in milliseconds
    return ms;

How to use assert-plus.array:

75
76
77
78
79
80
81
82
83
84
/**
 * Validates that a value is one of the values present in an array that
 * enumerates all allowed values.
 */
function validateEnum(values) {
    assert.array(values, 'values');
    return function _validateEnum(_, name, value, callback) {
        if (values.indexOf(value) === -1) {
            callback(errors.invalidParam(name,
                'must be one of: ' + values.map(JSON.stringify).join(', ')));

How to use assert-plus.AssertionError:

758
759
760
761
762
763
764
765
766
767
 * already been done) or opts.insecure (this module's chosen name
 * for this thing).
 */
var rejectUnauthorized;
if (opts.insecure !== undefined && opts.rejectUnauthorized !== undefined) {
    throw new assert.AssertionError(
        'cannot set both opts.insecure and opts.rejectUnauthorized');
} else if (opts.insecure !== undefined) {
    rejectUnauthorized = !opts.insecure;
} else if (opts.rejectUnauthorized !== undefined) {

How to use assert-plus.strictEqual:

1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
    return;
}

assert.ok(cls.cls_track.countTotal() <= limitObj.limit);
if (cls.cls_track.countSkipped() === limitObj.limit) {
    assert.strictEqual(cls.cls_track.countIncluded(), 0);
    /*
     * If we see a full page of objects with the same mtime, we
     * cannot tell if there are any more objects with the same
     * mtime or not.  All subsequent requests with the same

How to use assert-plus.arrayOfUuid:

58
59
60
61
62
63
64
65
66
67
68
69
 */
function filterListNetworks(api, cfg) {
    assert.object(api, 'api');
    assert.object(api.log, 'api.log');
    assert.object(cfg, 'cfg');
    assert.arrayOfUuid(cfg.accounts, 'cfg.accounts');


    var log = api.log;


    return function filterOwnerListNetworks(opts, networks) {

How to use assert-plus.finite:

534
535
536
537
538
539
540
541
542
543
 *
 * Throws TypeError's if you pass bad arguments.
 */
function MantaClient(options) {
    assert.object(options, 'options');
    assert.finite(options.connectTimeout, 'options.connectTimeout');
    assert.optionalObject(options.headers, 'options.headers');
    assert.object(options.log, 'options.log');
    assert.optionalString(options.url, 'options.url');
    assert.optionalString(options.socketPath, 'options.socketPath');

How to use assert-plus.notEqual:

33
34
35
36
37
38
39
40
41
42
  // Store library entry info in lib map.
  key = e.libName;
  assert.equal(key, e.enclosedByQualifiedName, e);
  _set(libToEntryMap, key, e);
} else if (e.enclosedBy) {
  assert.notEqual(e.type, 'library');
  key = e.enclosedByQualifiedName;
} else {
  assert.equal(e.type, 'library');
  // Add library "index" page to the library's entries in the general container map,

How to use assert-plus.optionalArrayOfObject:

514
515
516
517
518
519
520
521
522
523
function createRoles(ctx, next) {
    vasync.forEachPipeline({
        inputs: ctx.rolesToCreate,
        func: function createOne(role, nextRole) {
            assert.string(role.name, 'role.name');
            assert.optionalArrayOfObject(role.members, 'role.members');
            assert.optionalArrayOfString(role.default_members,
                'role.default_members');
            assert.optionalArray(role.policies,
                'role.policies');

How to use assert-plus.optionalArray:

153
154
155
156
157
158
159
160
161
162
 *      - @param failOnEmptyValue {Boolean} Optional - If true, throws an error
 *        if a given key's value is the empty string. Default is false.
 */
function _parseKeyValue(kv, validKeys, options) {
    assert.string(kv, 'kv');
    assert.optionalArray(validKeys, 'validKeys');
    assert.optionalObject(options, 'options');
    options = options || {};
    assert.optionalBool(options.disableTypeConversions,
        'options.disableTypeConversions');

How to use assert-plus.notStrictEqual:

1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
        stepcb();
    });
});

funcs.push(function fetchProbes(_, stepcb) {
    assertplus.notStrictEqual(self.ma_amon_deployed, null);

    /*
     * This function inserts the probe information into
     * self.ma_amon_deployed, so we don't need to do

How to use assert-plus.optionalUuid:

466
467
468
469
470
471
472
473
474
475
476
 *          provisioned on. Optional.
 * @param {Function} cb of the form `function (err, config, res)`
 */
CloudApi.prototype.updateConfig = function updateConfig(opts, cb) {
    assert.object(opts, 'opts');
    assert.optionalUuid(opts.default_network, 'opts.default_network');
    assert.func(cb, 'cb');


    this._request({
        method: 'PUT',

How to use assert-plus.fail:

588
589
590
591
592
593
594
595
596

        cb(new restify.errors.PreconditionFailedError(
            'Cannot sync when in migration phase: ' + phase));
        return;
    default:
        assert.fail(format('Unvalidated vm migration action: %s',
            action));
        break;
}

How to use assert-plus.ifError:

973
974
975
976
977
978
979
980
981
982
983


/**
 * Like ftw(3) - performs a `find` operation.
 *
 * client.ftw('/', function (err, res) {
 *     assert.ifError(err);
 *
 *     res.on('entry', function (obj) {
 *         console.log(obj);
 *     });

How to use assert-plus.optionalArrayOfUuid:

1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
assert.string(opts.name, 'opts.name');
assert.string(opts.version, 'opts.version');
assert.optionalString(opts.description, 'opts.description');
assert.optionalString(opts.homepage, 'opts.homepage');
assert.optionalString(opts.eula, 'opts.eula');
assert.optionalArrayOfUuid(opts.acl, 'opts.acl');
assert.optionalObject(opts.tags, 'opts.tags');
assert.func(cb, 'cb');

this._request({

How to use assert-plus.equal:

36
37
38
39
40
41
42
43
44
45
  _set(libToEntryMap, key, e);
} else if (e.enclosedBy) {
  assert.notEqual(e.type, 'library');
  key = e.enclosedByQualifiedName;
} else {
  assert.equal(e.type, 'library');
  // Add library "index" page to the library's entries in the general container map,
  // but not the lib map which is used to create the main API page index.
  key = e.libName;
  _set(containerToEntryMap, key, e);

How to use assert-plus.optionalFunc:

172
173
174
175
176
177
178
179
180
181
assert.string(opts.func, 'options.func');
assert.finite(opts.parallel, 'options.parallel');
assert.string(opts.type, 'options.type');
assert.string(opts.user, 'options.user');
assert.string(opts.watch, 'options.watch');
assert.optionalFunc(callback, 'callback');

var queue = new manta.Queue({
    limit: opts.parallel,
    worker: function getJobData(id, cb) {

How to use assert-plus.optionalFinite:

1851
1852
1853
1854
1855
1856
1857
1858
1859
if (typeof (opts) === 'function') {
    cb = opts;
    opts = {};
}
assert.object(opts, 'options');
assert.optionalFinite(opts.size, 'options.size');
assert.func(cb, 'callback');

input.pause();

How to use assert-plus.arrayOfObject:

65
66
67
68
69
70
71
72
73
74
var log = api.log;

return function filterOwnerListNetworks(opts, networks) {
    assert.object(opts, 'opts');
    assert.object(opts.account, 'opts.account');
    assert.arrayOfObject(networks, 'networks');

    log.debug('Running ' + filterOwnerListNetworks.name);

    var accountUuid = opts.account.uuid;

How to use assert-plus.bool:

557
558
559
560
561
562
563
564
565
566
567
 */
CloudApi.prototype.updateNetworkIp = function updateNetworkIp(opts, cb) {
    assert.object(opts, 'opts');
    assert.uuid(opts.id, 'opts.id');
    assert.string(opts.ip, 'opts.ip');
    assert.bool(opts.reserved, 'opts.reserved');
    assert.func(cb, 'cb');


    var endpoint = this._path(format('/%s/networks/%s/ips/%s',
        this.account, opts.id, opts.ip));

How to use assert-plus.arrayOfString:

165
166
167
168
169
170
171
172
173
174
175


// GetJob(Errors|Failures|Inputs|Outputs) all have the same logic
// So this is just a generic handler for those
function jobDataCommon(opts, callback) {
    assert.object(opts, 'options');
    assert.arrayOfString(opts.args, 'options.args');
    assert.object(opts.client, 'options.client');
    assert.string(opts.func, 'options.func');
    assert.finite(opts.parallel, 'options.parallel');
    assert.string(opts.type, 'options.type');

How to use assert-plus.number:

3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
    break;
case UPLOAD_OPS.GET:
    finalURL = location + '/state';
    break;
case UPLOAD_OPS.UPLOAD:
    assert.number(uu.uu_partNum, 'partNum');
    finalURL = location + '/' + uu.uu_partNum;
    break;
default:
    throw (new Verror('Invalid operation: %s', uu.uu_op));

How to use assert-plus.optionalArrayOfString:

541
542
543
544
545
546
547
548
549
550
assert.object(options.log, 'options.log');
assert.optionalString(options.url, 'options.url');
assert.optionalString(options.socketPath, 'options.socketPath');
assert.optionalString(options.user, 'options.user');
assert.optionalString(options.subuser, 'options.subuser');
assert.optionalArrayOfString(options.role, 'options.role');
if (options.sign === null)
    delete (options.sign);
assert.optionalFunc(options.sign, 'options.sign');
assert.ok(options.url || options.socketPath,

How to use assert-plus.optionalNumber:

3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
MantaClient.prototype.createUpload = function createUpload(p, opts, cb) {
    assert.string(p, 'path');
    assert.object(opts, 'options');
    assert.string(opts.account, 'opts.account');
    assert.optionalObject(opts.headers, 'opts.headers');
    assert.optionalNumber(opts.copies, 'opts.copies');
    assert.optionalNumber(opts.size, 'opts.size');
    assert.optionalString(opts.md5, 'opts.md5');
    assert.func(cb, 'callback');

How to use assert-plus.optionalObject:

108
109
110
111
112
113
114
115
116
117
118
119
120


function InvalidDirectoryError(dir, ent) {
    Error.call(this);


    assert.string(dir, 'dir');
    assert.optionalObject(ent, 'ent');


    this.name = 'InvalidDirectoryError';
    this.message = dir + ' is an invalid manta directory';
    this.info = ent;

How to use assert-plus.ok:

84
85
86
87
88
89
90
91
92
93
});

suite.test('cleanup: rm test directory ' + TESTDIR, function (t) {
    // Sanity checks that we don't `mrm -r` a non-test dir.
    assert.ok(TESTDIR);
    assert.ok(TESTDIR.indexOf('node-manta-test') !== -1);

    forkExecWait({ argv: [ MRM, '-r', TESTDIR ]}, function (err) {
        t.ifError(err, err);
        t.end();

How to use assert-plus.optionalBool:

65
66
67
68
69
70
71
72
73
74
assert.object(client, 'client');
assert.object(opts, 'options');
assert.finite(opts.batch, 'options.batch');
assert.arrayOfString(opts.jobIds, 'options.jobIds');
assert.finite(opts.parallel, 'options.parallel');
assert.optionalBool(opts.open, 'options.open');
assert.func(cb, 'callback');

var lstream = new LineStream({
    encoding: 'utf8'