Modify validation to have a more user friendly API
* Adding a container type to be returned from validate function * Modifying JsonRulesEngineFactChecker to throw with error information if check addition fails on validation Signed-off-by: Jussi Hallila <jussi@hallila.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
```ts
|
||||
import { Config } from '@backstage/config';
|
||||
import { CustomErrorBase } from '@backstage/errors';
|
||||
import { DateTime } from 'luxon';
|
||||
import { Logger as Logger_2 } from 'winston';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
@@ -30,6 +31,28 @@ export type CheckResult = {
|
||||
check: CheckResponse;
|
||||
};
|
||||
|
||||
// @public
|
||||
export class CheckValidationError extends CustomErrorBase {
|
||||
constructor({
|
||||
message,
|
||||
cause,
|
||||
errors,
|
||||
}: {
|
||||
message: string;
|
||||
cause?: Error;
|
||||
errors?: any;
|
||||
});
|
||||
// (undocumented)
|
||||
errors?: any;
|
||||
}
|
||||
|
||||
// @public
|
||||
export type CheckValidationResponse = {
|
||||
valid: boolean;
|
||||
message?: string;
|
||||
errors?: any;
|
||||
};
|
||||
|
||||
// @public
|
||||
export interface FactChecker<
|
||||
CheckType extends TechInsightCheck,
|
||||
@@ -38,7 +61,7 @@ export interface FactChecker<
|
||||
addCheck(check: CheckType): Promise<CheckType>;
|
||||
getChecks(): Promise<CheckType[]>;
|
||||
runChecks(entity: string, checks: string[]): Promise<CheckResultType[]>;
|
||||
validate(check: CheckType): Promise<boolean>;
|
||||
validate(check: CheckType): Promise<CheckValidationResponse>;
|
||||
}
|
||||
|
||||
// @public
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/backend-common": "^0.9.0",
|
||||
"@backstage/errors": "^0.1.2",
|
||||
"@backstage/config": "^0.1.8",
|
||||
"@types/luxon": "^2.0.5",
|
||||
"luxon": "^2.0.2",
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
import { TechInsightsStore } from './persistence';
|
||||
import { CheckResponse, FactResponse } from './responses';
|
||||
import { CustomErrorBase } from '@backstage/errors';
|
||||
|
||||
/**
|
||||
* A factory wrapper to construct FactChecker implementations.
|
||||
@@ -82,7 +83,7 @@ export interface FactChecker<
|
||||
* @param check - The check to be validated
|
||||
* @returns - Validation result
|
||||
*/
|
||||
validate(check: CheckType): Promise<boolean>;
|
||||
validate(check: CheckType): Promise<CheckValidationResponse>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,3 +172,39 @@ export interface TechInsightCheck {
|
||||
*/
|
||||
failureMetadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation response from CheckValidator
|
||||
*
|
||||
* May contain additional data for display purposes
|
||||
* @public
|
||||
*/
|
||||
export type CheckValidationResponse = {
|
||||
valid: boolean;
|
||||
message?: string;
|
||||
errors?: any;
|
||||
};
|
||||
|
||||
/**
|
||||
* Error object for Validation Errors
|
||||
*
|
||||
* Can be used to short circuit larger execution paths instead of passing validation response around
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class CheckValidationError extends CustomErrorBase {
|
||||
errors?: any;
|
||||
|
||||
constructor({
|
||||
message,
|
||||
cause,
|
||||
errors,
|
||||
}: {
|
||||
message: string;
|
||||
cause?: Error;
|
||||
errors?: any;
|
||||
}) {
|
||||
super(message, cause);
|
||||
this.errors = errors;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user