-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: allow to validate enums with validateOneOf
PR-URL: #34070 Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
258f64f
commit 04defba
Showing
10 changed files
with
108 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { validateOneOf } = require('internal/validators'); | ||
|
||
{ | ||
// validateOneOf number incorrect. | ||
const allowed = [2, 3]; | ||
assert.throws(() => validateOneOf(1, 'name', allowed), { | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
// eslint-disable-next-line quotes | ||
message: `The argument 'name' must be one of: 2, 3. Received 1` | ||
}); | ||
assert.throws(() => validateOneOf(1, 'name', allowed, true), { | ||
code: 'ERR_INVALID_OPT_VALUE', | ||
message: 'The value "1" is invalid for option "name". ' + | ||
'Must be one of: 2, 3' | ||
}); | ||
} | ||
|
||
{ | ||
// validateOneOf number correct. | ||
validateOneOf(2, 'name', [1, 2]); | ||
} | ||
|
||
{ | ||
// validateOneOf string incorrect. | ||
const allowed = ['b', 'c']; | ||
assert.throws(() => validateOneOf('a', 'name', allowed), { | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
// eslint-disable-next-line quotes | ||
message: `The argument 'name' must be one of: 'b', 'c'. Received 'a'` | ||
}); | ||
assert.throws(() => validateOneOf('a', 'name', allowed, true), { | ||
code: 'ERR_INVALID_OPT_VALUE', | ||
// eslint-disable-next-line quotes | ||
message: `The value "a" is invalid for option "name". ` + | ||
"Must be one of: 'b', 'c'", | ||
}); | ||
} | ||
|
||
{ | ||
// validateOneOf string correct. | ||
validateOneOf('two', 'name', ['one', 'two']); | ||
} | ||
|
||
{ | ||
// validateOneOf Symbol incorrect. | ||
const allowed = [Symbol.for('b'), Symbol.for('c')]; | ||
assert.throws(() => validateOneOf(Symbol.for('a'), 'name', allowed), { | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
// eslint-disable-next-line quotes | ||
message: `The argument 'name' must be one of: Symbol(b), Symbol(c). ` + | ||
'Received Symbol(a)' | ||
}); | ||
assert.throws(() => validateOneOf(Symbol.for('a'), 'name', allowed, true), { | ||
code: 'ERR_INVALID_OPT_VALUE', | ||
message: 'The value "Symbol(a)" is invalid for option "name". ' + | ||
'Must be one of: Symbol(b), Symbol(c)', | ||
}); | ||
} | ||
|
||
{ | ||
// validateOneOf Symbol correct. | ||
const allowed = [Symbol.for('b'), Symbol.for('c')]; | ||
validateOneOf(Symbol.for('b'), 'name', allowed); | ||
} |