Created
August 28, 2021 19:47
-
-
Save buildmotion/0e08c5b87688b3e91474eb0b4df574ae to your computer and use it in GitHub Desktop.
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
export class ValidationContext implements IValidationContext { | |
state: ValidationContextState = ValidationContextState.NotEvaluated; | |
results: Array<RuleResult> = new Array<RuleResult>(); | |
rules: Array<RulePolicy> = new Array<RulePolicy>(); | |
source: string; | |
/** | |
* Use this method to add a new rule to the ValidationContext. | |
*/ | |
addRule(rule: RulePolicy) { | |
if (this.source) { | |
rule.source = this.source; | |
} | |
this.rules.push(rule); | |
return this; | |
} | |
/** | |
* Use this method to execute the rules added to the [ValidationContext]. | |
*/ | |
renderRules(): ValidationContextBase { | |
this.results = new Array<RuleResult>(); | |
if (this.rules && this.rules.length < 1) { | |
return this; | |
} | |
this.rules.sort(r => r.priority).forEach(r => this.results.push(r.execute())); | |
return this; | |
} | |
/** | |
* Use to determine if the validation context has any rule violations. | |
*/ | |
hasRuleViolations(): boolean { | |
var hasViolations = false; | |
if (this.rules && this.rules.filter(r => r.isValid === false)) { | |
hasViolations = true; | |
} | |
return hasViolations; | |
} | |
/** | |
* *Use to indicate if the validation context is valid - no rule violations. | |
* @returns {}: returns a boolean. | |
*/ | |
get isValid(): boolean { | |
var isRuleValid: boolean = true; | |
if (this.rules) { | |
var invalidRulesCount = this.rules.filter(r => r.isValid === false).length; | |
if (invalidRulesCount > 0) { | |
isRuleValid = false; | |
} | |
} | |
return isRuleValid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment