diff --git a/back/validation/schedule.ts b/back/validation/schedule.ts index db9d31f8..0e1df1b7 100644 --- a/back/validation/schedule.ts +++ b/back/validation/schedule.ts @@ -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), diff --git a/src/pages/crontab/modal.tsx b/src/pages/crontab/modal.tsx index dfe9fa21..3313f458 100644 --- a/src/pages/crontab/modal.tsx +++ b/src/pages/crontab/modal.tsx @@ -28,6 +28,7 @@ const CronModal = ({ const method = cron?.id ? 'put' : 'post'; const payload = { ...values, + labels: values.labels || [], schedule: scheduleType !== ScheduleType.Normal ? scheduleTypeMap[scheduleType] diff --git a/src/utils/http.tsx b/src/utils/http.tsx index e45ab9c0..904fd076 100644 --- a/src/utils/http.tsx +++ b/src/utils/http.tsx @@ -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) => ( -
- {item.message} ({item.value}) -
+ {errorDetails.map((detail, index) => ( +
{detail}
))} ) : undefined, diff --git a/src/utils/httpError.ts b/src/utils/httpError.ts new file mode 100644 index 00000000..8e292d67 --- /dev/null +++ b/src/utils/httpError.ts @@ -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)); +}; diff --git a/test/back/cron-validation.test.cjs b/test/back/cron-validation.test.cjs new file mode 100644 index 00000000..ddf5f9a1 --- /dev/null +++ b/test/back/cron-validation.test.cjs @@ -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'], + ); +}); diff --git a/test/front/http-error.test.cjs b/test/front/http-error.test.cjs new file mode 100644 index 00000000..32cbd332 --- /dev/null +++ b/test/front/http-error.test.cjs @@ -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)'], + ); +});