mirror of
https://github.com/whyour/qinglong.git
synced 2026-08-25 05:56:56 +08:00
fix: show cron validation field errors
This commit is contained in:
@@ -44,7 +44,7 @@ export const commonCronSchema = {
|
||||
name: Joi.string().optional(),
|
||||
command: Joi.string().required(),
|
||||
schedule: scheduleSchema,
|
||||
labels: Joi.array().optional(),
|
||||
labels: Joi.array().optional().allow(null),
|
||||
sub_id: Joi.number().optional().allow(null),
|
||||
extra_schedules: Joi.array().optional().allow(null),
|
||||
task_before: Joi.string().optional().allow('').allow(null),
|
||||
|
||||
@@ -28,6 +28,7 @@ const CronModal = ({
|
||||
const method = cron?.id ? 'put' : 'post';
|
||||
const payload = {
|
||||
...values,
|
||||
labels: values.labels || [],
|
||||
schedule:
|
||||
scheduleType !== ScheduleType.Normal
|
||||
? scheduleTypeMap[scheduleType]
|
||||
|
||||
+6
-6
@@ -9,8 +9,9 @@ import axios, {
|
||||
AxiosResponse,
|
||||
InternalAxiosRequestConfig,
|
||||
} from 'axios';
|
||||
import { getErrorDetails, ValidationErrorResponse } from './httpError';
|
||||
|
||||
export interface IResponseData {
|
||||
export interface IResponseData extends ValidationErrorResponse {
|
||||
code?: number;
|
||||
data?: any;
|
||||
message?: string;
|
||||
@@ -43,6 +44,7 @@ const errorHandler = function (
|
||||
const msg = error.response.data
|
||||
? error.response.data.message || error.message
|
||||
: error.response.statusText;
|
||||
const errorDetails = getErrorDetails(error.response.data);
|
||||
const responseStatus = error.response.status;
|
||||
if ([502, 504].includes(responseStatus)) {
|
||||
history.push('/error');
|
||||
@@ -60,12 +62,10 @@ const errorHandler = function (
|
||||
msg &&
|
||||
notification.error({
|
||||
message: msg,
|
||||
description: error.response?.data?.errors ? (
|
||||
description: errorDetails.length ? (
|
||||
<>
|
||||
{error.response?.data?.errors?.map((item: any) => (
|
||||
<div>
|
||||
{item.message} ({item.value})
|
||||
</div>
|
||||
{errorDetails.map((detail, index) => (
|
||||
<div key={`${index}-${detail}`}>{detail}</div>
|
||||
))}
|
||||
</>
|
||||
) : undefined,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
export interface ValidationErrorResponse {
|
||||
errors?: Array<{ message?: string; value?: unknown }>;
|
||||
validation?: Record<
|
||||
string,
|
||||
{ source?: string; keys?: string[]; message?: string }
|
||||
>;
|
||||
}
|
||||
|
||||
export const getErrorDetails = (data?: ValidationErrorResponse) => {
|
||||
const details =
|
||||
data?.errors?.map((item) =>
|
||||
item.value === undefined
|
||||
? item.message
|
||||
: `${item.message} (${String(item.value)})`,
|
||||
) || [];
|
||||
|
||||
Object.values(data?.validation || {}).forEach((validation) => {
|
||||
if (validation.keys?.length) {
|
||||
validation.keys.forEach((key) => {
|
||||
details.push(
|
||||
validation.message ? `${key}: ${validation.message}` : key,
|
||||
);
|
||||
});
|
||||
} else if (validation.message) {
|
||||
details.push(validation.message);
|
||||
}
|
||||
});
|
||||
|
||||
return details.filter((detail): detail is string => Boolean(detail));
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const test = require('node:test');
|
||||
|
||||
const configPath = require.resolve('../../back/config');
|
||||
require.cache[configPath] = {
|
||||
id: configPath,
|
||||
filename: configPath,
|
||||
loaded: true,
|
||||
exports: { __esModule: true, default: { logPath: '/ql/data/log' } },
|
||||
children: [],
|
||||
paths: [],
|
||||
};
|
||||
|
||||
const { commonCronSchema } = require('../../back/validation/schedule');
|
||||
const { Joi } = require('celebrate');
|
||||
|
||||
const schema = Joi.object(commonCronSchema);
|
||||
|
||||
test('cron validation accepts leading-zero schedules and legacy null labels', () => {
|
||||
const result = schema.validate({
|
||||
name: 'legacy cron',
|
||||
command: 'task legacy.js',
|
||||
schedule: '01 7 * * *',
|
||||
labels: null,
|
||||
});
|
||||
|
||||
assert.equal(result.error, undefined);
|
||||
});
|
||||
|
||||
test('cron validation identifies invalid fields', () => {
|
||||
const result = schema.validate(
|
||||
{
|
||||
name: 'invalid cron',
|
||||
command: 'task invalid.js',
|
||||
schedule: '01 7 * * *',
|
||||
allow_multiple_instances: '',
|
||||
},
|
||||
{ abortEarly: false },
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
[...new Set(result.error.details.map((detail) => detail.path.join('.')))],
|
||||
['allow_multiple_instances'],
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const test = require('node:test');
|
||||
|
||||
const { getErrorDetails } = require('../../src/utils/httpError');
|
||||
|
||||
test('validation errors include the failing field names', () => {
|
||||
const details = getErrorDetails({
|
||||
message: 'Validation failed',
|
||||
validation: {
|
||||
body: {
|
||||
source: 'body',
|
||||
keys: ['labels', 'allow_multiple_instances'],
|
||||
message: 'request body contains invalid values',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(details, [
|
||||
'labels: request body contains invalid values',
|
||||
'allow_multiple_instances: request body contains invalid values',
|
||||
]);
|
||||
});
|
||||
|
||||
test('existing API error details remain visible', () => {
|
||||
assert.deepEqual(
|
||||
getErrorDetails({ errors: [{ message: 'duplicate value', value: 'foo' }] }),
|
||||
['duplicate value (foo)'],
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user