#[RPC\Assertions]

Last modified by Ashterix on 2024/05/19 21:22

Summary

ClassnameAssertions
NamespaceUfo\RpcObject\RPC
Targetparameter
Arguments:
$assertionsCollection of Symfony Constraint
typearray
optionalfalse

Input Parameter Validation

Instead of verifying whether the parameters entering your API method comply, you can utilize the powerful mechanism of Symfony Validation, specifically, you can specify requirements for each parameter and the RPC API Server will validate the input data. If the data does not comply, it will return an error to the client (learn more about error handling).

To take advantage of this feature, you must add the attribute #[RPC\Assertions] to the parameter you want to validate. This attribute acts as a container adapter that communicates your expectations about the parameter’s content to the RPC server.

RPC\Assertions accepts absolutely all classes that inherit from Symfony Constraint, including your own implementations. List of available Symfony constraints.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
namespace App\Api\Procedures;

use Symfony\Component\Validator\Constraints as Assert;
use Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService;
use Ufo\RpcObject\RPC;

class ExampleApi implements IRpcService
{
   public function sendEmail(
       #[RPC\Assertions([
           new Assert\Email(),
        ])]
        string $email,
       #[RPC\Assertions([
           new Assert\NotBlank(),
           new Assert\Length(max: 100),
        ])]
        string $subject,
       #[RPC\Assertions([
           new Assert\NotBlank(),
           new Assert\Length(min: 10),
        ])]
        string $text
    ): bool
    {
       // some logic send email
       return true;
    }
}