How to use the optionalBool function from assert-plus

Find comprehensive JavaScript assert-plus.optionalBool code examples handpicked from public code repositorys.

assert-plus.optionalBool is a function in the assert-plus library that checks if a value is a boolean or undefined.

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'
fork icon41
star icon75
watch icon57

+ 3 other calls in file

1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
if (typeof (opts) === 'string') {
    opts = {id: opts};
}
assert.object(opts, 'opts');
assert.uuid(opts.id, 'opts.id');
assert.optionalBool(opts.credentials, 'opts.credentials');

var query = {};
if (opts.credentials) {
    query.credentials = 'true';
fork icon38
star icon55
watch icon0

+ 55 other calls in file

How does assert-plus.optionalBool work?

assert-plus.optionalBool is a function that takes a value and an optional argument name, and returns true if the value is either a boolean or undefined, and false otherwise.

27
28
29
30
31
32
33
34
35
36
assert.string(opts.svcName, 'opts.svcName');
assert.arrayOfString(opts.serverNames, 'opts.serverNames');
assert.ok(opts.serverNames.length > 0, 'at least one server name');
assert.optionalUuid(opts.imageUuid, 'opts.imageUuid');
assert.optionalString(opts.imageChannel, 'opts.imageChannel');
assert.optionalBool(opts.skipHACheck, 'opts.skipHACheck');

this.svcName = opts.svcName;
this.serverNames = opts.serverNames;
this.imageUuid = opts.imageUuid;
fork icon19
star icon13
watch icon0

224
225
226
227
228
229
230
231
232
233
 */
function zfsRenameSnapshot(a, b, options, callback) {
    assert.string(a, 'a');
    assert.string(b, 'b');
    assert.object(options, 'options');
    assert.optionalBool(options.recursive, 'options.recursive');
    assert.object(options.log, 'options.log');
    assert.func(callback);
    var optStr = '';
    if (options.recursive) {
fork icon248
star icon0
watch icon0

+ 43 other calls in file

Ai Example

1
2
3
4
5
6
7
const assert = require("assert-plus");

function doSomething(opts) {
  assert.optionalBool(opts.enabled, "opts.enabled");
  const isEnabled = opts.enabled === undefined ? true : opts.enabled;
  // rest of the function logic
}

In the example above, assert.optionalBool(opts.enabled, 'opts.enabled') checks whether opts.enabled is a boolean or undefined. If it's not either of these, it throws an error. The second argument to assert.optionalBool is a string that will be included in the error message to help with debugging. After validating the opts.enabled parameter, the function assigns a default value of true to isEnabled if opts.enabled is undefined. This is a common pattern for handling optional boolean parameters.

573
574
575
576
577
578
579
580
581
582
583
584
585


function executeRbacUpdatePlan(ctx, cb) {
    assert.object(ctx.log, 'ctx.log');
    assert.arrayOfObject(ctx.rbacUpdatePlan, 'ctx.rbacUpdatePlan');
    assert.object(ctx.cloudapi, 'ctx.cloudapi');
    assert.optionalBool(ctx.rbacDryRun, 'ctx.rbacDryRun');


    // TODO: ctx.progress instead of console.log


    vasync.forEachPipeline({
fork icon38
star icon55
watch icon0

156
157
158
159
160
161
162
163
164
165
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');
    assert.optionalObject(options.typeHintFromKey, 'options.typeHintFromKey');
    assert.optionalBool(options.failOnEmptyValue, 'options.failOnEmptyValue');

fork icon38
star icon55
watch icon41

+ 15 other calls in file

647
648
649
650
651
652
653
654
655
656
assert.object(opts, 'options');
assert.object(opts.log, 'options.log');
assert.object(opts.pool, 'options.pool');
assert.optionalString(opts.url, 'options.url');
assert.optionalString(opts.user, 'options.user');
assert.optionalBool(opts.standalone, 'options.standalone');

var log = opts.log;

function _pgCreate(backend) {
fork icon21
star icon60
watch icon0

268
269
270
271
272
273
274
275
276
277
assert.optionalString(opts.password, 'opts.password');
// HTTP client opts:
assert.object(opts.log, 'opts.log');
assert.optionalObject(opts.agent, 'opts.agent');
// assert.optional object or bool(opts.proxy, 'opts.proxy');
assert.optionalBool(opts.insecure, 'opts.insecure');
assert.optionalString(opts.userAgent, 'opts.userAgent');
var log = opts.log;

// - add https:// prefix (or http) if none on 'realm'
fork icon35
star icon59
watch icon0

+ 9 other calls in file

608
609
610
611
612
613
614
615
616
617
618
619


// --- API


function HttpClient(options) {
    assert.object(options, 'options');
    assert.optionalBool(options.appendPath, 'options.appendPath');
    assert.optionalObject(options.headers, 'options.headers');
    assert.object(options.log, 'options.log');
    assert.optionalObject(options.query, 'options.query');
    assert.optionalFunc(options.signRequest, 'options.signRequest');
fork icon34
star icon55
watch icon0

+ 5 other calls in file

958
959
960
961
962
963
964
965
966
967
assertplus.strictEqual(this.ma_alarm_metadata, null);
assertplus.strictEqual(this.ma_maint_windows, null);

assertplus.optionalBool(args.sources.configBasic);
assertplus.optionalBool(args.sources.configFull);
assertplus.optionalBool(args.sources.windows);
assertplus.optionalObject(args.sources.alarms);
if (args.sources.alarms) {
    assertplus.optionalString(args.sources.alarms.state);
    if (typeof args.sources.alarms.state === 'string') {
fork icon13
star icon7
watch icon42

+ 8 other calls in file

285
286
287
288
289
290
291
292
293
294
assert.object(t, 't');
assert.object(opts, 'opts');
assert.object(opts.vm, 'opts.vm');
assert.string(opts.vm.uuid, 'opts.vm.uuid');
assert.string(opts.vm.server_uuid, 'opts.vm.server_uuid');
assert.optionalBool(opts.exp, 'opts.exp');
assert.optionalObject(opts.expErr, 'opts.expErr');

var desc = fmt(' (server %s, rule uuid=%s)', opts.vm.server_uuid,
    opts.vm.uuid);
fork icon7
star icon4
watch icon0

41
42
43
44
45
46
47
48
49
50
vasync.forEachParallel({
    func: function validateOneNetwork(networkUuid, cb) {
        napiClient.getNetwork(networkUuid, function onGetNetwork(err, net) {
            if (!err && net) {
                assert.object(net, 'net');
                assert.optionalBool(net.fabric, 'net.fabric');
                assert.optionalArrayOfUuid(net.owner_uuids,
                    'net.owner_uuids');

                if (net.fabric !== true) {
fork icon6
star icon8
watch icon0

+ 3 other calls in file

820
821
822
823
824
825
826
827
828
829
830
function updateReferencesAndReservationsForVm(vmUuid, options, callback) {
    mod_assert.uuid(vmUuid, 'vmUuid');
    mod_assert.object(options, 'options');
    mod_assert.object(options.log, 'options.log');
    mod_assert.object(options.vmapiClient, 'options.vmapiClient');
    mod_assert.optionalBool(options.considerProvisioningVmFailed,
        'opts.considerProvisioningVmFailed');
    mod_assert.func(callback, 'callback');


    var considerProvisioningVmFailed = options.considerProvisioningVmFailed;
fork icon6
star icon8
watch icon0

21
22
23
24
25
26
27
28
29
30
var self = this;

mod_assert.object(opts, 'opts');
mod_assert.number(opts.defaultTimeout, 'opts.defaultTimeout');
mod_assert.number(opts.defaultTTL, 'opts.defaultTTL');
mod_assert.optionalBool(opts.enforceRoot, 'opts.enforceRoot');
mod_assert.object(opts.log, 'opts.log');

self.log = opts.log;
self.defaultTimeout = opts.defaultTimeout;
fork icon8
star icon3
watch icon0

90
91
92
93
94
95
96
97
98
99
assert.uuid(volumeUuid, 'volumeUuid');
assert.object(options, 'options');
assert.arrayOfString(options.successStates, 'options.successStates');
assert.arrayOfString(options.failureStates, 'options.failureStates');
assert.optionalNumber(options.timeout, 'options.timeout');
assert.optionalBool(options.successOnVolumeNotFound,
        'options.successOnVolumeNotFound');
assert.func(callback, 'callback');

var pollStart;
fork icon13
star icon2
watch icon0

735
736
737
738
739
740
741
742
743
744
745
UFDS.prototype._getUser = function _getUser(filter, msg, account, cb, noCache) {
    assert.ok(ldap.filters.isFilter(filter), 'filter');
    assert.string(msg, 'msg');
    assert.optionalString(account, 'account');
    assert.func(cb, 'callback');
    assert.optionalBool(noCache, 'noCache');


    cb = once(cb);
    noCache = noCache || false;

fork icon9
star icon3
watch icon0

326
327
328
329
330
331
332
333
334
335
function assertArgsList(t, opts, callback) {
    assert.object(t, 't');
    assert.object(opts, 'opts');
    assert.optionalObject(opts.params, 'opts.params');
    assert.optionalObject(opts.expErr, 'opts.expErr');
    assert.optionalBool(opts.deepEqual, 'opts.deepEqual');
    assert.optionalArrayOfObject(opts.present, 'opts.present');
    assert.optionalFunc(callback, 'callback');
}

fork icon6
star icon0
watch icon0

216
217
218
219
220
221
222
223
224
225
226
 * @param cb {Function} `function (err, new RecoveryConfigurationTransition())`
 */
function updateRecoveryConfigurationTransition(opts, callback) {
    assert.object(opts, 'opts');
    assert.object(opts.val, 'opts.val');
    assert.optionalBool(opts.remove, 'opts.remove');
    assert.func(callback, 'callback');


    // None of the initially required properties of the object
    // can be modified. Let's ensure it:
fork icon6
star icon0
watch icon0

107
108
109
110
111
112
113
114
115
116
117
118
 *      - otherwise; exists will be true or false
 */
DummyVmadm.prototype.exists = function vmExists(opts, callback) {
    assert.object(opts, 'opts');
    assert.string(opts.uuid, 'opts.uuid');
    assert.optionalBool(opts.include_dni, 'opts.include_dni');


    var self = this;


    self.load(opts, {fields: ['uuid']}, function _onLoad(err, vm) {
fork icon3
star icon3
watch icon0

+ 25 other calls in file

554
555
556
557
558
559
560
561
562
563
    options = {};
}
assert.object(filters, 'filters');
assert.object(options, 'options');
assert.optionalObject(options.headers, 'options.headers');
assert.optionalBool(options.inclAdminFields, 'options.inclAdminFields');
assert.optionalString(options.channel, 'options.channel');
assert.func(callback, 'callback');
var inclAdminFields = (options.inclAdminFields !== undefined
        ? options.inclAdminFields.toString()
fork icon13
star icon2
watch icon36

+ 7 other calls in file