Pipes

Pipes are flexible mechanisms for transforming and validating input data in an application, ensuring processing consistency and efficiency.

Pipes in ModularBr are an important feature that allows transforming and validating input data in an application built with the framework. They are used to process the arguments passed to controller route handlers before invoking the handler itself.

There are two main use cases for pipes in ModularBr:

  1. Transformation: Transformation pipes allow converting input data into the desired format. For example, you can use a pipe to convert a string into an integer or perform formatting on the data before it is processed by the route handler. This ensures that the data is in a suitable format for use within the application context.

  2. Validation: Validation pipes are used to evaluate input data and determine its validity. If the data is valid, the pipe simply passes it along to the route handler. Otherwise, the pipe can throw an exception or take custom actions, such as returning an error response. This is useful for ensuring that the input data complies with the rules defined by the application.

ModularBr provides a range of built-in pipes that can be used immediately, covering data validation, serialization/deserialization, authentication, and authorization. Additionally, you can create custom pipes, allowing you to implement specific and personalized logic according to your application requirements.

Pipes are a powerful tool for handling and validating input data in an application using ModularBr. They help improve code security, consistency, and efficiency by ensuring proper data handling and validation.

...
[Body(TPerson, TParseJsonPipe)]
[Param('id', TIsNotEmpty), Param('id', TParseIntegerPipe)]
[Query('name', TIsString), Query('name', TIsNotEmpty)]
procedure NFeServicoStatus: string;

implementation

procedure TNFeRouteHandler.NFeServicoStatus: string;
var
  LId: integer;
begin
  LId := Modular.Request.Params.Value<Integer>('id');
  ...
end;
unit person;

interface

uses
  dmfbr.decorator.include;

type
  TPerson = class
  private
    FId: integer;
    FName: string;
  public
    [IsInteger, IsNotEmpty, IsMax(1)]
    property Id: integer read FId write FId;

    [IsString, IsNotEmpty, IsLength(5, 10)]
    property Name: string read FName write FName;
  end;

implementation

end.

To use pipes in ModularBr, you need to explicitly define their usage through the Modular.UsePipes() method. This allows you to control which routes or route groups should make use of the pipes, giving you fine-grained control over their application.

By defining the usage of pipes in ModularBr, you can apply transformations to input data, such as type conversions, formatting, or specific adjustments, before they are processed by the routes. Additionally, you can perform validations on the input data, ensuring that it complies with the rules and constraints set by your application.

However, it is important to note that the use of pipes is optional and depends on the specific needs and requirements of your project. If you do not require additional transformations or validations on the input data, there is no need to define the usage of pipes.

ModularBr provides flexibility for you to adapt the usage of pipes according to the needs of each route or route group in your application. By utilizing pipes, you gain greater control and the ability to add custom logic to efficiently and consistently handle and validate input data. This flexibility empowers you to tailor the behavior of your application to suit specific requirements while maintaining code organization and readability.

uses
  dmfbr.validation.pipe;
...  

Modular.UsePipes(TValidationPipe.Create);

Last updated