How to use the optionalString function from assert-plus

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

assert-plus.optionalString is a function that validates whether an input parameter is a string or not, but allows it to be undefined.

538
539
540
541
542
543
544
545
546
547
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');
assert.optionalString(options.user, 'options.user');
assert.optionalString(options.subuser, 'options.subuser');
assert.optionalArrayOfString(options.role, 'options.role');
if (options.sign === null)
fork icon41
star icon75
watch icon0

+ 21 other calls in file

110
111
112
113
114
115
116
117
118
119
assert.object(options, 'options');
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;
fork icon38
star icon55
watch icon0

+ 188 other calls in file

How does assert-plus.optionalString work?

The assert-plus.optionalString method is a validation function that checks if the given value is either a string or undefined/null. If the value is undefined/null, it returns true. If it is a string, it checks if the string length is greater than or equal to zero, and returns true if it is. Otherwise, it throws an error.

500
501
502
503
504
505
506
507
508
509
IMGADM.prototype._addSource = function _addSource(
        sourceInfo, skipPingCheck, callback) {
    assert.object(sourceInfo, 'sourceInfo');
    assert.string(sourceInfo.url, 'sourceInfo.url');
    assert.string(sourceInfo.type, 'sourceInfo.type');
    assert.optionalString(sourceInfo.channel, 'sourceInfo.channel');
    assert.optionalBool(sourceInfo.insecure, 'sourceInfo.secure');
    assert.bool(skipPingCheck, 'skipPingCheck');
    assert.func(callback, 'callback');
    var self = this;
fork icon248
star icon0
watch icon0

+ 11 other calls in file

90
91
92
93
94
95
96
97
98
99
 *      consider. This will be used by the default crudChangesForUpdate.
 */
function crudChangesForThings(opts) {
    assert.object(opts, 'opts');
    assert.string(opts.type, 'opts.type');
    assert.optionalString(opts.desc, 'opts.desc');
    assert.string(opts.idField, 'opts.idField');
    assert.arrayOfObject(opts.have, 'opts.have');
    assert.arrayOfObject(opts.want, 'opts.want');
    assert.optionalFunc(opts.crudChangesForCreate, 'opts.crudChangesForCreate');
fork icon38
star icon55
watch icon0

Ai Example

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

function printMessage(msg) {
  assert.optionalString(msg, "msg");
  console.log(`Message: ${msg || "Default message"}`);
}

printMessage("Hello world"); // Message: Hello world
printMessage(); // Message: Default message
printMessage(123); // Throws an error because 123 is not a string

149
150
151
152
153
154
155
156
157
158
 */
/* 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');
    assert.optionalFunc(opts.unlockKeyFn, 'opts.unlockKeyFn');
fork icon38
star icon55
watch icon0

260
261
262
263
264
265
266
267
268
269
 * See: docker/docker.git:registry/token.go
 */
function _getToken(opts, cb) {
    assert.string(opts.indexName, 'opts.indexName'); // used for error messages
    assert.string(opts.realm, 'opts.realm');
    assert.optionalString(opts.service, 'opts.service');
    assert.optionalArrayOfString(opts.scopes, 'opts.scopes');
    assert.optionalString(opts.username, 'opts.username');
    assert.optionalString(opts.password, 'opts.password');
    // HTTP client opts:
fork icon35
star icon59
watch icon0

+ 15 other calls in file

514
515
516
517
518
519
520
521
522
523
524
 * @return {Array} Array of cmdln options objects
 */
function getCliTableOptions(opts) {
    opts = opts || {};
    assert.object(opts, 'opts');
    assert.optionalString(opts.columnsDefault, 'opts.columnsDefault');
    assert.optionalString(opts.sortDefault, 'opts.sortDefault');
    assert.optionalBool(opts.includeLong, 'opts.includeLong');


    var o;
fork icon38
star icon55
watch icon41

+ 7 other calls in file

646
647
648
649
650
651
652
653
654
655
656
function pgCreate(opts) {
    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;

fork icon21
star icon60
watch icon0

+ 3 other calls in file

616
617
618
619
620
621
622
623
624
assert.optionalObject(options.query, 'options.query');
assert.optionalFunc(options.signRequest, 'options.signRequest');
assert.optionalString(options.socketPath, 'options.socketPath');
assert.optionalString(options.url, 'options.url');
assert.optionalBool(options.followRedirects, 'options.followRedirects');
assert.optionalString(options.noProxy, 'options.noProxy');
assert.optionalNumber(options.maxRedirects, 'options.maxRedirects');

EventEmitter.call(this);
fork icon34
star icon55
watch icon0

+ 5 other calls in file

26
27
28
29
30
31
32
33
34
35
36
function CreateInstanceProcedure(opts) {
    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;
fork icon19
star icon13
watch icon0

48
49
50
51
52
53
54
55
56
57
58
59
function getKeyFingerprint(key) {
    return (sshpk.parseKey(key, 'auto').fingerprint('md5').toString());
}


function createLogger(name) {
    assert.optionalString(name, 'name');


    var log = bunyan.createLogger({
        level: (process.env.LOG_LEVEL || 'warn'),
        name: name || process.argv[1],
fork icon16
star icon13
watch icon0

977
978
979
980
981
982
983
984
985
986
987
 * to moray according to the sort parameter "sortparam" that was passed
 * by the HTTP client.
 * If no sort option needs to be set, an empty array is returned.
 */
function _sortOptionsFromSortParam(sortParam) {
    assert.optionalString(sortParam, 'sortParam must be a string or undefined');


    var sortOptions = [];
    var order;

fork icon20
star icon11
watch icon0

426
427
428
429
430
431
432
433
434
435
 * Migrates a vm with ?action=migrate
 */
function migrateVm(req, res, next) {
    assert.object(req, 'req');
    assert.object(req.params, 'req.params');
    assert.optionalString(req.params.migration_action,
        'req.params.migration_action');
    assert.object(req.vm, 'req.vm');
    assert.object(res, 'res');
    assert.object(req.filteredNetworks, 'req.filteredNetworks');
fork icon20
star icon11
watch icon0

961
962
963
964
965
966
967
968
969
970
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 (
            args.sources.alarms.state !== 'open' &&
            args.sources.alarms.state !== 'all' &&
fork icon13
star icon7
watch icon42

+ 4 other calls in file

733
734
735
736
737
738
739
740
741
742
743
 * lookup.
 */
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);
fork icon9
star icon3
watch icon0

311
312
313
314
315
316
317
318
319
320
    assert.optionalObject(opts.exp, 'opts.exp');
    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

152
153
154
155
156
157
158
159
160
161
162
 * @param callback {Function} `function (err)`
 */
DummyVmadm.prototype.load = function vmLoad(opts, vmopts, callback) {
    assert.object(opts, 'opts');
    assert.string(opts.uuid, 'opts.uuid');
    assert.optionalString(opts.req_id, 'opts.req_id');
    assert.optionalBool(opts.include_dni, 'opts.include_dni');


    var self = this;

fork icon3
star icon3
watch icon0

+ 19 other calls in file

260
261
262
263
264
265
266
267
268
269
function IMGAPI(options) {
    assert.object(options, 'options');
    assert.string(options.url, 'options.url');
    assert.optionalString(options.channel, 'options.channel');
    assert.optionalString(options.user, 'options.user');
    assert.optionalString(options.password, 'options.password');
    assert.optionalFunc(options.sign, 'options.sign');
    assert.ok(!(options.password && options.sign),
        'not both "options.password" and "options.sign"');
    if (options.version) {
fork icon13
star icon2
watch icon36

+ 57 other calls in file

372
373
374
375
376
377
378
379
380
381
    type: options.type
};

switch (options.type) {
case 'object':
    assert.optionalString(options.contentDisposition,
            'options.contentDisposition');
    assert.number(options.contentLength, 'options.contentLength');
    assert.string(options.contentMD5, 'options.contentMD5');
    assert.string(options.contentType, 'options.contentType');
fork icon6
star icon2
watch icon0

283
284
285
286
287
288
289
290
291
 *                              was not present.
 */
signRequest: function signRequest(request, options) {
  assert.object(request, 'request');
  assert.object(options, 'options');
  assert.optionalString(options.algorithm, 'options.algorithm');
  assert.string(options.keyId, 'options.keyId');
  assert.optionalArrayOfString(options.headers, 'options.headers');
  assert.optionalString(options.httpVersion, 'options.httpVersion');
fork icon1
star icon0
watch icon0