How to use the arrayOfString function from assert-plus

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

assert-plus.arrayOfString is a function that validates whether a given input is an array containing only strings.

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

+ 3 other calls in file

2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
assert.string(j, 'job');
if (!Array.isArray(k)) {
    assert.string(k, 'key');
    k = [k];
} else {
    assert.arrayOfString(k, 'keys');
}
if (typeof (opts) === 'function') {
    cb = opts;
    opts = {};
fork icon41
star icon75
watch icon0

How does assert-plus.arrayOfString work?

assert-plus.arrayOfString is a validation function provided by the assert-plus module in Node.js, which checks if a given value is an array containing only string values. Internally, it uses the Array.isArray() method to check if the given value is an array, and then it iterates over the array to check if every element is of type "string" using the typeof operator. If any element in the array is not a string, or if the given value is not an array, it throws a TypeError with an appropriate error message. Otherwise, it returns the input array.

1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
CloudApi.prototype.waitForImageStates =
function waitForImageStates(opts, cb) {
    var self = this;
    assert.object(opts, 'opts');
    assert.uuid(opts.id, 'opts.id');
    assert.arrayOfString(opts.states, 'opts.states');
    assert.optionalNumber(opts.interval, 'opts.interval');
    assert.func(cb, 'cb');
    var interval = (opts.interval === undefined ? 1000 : opts.interval);
    assert.ok(interval > 0, 'interval must be a positive number');
fork icon38
star icon55
watch icon0

+ 41 other calls in file

228
229
230
231
232
233
234
235
236
237
 *      valid keys.
 * @param {String} compositionType - the way each key/value pair will be
 *  combined to form a JSON predicate. Valid values are 'or' and 'and'.
 */
function jsonPredFromKv(kvs, validKeys, compositionType) {
    assert.arrayOfString(kvs, 'kvs');
    assert.string(compositionType, 'string');
    assert.ok(compositionType === 'or' || compositionType === 'and',
        'compositionType');

fork icon38
star icon55
watch icon41

+ 5 other calls in file

Ai Example

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

function validateArrayOfObjects(arr) {
  assert.arrayOfObject(arr, "arr must be an array of objects");
  // do something with arr
}

validateArrayOfObjects([
  { name: "John", age: 30 },
  { name: "Jane", age: 25 },
]); // this will pass
validateArrayOfObjects(["John", "Jane"]); // this will throw an AssertionError

In the above example, the validateArrayOfObjects function uses assert.arrayOfObject to ensure that the input arr is an array of objects. If the input is not an array of objects, then an AssertionError will be thrown with the message arr must be an array of objects.

23
24
25
26
27
28
29
30
31
32
33
34
var steps = require('../steps');




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');
fork icon19
star icon13
watch icon0

427
428
429
430
431
432
433
434
435
436
function createPolicies(ctx, next) {
    vasync.forEachPipeline({
        inputs: ctx.policiesToCreate,
        func: function createOne(policy, nextPolicy) {
            assert.string(policy.name, 'policy.name');
            assert.arrayOfString(policy.rules, 'policy.rules');

            t.comment('creating policy ' + policy.name);
            ctx.smartdcClient.createPolicy(policy, function (err) {
                ctx.madeAdditions = true;
fork icon16
star icon13
watch icon34

1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
function renderVolumeReferences(req, res, next) {
    assert.object(req, 'req');
    assert.object(res, 'res');
    assert.func(next, 'next');


    assert.arrayOfString(req.volumeReferences, 'req.volumeReferences');
    req.renderedResponse = req.volumeReferences;


    next();
}
fork icon6
star icon8
watch icon0

982
983
984
985
986
987
988
989
990
991
        'cannot specify "sources.alarms.state" and ' +
            '"sources.alarms.alarmIds"'
    );
    alarmstate = args.sources.alarms.state;
} else {
    assertplus.arrayOfString(
        args.sources.alarms.alarmIds,
        'must specify "sources.alarms.state" or ' +
            '"sources.alarms.alarmIds'
    );
fork icon13
star icon7
watch icon42

+ 4 other calls in file

87
88
89
90
91
92
93
94
95
96
97


function pollVolumeStateChange(client, volumeUuid, options, callback) {
    assert.object(client, 'client');
    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');
fork icon13
star icon2
watch icon0

+ 3 other calls in file

1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
    fingerprint: fingerprint,
    name: key.name || pkey.comment || fingerprint,
    objectclass: 'sdckey'
};
if (key.attestation) {
    assert.arrayOfString(key.attestation, 'key.attestation');
    entry.attestation = key.attestation;
}

// We are searching keys by fingerprint or name before allowing
fork icon9
star icon3
watch icon0

258
259
260
261
262
263
264
265
266
267
assert.string(name, 'name');
if (callback === undefined) {
    callback = properties;
    properties = [];
}
assert.arrayOfString(properties, 'properties');

if (properties.indexOf('name') === -1) {
    properties.push('name');
}
fork icon248
star icon0
watch icon0

+ 3 other calls in file

24
25
26
27
28
29
30
31
32
33
34
35
const UPDATES_URL = process.env.UPDATES_IMGADM_URL ||
    'https://updates.tritondatacenter.com/';


function checkUpdateResults(t, err, stdout, stderr, moreStrings) {
    if (moreStrings) {
        assert.arrayOfString(moreStrings, 'moreStrings');
    }


    t.ifError(err);
    t.equal(stderr, '');
fork icon19
star icon13
watch icon0

54
55
56
57
58
59
60
61
62
63
64
65
}


function runValidTestCase(t, testCase, callback) {
    assert.object(t, 't');
    assert.object(testCase, 'testCase');
    assert.arrayOfString(testCase.queryStrings, 'testCase.queryStrings');
    assert.arrayOfObject(testCase.vmsToCreate, 'testCase.vmsToCreate');
    assert.func(callback, 'callback');


    var createdVmUuids = [];
fork icon20
star icon11
watch icon0

518
519
520
521
522
523
524
525
526
527
528
 * validation on such parameters already took place, and thus we consider these
 * parameters valid.
 */
function _addInternalMetadataFilter(params, filter) {
    assert.object(params, 'params');
    assert.arrayOfString(filter, 'filter');


    var FILTER_KEY = 'internal_metadata_search_array';
    var idx;
    var metadataKey;
fork icon20
star icon11
watch icon0

2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
} else if (typeof (options) === 'function') {
    callback = options;
    options = {};
}
assert.optionalString(account, 'account');
assert.arrayOfString(acl, 'acl');
assert.object(options, 'options');
assert.optionalObject(options.headers, 'options.headers');
assert.optionalString(options.channel, 'options.channel');
assert.func(callback, 'callback');
fork icon13
star icon2
watch icon36

1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
    // },
};


function validateFields(manifest, requiredFields, options) {
    assert.object(manifest, 'manifest');
    assert.arrayOfString(requiredFields, 'requiredFields');
    assert.optionalObject(options, 'options');


    var errs = []; // validation errors

fork icon5
star icon2
watch icon0

+ 2 other calls in file

112
113
114
115
116
117
118
119
120
}
if (options.headers === undefined) {
  options.headers = [request.headers['x-date'] ? 'x-date' : 'date'];
}
assert.object(options, 'options');
assert.arrayOfString(options.headers, 'options.headers');
assert.optionalFinite(options.clockSkew, 'options.clockSkew');

var authzHeaderName = options.authorizationHeaderName || 'authorization';
fork icon1
star icon0
watch icon0

456
457
458
459
460
461
462
463
464
465
 * Validates a "fields" array - an array of strings specifying which of an
 * object's fields to return in a response.  `fields` is the list of allowed
 * fields that can be in the array.
 */
function validateFieldsArray(fields) {
    assert.arrayOfString(fields, 'fields');
    return function _validateFieldsArray(_, name, arr, callback) {
        if (!Array.isArray(arr)) {
            callback(new errors.invalidParam(name,
                    constants.msg.ARRAY_OF_STR));
fork icon1
star icon0
watch icon0