self-assert / DraftAssistant
Class: abstract
DraftAssistant<Model, ContainerModel>
Provides an assistant to guide the completion of a model.
A DraftAssistant
encapsulates the logic needed to:
- track the current state of a form field or group of fields,
- validate the model being built,
- handle and route failed assertions,
- notify observers (viewers) of changes or validation failures.
Assistants can be nested and composed to build complex models.
Remarks
Originally, this class was named ModelCreator
. Later, it was renamed to FormCompletionAssistant
, employing the metaphor of an assistant guiding form completion. This could have led to confusion, since the class has more use cases than just form completion.
It can, for example, be used in a backend context to validate an object before persisting it.
Extended by
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
Model | any | The type of the model the assistant helps to create. |
ContainerModel | any | The type of the container model the assistant works on. |
Model creation
isInvalidModel()
static
isInvalidModel(potentialModel
):boolean
Parameters
Parameter | Type |
---|---|
potentialModel | unknown |
Returns
boolean
createModel()
abstract
createModel():Model
Attempts to create a model. It fails if any of the assertions fail.
Returns
Model
See
Throws
RulesBroken if the model is invalid
withCreatedModelDo()
withCreatedModelDo<
ReturnType
>(validModelClosure
,invalidModelClosure
):ReturnType
Executes a closure depending on whether the model is valid or not after creating it.
Type Parameters
Type Parameter | Description |
---|---|
ReturnType | The type of the value returned by the closures. |
Parameters
Parameter | Type | Description |
---|---|---|
validModelClosure | (model ) => ReturnType | A closure that will be called with the created model if it's valid. |
invalidModelClosure | () => ReturnType | A closure that will be called if the model is invalid. |
Returns
ReturnType
The return value of the closure that was called.
getModel()
getModel():
Model
Returns
Model
setModel()
setModel(
newModel
):void
Parameters
Parameter | Type |
---|---|
newModel | Model |
Returns
void
resetModel()
resetModel():
void
Resets the model to its initial value.
Returns
void
setModelFrom()
setModelFrom(
containerModel
):void
Sets the model from its container.
Parameters
Parameter | Type |
---|---|
containerModel | ContainerModel |
Returns
void
Rules
addBrokenRule()
addBrokenRule(
aBrokenRuleLabel
):void
Adds a rule to the list of broken rules.
Parameters
Parameter | Type |
---|---|
aBrokenRuleLabel | LabeledRule |
Returns
void
addBrokenRules()
addBrokenRules(
brokenRules
):void
Adds a list of rules to the list of broken rules.
Parameters
Parameter | Type |
---|---|
brokenRules | LabeledRule [] |
Returns
void
hasBrokenRules()
hasBrokenRules():
boolean
Returns
boolean
true
if the list of broken rules is not empty
doesNotHaveBrokenRules()
doesNotHaveBrokenRules():
boolean
Opposite of hasBrokenRules.
Returns
boolean
brokenRulesDescriptions()
brokenRulesDescriptions():
string
[]
Returns
string
[]
The descriptions of the broken rules
handles()
handles(
aRule
):boolean
Parameters
Parameter | Type |
---|---|
aRule | LabeledRule |
Returns
boolean
true
if this assistant handles the given Assertion
.
addLabelId()
addLabelId(
aLabelId
):void
Adds an assertion id to the list of handled assertions.
Parameters
Parameter | Type |
---|---|
aLabelId | string |
Returns
void
hasBrokenRule()
hasBrokenRule(
aBrokenRuleLabel
):boolean
Parameters
Parameter | Type |
---|---|
aBrokenRuleLabel | LabeledRule |
Returns
boolean
hasOnlyOneRuleBrokenIdentifiedAs()
hasOnlyOneRuleBrokenIdentifiedAs(
assertionId
):boolean
Parameters
Parameter | Type |
---|---|
assertionId | string |
Returns
boolean
true
if this assistant has only one failed assertion that is identified as the given assertionId
.
Remarks
Used mostly for testing.
removeBrokenRules()
removeBrokenRules():
void
Returns
void
Viewers
accept()
accept(
aViewer
):void
Adds a viewer to the list of observers.
Parameters
Parameter | Type |
---|---|
aViewer | DraftViewer <Model > |
Returns
void
removeViewer()
removeViewer(
aViewer
):void
Removes a viewer from the list of observers.
Parameters
Parameter | Type |
---|---|
aViewer | DraftViewer <never > |
Returns
void
numberOfViewers()
numberOfViewers():
number
Returns
number
The number of viewers currently observing the assistant.
Others
topLevelModelFromContainer()
static
topLevelModelFromContainer<Model
>():ModelFromContainer
<Model
,unknown
>
Type Parameters
Type Parameter | Default type |
---|---|
Model | any |
Returns
ModelFromContainer
<Model
, unknown
>
A default model getter from a container for the top-level assistant. Since there is no container to get the model from, it throws an error.