build: 1.3.5

This commit is contained in:
手瓜一十雪 2024-05-18 20:09:33 +08:00
parent 91fc83621e
commit f4d5d417d0

View File

@ -2,20 +2,24 @@ import { ActionName, BaseCheckResult } from './types';
import { OB11Response } from './OB11Response'; import { OB11Response } from './OB11Response';
import { OB11Return } from '../types'; import { OB11Return } from '../types';
import { log, logError } from '../../common/utils/log'; import { log, logError } from '../../common/utils/log';
import Ajv from 'ajv'; import Ajv, { ErrorObject, ValidateFunction } from 'ajv';
class BaseAction<PayloadType, ReturnDataType> { class BaseAction<PayloadType, ReturnDataType> {
actionName!: ActionName; actionName!: ActionName;
private validate: any = undefined; private validate: undefined | ValidateFunction<any> = undefined;
PayloadSchema: any = undefined; PayloadSchema: any = undefined;
protected async check(payload: PayloadType): Promise<BaseCheckResult> { protected async check(payload: PayloadType): Promise<BaseCheckResult> {
if (this.PayloadSchema) { if (this.PayloadSchema) {
this.validate = new Ajv().compile(this.PayloadSchema); this.validate = new Ajv().compile(this.PayloadSchema);
} }
if (this.validate && !this.validate(payload)) { if (this.validate && !this.validate(payload)) {
const errors = this.validate.errors as ErrorObject[];
const errorMessages: string[] = errors.map((e) => {
return `Key: ${e.instancePath.split('/').slice(1).join('.')}, Message: ${e.message}`;
});
return { return {
valid: false, valid: false,
message: this.validate.errors?.map((e: { message: any; }) => e.message).join(',') as string message: errorMessages.join('\n') as string || '未知错误'
} }
} }
return { return {