• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4require('../common');
5const assert = require('assert');
6const { validateOneOf } = require('internal/validators');
7
8{
9  // validateOneOf number incorrect.
10  const allowed = [2, 3];
11  assert.throws(() => validateOneOf(1, 'name', allowed), {
12    code: 'ERR_INVALID_ARG_VALUE',
13    // eslint-disable-next-line quotes
14    message: `The argument 'name' must be one of: 2, 3. Received 1`
15  });
16  assert.throws(() => validateOneOf(1, 'name', allowed, true), {
17    code: 'ERR_INVALID_OPT_VALUE',
18    message: 'The value "1" is invalid for option "name". ' +
19      'Must be one of: 2, 3'
20  });
21}
22
23{
24  // validateOneOf number correct.
25  validateOneOf(2, 'name', [1, 2]);
26}
27
28{
29  // validateOneOf string incorrect.
30  const allowed = ['b', 'c'];
31  assert.throws(() => validateOneOf('a', 'name', allowed), {
32    code: 'ERR_INVALID_ARG_VALUE',
33    // eslint-disable-next-line quotes
34    message: `The argument 'name' must be one of: 'b', 'c'. Received 'a'`
35  });
36  assert.throws(() => validateOneOf('a', 'name', allowed, true), {
37    code: 'ERR_INVALID_OPT_VALUE',
38    // eslint-disable-next-line quotes
39    message: `The value "a" is invalid for option "name". ` +
40    "Must be one of: 'b', 'c'",
41  });
42}
43
44{
45  // validateOneOf string correct.
46  validateOneOf('two', 'name', ['one', 'two']);
47}
48
49{
50  // validateOneOf Symbol incorrect.
51  const allowed = [Symbol.for('b'), Symbol.for('c')];
52  assert.throws(() => validateOneOf(Symbol.for('a'), 'name', allowed), {
53    code: 'ERR_INVALID_ARG_VALUE',
54    // eslint-disable-next-line quotes
55    message: `The argument 'name' must be one of: Symbol(b), Symbol(c). ` +
56      'Received Symbol(a)'
57  });
58  assert.throws(() => validateOneOf(Symbol.for('a'), 'name', allowed, true), {
59    code: 'ERR_INVALID_OPT_VALUE',
60    message: 'The value "Symbol(a)" is invalid for option "name". ' +
61      'Must be one of: Symbol(b), Symbol(c)',
62  });
63}
64
65{
66  // validateOneOf Symbol correct.
67  const allowed = [Symbol.for('b'), Symbol.for('c')];
68  validateOneOf(Symbol.for('b'), 'name', allowed);
69}
70