mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:34:31 +00:00
fix(gateway): improve validation errors (#1347)
Thanks @vignesh07. Co-authored-by: Vignesh <vignesh07@users.noreply.github.com>
This commit is contained in:
65
src/gateway/protocol/index.test.ts
Normal file
65
src/gateway/protocol/index.test.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { ErrorObject } from "ajv";
|
||||
|
||||
import { formatValidationErrors } from "./index.js";
|
||||
|
||||
const makeError = (overrides: Partial<ErrorObject>): ErrorObject => ({
|
||||
keyword: "type",
|
||||
instancePath: "",
|
||||
schemaPath: "#/",
|
||||
params: {},
|
||||
message: "validation error",
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe("formatValidationErrors", () => {
|
||||
it("returns unknown validation error when missing errors", () => {
|
||||
expect(formatValidationErrors(undefined)).toBe("unknown validation error");
|
||||
expect(formatValidationErrors(null)).toBe("unknown validation error");
|
||||
});
|
||||
|
||||
it("returns unknown validation error when errors list is empty", () => {
|
||||
expect(formatValidationErrors([])).toBe("unknown validation error");
|
||||
});
|
||||
|
||||
it("formats additionalProperties at root", () => {
|
||||
const err = makeError({
|
||||
keyword: "additionalProperties",
|
||||
params: { additionalProperty: "token" },
|
||||
});
|
||||
|
||||
expect(formatValidationErrors([err])).toBe("at root: unexpected property 'token'");
|
||||
});
|
||||
|
||||
it("formats additionalProperties with instancePath", () => {
|
||||
const err = makeError({
|
||||
keyword: "additionalProperties",
|
||||
instancePath: "/auth",
|
||||
params: { additionalProperty: "token" },
|
||||
});
|
||||
|
||||
expect(formatValidationErrors([err])).toBe("at /auth: unexpected property 'token'");
|
||||
});
|
||||
|
||||
it("formats message with path for other errors", () => {
|
||||
const err = makeError({
|
||||
keyword: "required",
|
||||
instancePath: "/auth",
|
||||
message: "must have required property 'token'",
|
||||
});
|
||||
|
||||
expect(formatValidationErrors([err])).toBe("at /auth: must have required property 'token'");
|
||||
});
|
||||
|
||||
it("de-dupes repeated entries", () => {
|
||||
const err = makeError({
|
||||
keyword: "required",
|
||||
instancePath: "/auth",
|
||||
message: "must have required property 'token'",
|
||||
});
|
||||
|
||||
expect(formatValidationErrors([err, err])).toBe(
|
||||
"at /auth: must have required property 'token'",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user