build: 1.3.5
This commit is contained in:
1 parent
5a7c851da2
commit
50534f5e12
1 file changed
+7
-3
@@ -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 {
|
||||||
|
|||||||
Reference in new issue
Block a user