generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Change policy validation result from string to object (#1450)
- Loading branch information
1 parent
110b992
commit 07dd94c
Showing
10 changed files
with
247 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum ValidationResultCode { | ||
PermitTypeUnknown = 'permit-type-unknown', | ||
FieldValidationError = 'field-validation-error', | ||
ConfigurationInvalid = 'configuration-invalid', | ||
GeneralResult = 'general-result', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,28 @@ | ||
import { EngineResult } from 'json-rules-engine'; | ||
import { ValidationResultType } from './enum/validation-result-type.enum'; | ||
|
||
/** Represents the result of a permit application validation against policy */ | ||
class ValidationResult { | ||
/** Array of policy violations found during validation. */ | ||
violations: Array<string> = []; | ||
/** | ||
* Represents a single validation result when a permit application is | ||
* validated against policy. A single validation run can result in multiple | ||
* validation result messages. | ||
*/ | ||
export class ValidationResult { | ||
/** | ||
* Array of additional requirements found during validation. For example, | ||
* a permit may require that other additional permits be applied for in | ||
* order to be legal. | ||
* Creates a new ValidationResult from the supplied type, code, and message. | ||
* | ||
* @param type type of result, one of the ValidationResultType enum | ||
* @param code code for identifying common categories of results | ||
* @param message text message describing the result | ||
*/ | ||
requirements: Array<string> = []; | ||
/** Array of policy warnings found during validation. */ | ||
warnings: Array<string> = []; | ||
/** | ||
* Array of policy messages found during validation. These should be | ||
* considered informational only and do not impact the validity of the | ||
* permit. | ||
*/ | ||
information: Array<string> = []; | ||
|
||
/** | ||
* Creates a new ValidationResult from the json-rules-engine result. | ||
* Populates the violations, requirements, warnings, and messages arrays | ||
* based on the events in the json-rules-engine result. | ||
* @param engineResult json-rules-engine run result. | ||
*/ | ||
constructor(engineResult?: EngineResult) { | ||
if (engineResult) { | ||
engineResult.events.forEach((e) => { | ||
let message: string; | ||
if (e.params && e.params.message) { | ||
message = e.params.message; | ||
} else { | ||
// All events should have a message, if there is no message | ||
// push the entire params in (likely error scenario). | ||
message = `Unknown message: params=${JSON.stringify(e.params)}`; | ||
} | ||
|
||
switch (e.type) { | ||
case ValidationResultType.Violation: | ||
this.violations.push(message); | ||
break; | ||
case ValidationResultType.Requirement: | ||
this.requirements.push(message); | ||
break; | ||
case ValidationResultType.Warning: | ||
this.warnings.push(message); | ||
break; | ||
case ValidationResultType.Information: | ||
this.information.push(message); | ||
break; | ||
default: | ||
// Treat any unknown validation event as a violation since | ||
// it is an unexpected scenario. | ||
console.log('Unknown validation event encountered'); | ||
this.violations.push(message); | ||
} | ||
}); | ||
} | ||
constructor(type: string, code: string, message: string) { | ||
this.type = type; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
type: string; | ||
code: string; | ||
message: string; | ||
fieldReference?: string; | ||
} | ||
|
||
export default ValidationResult; | ||
export default ValidationResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { EngineResult } from 'json-rules-engine'; | ||
import { ValidationResultType } from './enum/validation-result-type.enum'; | ||
import { ValidationResultCode } from './enum/validation-result-code.enum'; | ||
import { ValidationResult } from './validation-result'; | ||
|
||
/** Represents the results of a permit application validation against policy */ | ||
class ValidationResults { | ||
/** Array of policy violations found during validation. */ | ||
violations: Array<ValidationResult> = []; | ||
/** | ||
* Array of additional requirements found during validation. For example, | ||
* a permit may require that other additional permits be applied for in | ||
* order to be legal. | ||
*/ | ||
requirements: Array<ValidationResult> = []; | ||
/** Array of policy warnings found during validation. */ | ||
warnings: Array<ValidationResult> = []; | ||
/** | ||
* Array of policy messages found during validation. These should be | ||
* considered informational only and do not impact the validity of the | ||
* permit. | ||
*/ | ||
information: Array<ValidationResult> = []; | ||
|
||
/** | ||
* Creates a new ValidationResults from the json-rules-engine result. | ||
* Populates the violations, requirements, warnings, and messages arrays | ||
* based on the events in the json-rules-engine result. | ||
* @param engineResult json-rules-engine run result. | ||
*/ | ||
constructor(engineResult?: EngineResult) { | ||
if (engineResult) { | ||
engineResult.events.forEach((e) => { | ||
let message: string = e.params?.message ?? `Unknown message: params=${JSON.stringify(e.params)}`; | ||
let code: string = e.params?.code ?? ValidationResultCode.GeneralResult; | ||
let type: string = e.type ?? ValidationResultType.Violation.toString(); | ||
|
||
let result: ValidationResult = new ValidationResult(type, code, message); | ||
result.fieldReference = e.params?.fieldReference; | ||
|
||
switch (type) { | ||
case ValidationResultType.Violation: | ||
this.violations.push(result); | ||
break; | ||
case ValidationResultType.Requirement: | ||
this.requirements.push(result); | ||
break; | ||
case ValidationResultType.Warning: | ||
this.warnings.push(result); | ||
break; | ||
case ValidationResultType.Information: | ||
this.information.push(result); | ||
break; | ||
default: | ||
// Treat any unknown validation event as a violation since | ||
// it is an unexpected scenario. | ||
console.log('Unknown validation event encountered'); | ||
this.violations.push(new ValidationResult( | ||
ValidationResultType.Violation, | ||
ValidationResultCode.GeneralResult, | ||
'Unknown validation event encountered' | ||
)); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default ValidationResults; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.