-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
474 additions
and
172 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,60 @@ | ||
import { | ||
Rule, | ||
SubRule, | ||
Operator, | ||
Condition, | ||
Constraint, | ||
ConditionType, | ||
} from "../types/rule"; | ||
|
||
export abstract class BaseBuilder { | ||
/** Stores the rule being constructed */ | ||
#rule: Rule = { conditions: [] }; | ||
|
||
/** | ||
* Adds a node (in the root) to the rule being constructed | ||
* @param node The node to add to the rule | ||
*/ | ||
add(node: Condition): BaseBuilder { | ||
(this.#rule.conditions as Condition[]).push(node); | ||
return this; | ||
} | ||
|
||
/** | ||
* Creates a new condition node | ||
* @param type The type of condition | ||
* @param nodes Any child nodes of the condition | ||
* @param result The result of the condition node (for granular rules) | ||
* @param subRule A sub-rule to apply to the condition | ||
*/ | ||
condition( | ||
type: ConditionType, | ||
nodes: Condition[ConditionType], | ||
result?: Condition["result"], | ||
subRule?: SubRule | ||
): Condition { | ||
return { | ||
[type]: nodes, | ||
...(subRule ? { rule: subRule } : {}), | ||
...(result ? { result } : {}), | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a new constraint node | ||
* @param field The field to apply the constraint to | ||
* @param operator The operator to apply to the field | ||
* @param value The value to compare the field to | ||
*/ | ||
constraint( | ||
field: string, | ||
operator: Operator, | ||
value: Constraint["value"] | ||
): Constraint { | ||
return { | ||
field, | ||
operator, | ||
value, | ||
}; | ||
} | ||
} |
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,48 @@ | ||
import { Rule } from "../types/rule"; | ||
import { Validator } from "../services"; | ||
import { RuleError } from "../types/error"; | ||
import { SubRuleBuilder } from "./sub-rule-builder"; | ||
import { BaseBuilder } from "./base-builder.abstract"; | ||
|
||
export class Builder extends BaseBuilder { | ||
constructor(validator: Validator) { | ||
super(); | ||
this.#validator = validator; | ||
} | ||
|
||
/** Stores the rule being constructed */ | ||
#rule: Rule = { conditions: [] }; | ||
|
||
/** Holds a reference to the Validator class */ | ||
#validator: Validator; | ||
|
||
/** | ||
* Sets the default value of the rule being constructed | ||
* @param value The default value of the rule | ||
*/ | ||
default(value: Rule["default"]): Builder { | ||
this.#rule.default = value; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds the rule being and returns it | ||
* @param validate Whether to validate the rule before returning it | ||
* @throws Error if validation is enabled and the rule is invalid | ||
*/ | ||
build(validate?: boolean): Rule { | ||
if (!validate) return this.#rule; | ||
|
||
const validationResult = this.#validator.validate(this.#rule); | ||
if (validationResult.isValid) return this.#rule; | ||
|
||
throw new RuleError(validationResult); | ||
} | ||
|
||
/** | ||
* Creates a new sub-rule builder | ||
*/ | ||
subRule(): SubRuleBuilder { | ||
return new SubRuleBuilder(); | ||
} | ||
} |
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,3 @@ | ||
export * from "./base-builder.abstract"; | ||
export * from "./builder"; | ||
export * from "./sub-rule-builder"; |
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,16 @@ | ||
import { SubRule } from "../types/rule"; | ||
import { BaseBuilder } from "./base-builder.abstract"; | ||
|
||
export class SubRuleBuilder extends BaseBuilder { | ||
/** Stores the rule being constructed */ | ||
#rule: SubRule = { conditions: [], result: undefined }; | ||
|
||
/** | ||
* Sets the result of the subrule being constructed | ||
* @param value The default value of the rule | ||
*/ | ||
result(value: SubRule["result"]): SubRuleBuilder { | ||
this.#rule.result = value; | ||
return this; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.