mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Use formatErrors
from utils instead of the default io-ts PathReporter for a concise report of schema validation errors. (#124555)
This commit is contained in:
parent
bb4509cf3b
commit
af9d3a1602
2 changed files with 6 additions and 36 deletions
|
@ -5,41 +5,11 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { PathReporter } from 'io-ts/lib/PathReporter';
|
||||
import { isRight } from 'fp-ts/lib/Either';
|
||||
import { formatErrors } from '@kbn/securitysolution-io-ts-utils';
|
||||
import { HttpFetchQuery, HttpSetup } from 'src/core/public';
|
||||
import * as t from 'io-ts';
|
||||
import { FETCH_STATUS, AddInspectorRequest } from '../../../../observability/public';
|
||||
|
||||
function isObject(value: unknown) {
|
||||
const type = typeof value;
|
||||
return value != null && (type === 'object' || type === 'function');
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use packages/kbn-securitysolution-io-ts-utils/src/format_errors/index.ts
|
||||
*/
|
||||
export const formatErrors = (errors: t.Errors): string[] => {
|
||||
return errors.map((error) => {
|
||||
if (error.message != null) {
|
||||
return error.message;
|
||||
} else {
|
||||
const keyContext = error.context
|
||||
.filter(
|
||||
(entry) => entry.key != null && !Number.isInteger(+entry.key) && entry.key.trim() !== ''
|
||||
)
|
||||
.map((entry) => entry.key)
|
||||
.join('.');
|
||||
|
||||
const nameContext = error.context.find((entry) => entry.type?.name?.length > 0);
|
||||
const suppliedValue =
|
||||
keyContext !== '' ? keyContext : nameContext != null ? nameContext.type.name : '';
|
||||
const value = isObject(error.value) ? JSON.stringify(error.value) : error.value;
|
||||
return `Invalid value "${value}" supplied to "${suppliedValue}"`;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
class ApiService {
|
||||
private static instance: ApiService;
|
||||
private _http!: HttpSetup;
|
||||
|
@ -118,7 +88,7 @@ class ApiService {
|
|||
} else {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
`API ${apiUrl} is not returning expected response, ${PathReporter.report(decoded)}`
|
||||
`API ${apiUrl} is not returning expected response, ${formatErrors(decoded.left)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -138,7 +108,7 @@ class ApiService {
|
|||
} else {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
`API ${apiUrl} is not returning expected response, ${PathReporter.report(decoded)}`
|
||||
`API ${apiUrl} is not returning expected response, ${formatErrors(decoded.left)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
import { isLeft } from 'fp-ts/lib/Either';
|
||||
import { PathReporter } from 'io-ts/lib/PathReporter';
|
||||
import { formatErrors } from '@kbn/securitysolution-io-ts-utils';
|
||||
|
||||
import {
|
||||
BrowserFieldsCodec,
|
||||
|
@ -49,7 +49,7 @@ export function validateMonitor(monitorFields: MonitorFields): {
|
|||
return {
|
||||
valid: false,
|
||||
reason: `Monitor type is invalid`,
|
||||
details: PathReporter.report(decodedType).join(' | '),
|
||||
details: formatErrors(decodedType.left).join(' | '),
|
||||
payload: monitorFields,
|
||||
};
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ export function validateMonitor(monitorFields: MonitorFields): {
|
|||
return {
|
||||
valid: false,
|
||||
reason: `Monitor is not a valid monitor of type ${monitorType}`,
|
||||
details: PathReporter.report(decodedMonitor).join(' | '),
|
||||
details: formatErrors(decodedMonitor.left).join(' | '),
|
||||
payload: monitorFields,
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue