Skip to content

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 ParameterDefault typeDescription
ModelanyThe type of the model the assistant helps to create.
ContainerModelanyThe type of the container model the assistant works on.

Model creation

isInvalidModel()

static isInvalidModel(potentialModel): boolean

Parameters

ParameterType
potentialModelunknown

Returns

boolean


createModel()

abstract createModel(): Model

Attempts to create a model. It fails if any of the assertions fail.

Returns

Model

See

withCreatedModelDo.

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 ParameterDescription
ReturnTypeThe type of the value returned by the closures.

Parameters

ParameterTypeDescription
validModelClosure(model) => ReturnTypeA closure that will be called with the created model if it's valid.
invalidModelClosure() => ReturnTypeA 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

ParameterType
newModelModel

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

ParameterType
containerModelContainerModel

Returns

void

Rules

addBrokenRule()

addBrokenRule(aBrokenRuleLabel): void

Adds a rule to the list of broken rules.

Parameters

ParameterType
aBrokenRuleLabelLabeledRule

Returns

void


addBrokenRules()

addBrokenRules(brokenRules): void

Adds a list of rules to the list of broken rules.

Parameters

ParameterType
brokenRulesLabeledRule[]

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

ParameterType
aRuleLabeledRule

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

ParameterType
aLabelIdstring

Returns

void


hasBrokenRule()

hasBrokenRule(aBrokenRuleLabel): boolean

Parameters

ParameterType
aBrokenRuleLabelLabeledRule

Returns

boolean


hasOnlyOneRuleBrokenIdentifiedAs()

hasOnlyOneRuleBrokenIdentifiedAs(assertionId): boolean

Parameters

ParameterType
assertionIdstring

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

ParameterType
aViewerDraftViewer<Model>

Returns

void


removeViewer()

removeViewer(aViewer): void

Removes a viewer from the list of observers.

Parameters

ParameterType
aViewerDraftViewer<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 ParameterDefault type
Modelany

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.