How to use the optionalObject function from assert-plus

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

assert-plus.optionalObject is a function provided by the assert-plus library that validates that an argument is an object, or null or undefined.

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;
fork icon41
star icon75
watch icon0

+ 11 other calls in file

255
256
257
258
259
260
261
262
263
264
265
function jobWait(opts, cb) {
    assert.object(opts, 'options');
    assert.object(opts.client, 'options.client');
    assert.string(opts.id, 'options.id');
    assert.object(opts.opts, 'options.opts');
    assert.optionalObject(opts.baropts, 'options.baropts');
    assert.optionalBool(opts.catOutputOnError, 'options.catOutputOnError');
    assert.optionalFunc(cb, 'callback');


    if (!opts.opts.watch && !opts.opts.cat_outputs) {
fork icon41
star icon75
watch icon57

+ 3 other calls in file

How does assert-plus.optionalObject work?

assert-plus.optionalObject is a function provided by the assert-plus library that validates that an argument is an object, or null or undefined. When you call assert-plus.optionalObject, you pass in the value to be validated, as well as an optional error message and options object. The function then checks that the value is an object, or null or undefined, and throws an error with the specified message if it is not. If the value is valid, the function simply returns without throwing an error. The behavior of assert-plus.optionalObject can be customized using the options object. For example, you can specify a custom error message, disable coercion of non-object values to objects, or allow empty objects. Overall, assert-plus.optionalObject provides a convenient way to validate that an argument is an object or null or undefined, which can be useful for ensuring that your functions are called with the correct arguments.

111
112
113
114
115
116
117
118
119
120
assert.string(options.url, 'options.url');
assert.string(options.account, 'options.account');

assert.optionalArrayOfString(options.roles, 'options.roles');
assert.optionalString(options.version, 'options.version');
assert.optionalObject(options.log, 'options.log');

assert.optionalObject(options.principal, 'options.principal');
this.principal = options.principal;
if (options.principal === undefined) {
fork icon38
star icon55
watch icon0

+ 55 other calls in file

35
36
37
38
39
40
41
42
43
44
45
46
47


// ---- support stuff


function objCopy(obj, target) {
    assert.object(obj, 'obj');
    assert.optionalObject(obj, 'target');


    if (target === undefined) {
        target = {};
    }
fork icon38
star icon55
watch icon41

+ 19 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const assert = require("assert-plus");

function processObject(obj) {
  assert.optionalObject(obj, "obj");
  console.log(`Processing object with properties: ${JSON.stringify(obj)}`);
}

// Call the function with a valid object argument
processObject({ prop1: "value1", prop2: "value2" });

// Call the function with a null argument
processObject(null);

// Call the function with an undefined argument
processObject(undefined);

// Call the function with an invalid argument
processObject("not an object");

In this example, assert-plus.optionalObject is used to validate an object argument passed to a function called processObject. The function expects an object argument, but it is not required. First, we call the function with a valid object argument. The assert.optionalObject call does not throw an error, so the function proceeds to process the object and logs its properties to the console. Next, we call the function with a null argument. Again, the assert.optionalObject call does not throw an error, so the function proceeds to log a message indicating that it is processing an object with no properties. We repeat this process with an undefined argument, and again, the function proceeds without throwing an error. Finally, we call the function with an invalid argument that is not an object. In this case, assert.optionalObject throws an error with the message 'obj' must be an object or null/undefined because the value is not an object, null, or undefined. Note that assert-plus.optionalObject can be customized using the options object, which can be used to modify its behavior. For example, you can specify a custom error message, disable coercion of non-object values to objects, or allow empty objects.

199
200
201
202
203
204
205
206
207
208
209
function _releaseTicketAndSendResponse(ticket, req, res, next, error) {
    assert.object(ticket, 'ticket');
    assert.object(req, 'req');
    assert.object(res, 'res');
    assert.func(next, 'next');
    assert.optionalObject(error, 'error');


    if (ticket !== undefined) {
        _releaseVolumeTicket(ticket, {
            cnapiClient: req._cnapiClient,
fork icon6
star icon8
watch icon0

+ 5 other calls in file

227
228
229
230
231
232
233
234
235
236
237
238
function VolumesUpdater(options) {
    this._changefeedListener = null;


    mod_assert.object(options, 'options');


    mod_assert.optionalObject(options.log, 'options.log');
    this._log = options.log || mod_bunyan.createLogger({
        name: 'vms-updater',
        level: 'info',
        stream: process.stderr
fork icon6
star icon8
watch icon0

+ 7 other calls in file

959
960
961
962
963
964
965
966
967
968
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') {
        if (
fork icon13
star icon7
watch icon42

218
219
220
221
222
223
224
225
226
227
} else if (typeof (options) === 'function') {
    callback = options;
    options = undefined;
}

assert.optionalObject(params, 'params');
assert.optionalObject(options, 'options');
assert.func(callback, 'callback');

var reqOpts = { path: '/volumes', query: params };
fork icon13
star icon2
watch icon0

+ 27 other calls in file

429
430
431
432
433
434
435
436
437
438
439
function UFDS(opts) {
    assert.object(opts, 'options');
    assert.string(opts.bindDN, 'options.bindDN');
    assert.string(opts.bindPassword, 'options.bindPassword');
    assert.ok(!opts.bindCredentials, 'options.bindCredentials not supported');
    assert.optionalObject(opts.log, 'options.log');
    assert.string(opts.url, 'options.url');


    var self = this;
    EventEmitter.call(this);
fork icon9
star icon3
watch icon0

286
287
288
289
290
291
292
293
294
295
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);
var checkOpts = {
fork icon7
star icon4
watch icon0

+ 13 other calls in file

137
138
139
140
141
142
143
144
145
146
147
 *     }
 *   }
 */
VMCache.prototype.addTags = function addTags(owner, tags) {
    assert.optionalUuid(owner, 'owner');
    assert.optionalObject(tags, 'tags');


    if (!tags || prim.isEmpty(tags)) {
        return;
    }
fork icon6
star icon1
watch icon0

312
313
314
315
316
317
318
319
320
321
    assert.optionalObject(opts.expErr, 'opts.expErr');
    assert.optionalObject(opts.partialExp, 'opts.partialExp');
    assert.ok(opts.exp || opts.partialExp || opts.expErr,
        'one of exp, expErr, partialExp required');
    assert.optionalString(opts.etag, 'opts.etag');
    assert.optionalObject(opts.params, 'opts.params');
    assert.optionalObject(opts.state, 'opts.state');
    assert.optionalFunc(callback, 'callback');
}

fork icon6
star icon0
watch icon0

+ 13 other calls in file

74
75
76
77
78
79
80
81
82
83
84
85
 */
function DummyVmadm(opts) {
    assert.object(opts);
    assert.object(opts.log, 'opts.log');
    assert.string(opts.serverRoot, 'opts.serverRoot');
    assert.optionalObject(opts.sysinfo, 'opts.sysinfo');


    var self = this;


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

1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
IMGADM.prototype._downloadImageFile = function _downloadImageFile(opts, cb) {
    var self = this;
    assert.func(cb, 'cb');
    assert.object(opts, 'opts');
    assert.object(opts.source, 'opts.source');
    assert.optionalObject(opts.bar, 'opts.bar');
    assert.optionalBool(opts.zstream, 'opts.zstream');
    assert.func(opts.logCb, 'opts.logCb');
    // As from `source.getImportInfo`.
    assert.object(opts.importInfo, 'opts.importInfo');
fork icon248
star icon0
watch icon0

+ 3 other calls in file

148
149
150
151
152
153
154
155
156
157
 * @param {Function} cb - `function (err, client)`
 */
/* END JSSTYLED */
function createClient(opts, cb) {
    assert.object(opts, 'opts');
    assert.optionalObject(opts.profile, 'opts.profile');
    assert.optionalString(opts.profileName, 'opts.profileName');
    assert.optionalObject(opts.config, 'opts.config');
    assert.optionalString(opts.configDir, 'opts.configDir');
    assert.optionalObject(opts.log, 'opts.log');
fork icon38
star icon55
watch icon0

+ 2 other calls in file

46
47
48
49
50
51
52
53
54
55
56
57
58
59


// --- internal support functions




function _createLogger(log) {
    assert.optionalObject(log, 'log');


    if (log) {
        // TODO avoid this .child if already have the serializers, e.g. for
        // recursive call.
fork icon35
star icon59
watch icon0

+ 11 other calls in file

59
60
61
62
63
64
65
66
67
68
69
function cloneRetryOptions(options, defaults) {
    if (options === false) {
        return (false);
    }


    assert.optionalObject(options, 'options.retry');
    var r = options || {};
    assert.optionalNumber(r.minTimeout, 'options.retry.minTimeout');
    assert.optionalNumber(r.maxTimeout, 'options.retry.maxTimeout');
    assert.optionalNumber(r.retries, 'options.retry.retries');
fork icon34
star icon55
watch icon0

+ 15 other calls in file

172
173
174
175
176
177
178
179
180
181
182
function auditLogger(opts) {
    assert.object(opts, 'opts');
    assert.object(opts.log, 'opts.log');
    assert.string(opts.event, 'opts.event');
    assert.optionalFunc(opts.context, 'opts.context');
    assert.optionalObject(opts.server, 'opts.server');
    assert.optionalBool(opts.printLog, 'opts.printLog');
    assert.optionalObject(opts.serializers, 'opts.serializers');


    if (
fork icon0
star icon2
watch icon0

1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
 */
function _trySetupBucket(bucketName, bucketConfig, cb) {
    assert.string(bucketName, 'bucketName');
    assert.object(bucketConfig, 'bucketConfig');
    assert.object(bucketConfig.schema, 'bucketConfig.schema');
    assert.optionalObject(bucketConfig.schema.options,
        'bucketConfig.schema.options');
    if (bucketConfig.schema.options) {
        assert.optionalNumber(bucketConfig.schema.options.version,
            'bucketConfig.schema.options.version');
fork icon20
star icon11
watch icon0

+ 3 other calls in file

40
41
42
43
44
45
46
47
48
49
50
51
];




function validateMigrationBegin(vm, ctx, callback) {
    assert.object(vm, 'vm');
    assert.optionalObject(vm.tags, 'vm.tags');
    assert.object(ctx, 'ctx');
    assert.object(ctx.app, 'ctx.app');
    assert.object(ctx.app.options, 'ctx.app.options');
    assert.optionalBool(ctx.app.options.userMigrationAllowed,
fork icon20
star icon11
watch icon0

+ 2 other calls in file