Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make json ArgumentType accept anything but undefined #545

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions packages/buidler-core/src/internal/core/params/argumentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,16 @@ export const json: ArgumentType<any> = {
}
},
/**
* Check if argument value is of type "json"
* "json" validation succeeds if it is of "object map"-like structure or of "array" structure
* ie. this excludes 'null', function, numbers, date, regexp, etc.
* Check if argument value is of type "json". We consider everything except
* undefined to be json.
*
* @param argName {string} argument's name - used for context in case of error.
* @param value {any} argument's value to validate.
*
* @throws BDLR301 if value is not of type "json"
*/
validate: (argName: string, value: any): void => {
const valueTypeString = Object.prototype.toString.call(value);
const isJsonValue =
valueTypeString === "[object Object]" ||
valueTypeString === "[object Array]";

if (!isJsonValue) {
if (value === undefined) {
throw new BuidlerError(ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
value,
name: argName,
Expand Down
10 changes: 10 additions & 0 deletions packages/buidler-core/test/internal/core/params/argumentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,15 @@ describe("argumentTypes", () => {
it("Should parse a string", () => {
assert.deepEqual(types.json.parse("j", '"a"'), "a");
});

it("Should accept anything except undefined as valid", () => {
assert.doesNotThrow(() => types.json.validate!("json", 1));
assert.doesNotThrow(() => types.json.validate!("json", "asd"));
assert.doesNotThrow(() => types.json.validate!("json", [1]));
assert.doesNotThrow(() => types.json.validate!("json", { a: 123 }));
assert.doesNotThrow(() => types.json.validate!("json", null));

assert.throws(() => types.json.validate!("json", undefined));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ describe("Environment", () => {
"some default",
types.string
)
.addOptionalParam(
"optJsonParam",
"an opt 'json' param",
JSON.stringify({ a: 1 }),
types.json
)
.addOptionalVariadicPositionalParam(
"variadicOptStrParam",
"an opt variadic 'str' param",
Expand Down Expand Up @@ -223,7 +217,6 @@ describe("Environment", () => {
optIntParam: { valid: 10, invalid: 1.2 },
optFloatParam: { valid: 1.2, invalid: NaN },
optStringParam: { valid: "a string", invalid: 123 },
optJsonParam: { valid: { a: 20 }, invalid: 1234 },
optFileParam: { valid: __filename, invalid: __dirname },
variadicOptStrParam: { valid: ["a", "b"], invalid: ["a", 1] },
};
Expand Down