Use formatErrors from utils instead of the default io-ts PathReporter for a concise report of schema validation errors. (#124555) (#124565)

(cherry picked from commit af9d3a1602)

Co-authored-by: Abdul Wahab Zahid <abdul.zahid@deliveryhero.com>
This commit is contained in:
Kibana Machine 2022-02-03 14:32:07 -05:00 committed by GitHub
parent 7730d9c18e
commit 6258b33ed4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 36 deletions

View file

@ -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)}`
);
}
}

View file

@ -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,
};
}