Skip to content
Open
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
16 changes: 2 additions & 14 deletions src/jsc/bindings/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,21 +1047,9 @@ JSC::EncodedJSValue INVALID_ARG_VALUE_RangeError(JSC::ThrowScope& throwScope, JS
}
JSC::EncodedJSValue INVALID_ARG_VALUE(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, JSC::JSValue name, JSC::JSValue value, const WTF::String& reason)
{
WTF::StringBuilder builder;
builder.append("The argument '"_s);
auto& vm = JSC::getVM(globalObject);
determineSpecificType(vm, globalObject, builder, name);
RELEASE_RETURN_IF_EXCEPTION(throwScope, {});

builder.append("' "_s);
builder.append(reason);
builder.append(". Received "_s);
JSValueToStringSafe(globalObject, builder, value, true);
auto nameString = name.toWTFString(globalObject);
RELEASE_RETURN_IF_EXCEPTION(throwScope, {});

throwScope.throwException(globalObject, createError(globalObject, ErrorCode::ERR_INVALID_ARG_VALUE, builder.toString()));
throwScope.release();
return {};
return INVALID_ARG_VALUE(throwScope, globalObject, nameString, value, reason);
}

// for validateOneOf
Expand Down
4 changes: 2 additions & 2 deletions src/jsc/bindings/NodeValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ JSC::EncodedJSValue V::validateArray(JSC::ThrowScope& scope, JSC::JSGlobalObject
auto minLength_num = minLength.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, {});
if (length_num < minLength_num) {
return Bun::ERR::INVALID_ARG_VALUE(scope, globalObject, name, value, makeString("must be longer than "_s, minLength_num));
return Bun::ERR::INVALID_ARG_VALUE(scope, globalObject, name, value, makeString("must have a length of at least "_s, minLength_num));
}
return JSValue::encode(jsUndefined());
}
Expand All @@ -372,7 +372,7 @@ JSC::EncodedJSValue V::validateArray(JSC::ThrowScope& scope, JSC::JSGlobalObject
auto minLength_num = minLength.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, {});
if (length_num < minLength_num) {
return Bun::ERR::INVALID_ARG_VALUE(scope, globalObject, name, value, makeString("must be longer than "_s, minLength_num));
return Bun::ERR::INVALID_ARG_VALUE(scope, globalObject, name, value, makeString("must have a length of at least "_s, minLength_num));
}
return JSValue::encode(jsUndefined());
}
Expand Down
44 changes: 44 additions & 0 deletions test/js/node/internal/validators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { exposedInternals } from "bun:internal-for-testing";
import { describe, expect, test } from "bun:test";

// The same functions node's lib/internal/validators.js exports, backed by
// src/jsc/bindings/NodeValidator.cpp. Expected messages are node v26.3.0's.
const { validateArray } = exposedInternals["internal/validators"];

describe("validateArray", () => {
test("accepts arrays that satisfy minLength", () => {
expect(() => validateArray([], "foo")).not.toThrow();
expect(() => validateArray([], "foo", 0)).not.toThrow();
expect(() => validateArray([1], "foo", 1)).not.toThrow();
});

test("array shorter than minLength: argument name", () => {
expect(() => validateArray([], "foo", 1)).toThrow(
expect.objectContaining({
name: "TypeError",
code: "ERR_INVALID_ARG_VALUE",
message: "The argument 'foo' must have a length of at least 1. Received []",
}),
);
});

test("array shorter than minLength: dotted name is a property", () => {
expect(() => validateArray([1], "options.foo", 2)).toThrow(
expect.objectContaining({
name: "TypeError",
code: "ERR_INVALID_ARG_VALUE",
message: "The property 'options.foo' must have a length of at least 2. Received [ 1 ]",
}),
);
});

test("non-array", () => {
expect(() => validateArray("x", "foo", 1)).toThrow(
expect.objectContaining({
name: "TypeError",
code: "ERR_INVALID_ARG_TYPE",
message: `The "foo" argument must be an instance of Array. Received type string ('x')`,
}),
);
});
});