@validator
The @validator
decorator is used to mark the validation method in a block. That method is invoked when the block needs to be validated. It allows synchronous and asynchronous validation. When synchronous validation is used the decorator method should return true
when the validation succeeds or false
when the validation fails. When asynchronous validation is used, the method should return the callback
property of the props
argument. When the validation completes, the result should be passed to callback.return
(see example below).
It can be used to decorate a method in a NodeBlock
or HeadlessBlock
derived class.
Read more about validation in the Block validation guide.
Decorator type
Method ℹ️
Applies to
Decorator signature
@validator
Decorated method signature
(props?: {
callback: Callback<boolean>;
current: "unknown" | "fail" | "pass";
}): boolean | Callback<boolean>;
Decorated method parameters
Name | Type | Optional | Description |
---|---|---|---|
props | object | Yes | Specifies the properties for the validator method. Contains the following properties: - callback : Reference to the Callback instance. Pass this reference as method return value to enable the asynchronous validation callback;- current : Contains the current validation state. |
Return value
Returns true
if the validation passes, false
when the validation fails, or props.callback
to indicate asynchronous operation.
Example
Synchronous example:
import { tripetto, validator, NodeBlock, Callback } from "@tripetto/runner";
@tripetto({
type: "node",
identifier: "example-block",
})
class ExampleBlock extends NodeBlock {
@validator
validate(): boolean {
// Indicate the validation passes
return true;
}
}
Asynchronous example:
import { tripetto, validator, NodeBlock, Callback } from "@tripetto/runner";
@tripetto({
type: "node",
identifier: "example-block",
})
class ExampleBlock extends NodeBlock {
@validator
validate({ callback }: { callback: Callback<boolean> }): Callback<boolean> {
setTimeout(() => {
// After a while, indicate the validation fails
callback.return(false);
}, 1000);
return callback;
}
}