mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 20:58:26 +00:00
refactor(gateway): share server-method param validation
This commit is contained in:
27
src/gateway/server-methods/validation.ts
Normal file
27
src/gateway/server-methods/validation.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { ErrorObject } from "ajv";
|
||||
import type { RespondFn } from "./types.js";
|
||||
import { ErrorCodes, errorShape, formatValidationErrors } from "../protocol/index.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;
|
||||
}
|
||||
Reference in New Issue
Block a user