self-assert / FieldDraftAssistant
Class: FieldDraftAssistant<ContainerModel, Model>
An assistant designed to manage a single field or a simple piece of data within a larger form or model.
Extends
DraftAssistant
<Model
,ContainerModel
>
Type Parameters
Type Parameter | Default type |
---|---|
ContainerModel | any |
Model extends string | string |
Model creation
isInvalidModel()
static
isInvalidModel(potentialModel
):boolean
Parameters
Parameter | Type |
---|---|
potentialModel | unknown |
Returns
boolean
Inherited from
createModel()
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
Overrides
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.
Inherited from
DraftAssistant
.withCreatedModelDo
getModel()
getModel():
Model
Returns
Model
Inherited from
setModel()
setModel(
newModel
):void
Parameters
Parameter | Type |
---|---|
newModel | Model |
Returns
void
Inherited from
resetModel()
resetModel():
void
Resets the model to its initial value.
Returns
void
Inherited from
setModelFrom()
setModelFrom(
containerModel
):void
Sets the model from its container.
Parameters
Parameter | Type |
---|---|
containerModel | ContainerModel |
Returns
void
Inherited from
Rules
addBrokenRule()
addBrokenRule(
aBrokenRuleLabel
):void
Adds a rule to the list of broken rules.
Parameters
Parameter | Type |
---|---|
aBrokenRuleLabel | LabeledRule |
Returns
void
Inherited from
addBrokenRules()
addBrokenRules(
brokenRules
):void
Adds a list of rules to the list of broken rules.
Parameters
Parameter | Type |
---|---|
brokenRules | LabeledRule [] |
Returns
void
Inherited from
hasBrokenRules()
hasBrokenRules():
boolean
Returns
boolean
true
if the list of broken rules is not empty
Inherited from
doesNotHaveBrokenRules()
doesNotHaveBrokenRules():
boolean
Opposite of hasBrokenRules.
Returns
boolean
Inherited from
DraftAssistant
.doesNotHaveBrokenRules
brokenRulesDescriptions()
brokenRulesDescriptions():
string
[]
Returns
string
[]
The descriptions of the broken rules
Inherited from
DraftAssistant
.brokenRulesDescriptions
handles()
handles(
aRule
):boolean
Parameters
Parameter | Type |
---|---|
aRule | LabeledRule |
Returns
boolean
true
if this assistant handles the given Assertion
.
Inherited from
addLabelId()
addLabelId(
aLabelId
):void
Adds an assertion id to the list of handled assertions.
Parameters
Parameter | Type |
---|---|
aLabelId | string |
Returns
void
Inherited from
hasBrokenRule()
hasBrokenRule(
aBrokenRuleLabel
):boolean
Parameters
Parameter | Type |
---|---|
aBrokenRuleLabel | LabeledRule |
Returns
boolean
Inherited from
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.
Inherited from
DraftAssistant
.hasOnlyOneRuleBrokenIdentifiedAs
removeBrokenRules()
removeBrokenRules():
void
Returns
void
Inherited from
DraftAssistant
.removeBrokenRules
Viewers
accept()
accept(
aViewer
):void
Adds a viewer to the list of observers.
Parameters
Parameter | Type |
---|---|
aViewer | DraftViewer <Model > |
Returns
void
Inherited from
removeViewer()
removeViewer(
aViewer
):void
Removes a viewer from the list of observers.
Parameters
Parameter | Type |
---|---|
aViewer | DraftViewer <never > |
Returns
void
Inherited from
numberOfViewers()
numberOfViewers():
number
Returns
number
The number of viewers currently observing the assistant.
Inherited from
DraftAssistant
.numberOfViewers
Others
handling()
static
handling<ContainerModel
,Model
>(assertionId
,modelFromContainer
,initialModel
):FieldDraftAssistant
<ContainerModel
,Model
>
Type Parameters
Type Parameter | Default type |
---|---|
ContainerModel | any |
Model extends string | string |
Parameters
Parameter | Type | Default value |
---|---|---|
assertionId | string | undefined |
modelFromContainer | ModelFromContainer <Model , ContainerModel > | undefined |
initialModel | string | "" |
Returns
FieldDraftAssistant
<ContainerModel
, Model
>
handlingAll()
static
handlingAll<ContainerModel
,Model
>(assertionIds
,modelFromContainer
,initialModel
):FieldDraftAssistant
<ContainerModel
,Model
>
Type Parameters
Type Parameter | Default type |
---|---|
ContainerModel | any |
Model extends string | string |
Parameters
Parameter | Type | Default value |
---|---|---|
assertionIds | string [] | undefined |
modelFromContainer | ModelFromContainer <Model , ContainerModel > | undefined |
initialModel | string | "" |
Returns
FieldDraftAssistant
<ContainerModel
, Model
>
requiring()
static
requiring<ContainerModel
,Model
>(assertion
,modelFromContainer
,initialModel
):FieldDraftAssistant
<ContainerModel
,Model
>
Type Parameters
Type Parameter | Default type |
---|---|
ContainerModel | any |
Model extends string | string |
Parameters
Parameter | Type | Default value |
---|---|---|
assertion | CollectableRule <void | Model , void > | undefined |
modelFromContainer | ModelFromContainer <Model , ContainerModel > | undefined |
initialModel | string | "" |
Returns
FieldDraftAssistant
<ContainerModel
, Model
>
requiringAll()
static
requiringAll<ContainerModel
,Model
>(assertions
,modelFromContainer
,initialModel
):FieldDraftAssistant
<ContainerModel
,Model
>
Type Parameters
Type Parameter | Default type |
---|---|
ContainerModel | any |
Model extends string | string |
Parameters
Parameter | Type | Default value |
---|---|---|
assertions | CollectableRule <void | Model , void >[] | undefined |
modelFromContainer | ModelFromContainer <Model , ContainerModel > | undefined |
initialModel | string | "" |
Returns
FieldDraftAssistant
<ContainerModel
, Model
>
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.
Inherited from
DraftAssistant
.topLevelModelFromContainer
review()
review():
void
Checks if the current draft verifies all assertions. If not, it adds them to the list of failed assertions.
Returns
void