-
-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: argument validation of parameters with defaults, Map and Set (#7435
- Loading branch information
Showing
4 changed files
with
188 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
import { validateArgs } from "@zwave-js/transformers"; | ||
import assert from "node:assert"; | ||
|
||
class Test { | ||
@validateArgs() | ||
foo(arg1: number = 5): void { | ||
arg1; | ||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
bar(arg1: number | string = 5): void { | ||
arg1; | ||
return void 0; | ||
} | ||
} | ||
|
||
const test = new Test(); | ||
// These should not throw | ||
test.foo(); | ||
test.foo(1); | ||
test.foo(undefined); | ||
|
||
test.bar(); | ||
test.bar(1); | ||
test.bar("str"); | ||
test.bar(undefined); | ||
|
||
// These should throw | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.foo(true), | ||
/optional parameter arg1 to be a number, got true/, | ||
); | ||
|
||
assert.throws( | ||
// @ts-expect-error | ||
() => test.bar(true), | ||
/optional parameter arg1 to be one of (number \| string|string \| number), got true/, | ||
); |
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,79 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
import { validateArgs } from "@zwave-js/transformers"; | ||
import assert from "node:assert"; | ||
|
||
class Test { | ||
@validateArgs() | ||
map(arg1: Map<any, any>): void { | ||
arg1; | ||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
readonlyMap(arg1: ReadonlyMap<any, any>): void { | ||
arg1; | ||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
set(arg1: Set<any>): void { | ||
arg1; | ||
return void 0; | ||
} | ||
|
||
@validateArgs() | ||
readonlySet(arg1: ReadonlySet<any>): void { | ||
arg1; | ||
return void 0; | ||
} | ||
} | ||
|
||
const test = new Test(); | ||
// These should not throw | ||
test.map(new Map()); | ||
test.readonlyMap(new Map()); // readonly does not exist at runtime | ||
test.set(new Set()); | ||
test.readonlySet(new Set()); // readonly does not exist at runtime | ||
|
||
// These should throw | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.map({}), | ||
/arg1 to be an instance of class Map, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlyMap({}), | ||
/arg1 to be an instance of class Map, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.map("string"), | ||
/arg1 to be an instance of class Map, got string "string"/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlyMap("string"), | ||
/arg1 to be an instance of class Map, got string "string"/, | ||
); | ||
|
||
assert.throws( | ||
// @ts-expect-error | ||
() => test.set({}), | ||
/arg1 to be an instance of class Set, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlySet({}), | ||
/arg1 to be an instance of class Set, got object/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.set("string"), | ||
/arg1 to be an instance of class Set, got string "string"/, | ||
); | ||
assert.throws( | ||
// @ts-expect-error | ||
() => test.readonlySet("string"), | ||
/arg1 to be an instance of class Set, got string "string"/, | ||
); |
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