Files
openclaw/src/gateway/server-methods/validation.ts
2026-02-18 01:34:35 +00:00

28 lines
658 B
TypeScript

import type { ErrorObject } from "ajv";
import { ErrorCodes, errorShape, formatValidationErrors } from "../protocol/index.js";
import type { RespondFn } from "./types.js";
export type Validator<T> = ((params: unknown) => params is T) & {
errors?: ErrorObject[] | null;
};
export function assertValidParams<T>(
params: unknown,
validate: Validator<T>,
method: string,
respond: RespondFn,
): params is T {
if (validate(params)) {
return true;
}
respond(
false,
undefined,
errorShape(
ErrorCodes.INVALID_REQUEST,
`invalid ${method} params: ${formatValidationErrors(validate.errors)}`,
),
);
return false;
}