tbaronnat/api-validation-rules-bundle

System to get all validation rules by validation groups for each api endpoint

1.0.0 2025-01-28 20:33 UTC

This package is not auto-updated.

Last update: 2025-04-09 12:27:44 UTC


README

Description

This project provides a system to get all validation rules by validation groups for each API endpoint. It includes methods to retrieve validation rules for all API routes.

Features

  • Retrieve validation rules for all API routes.

Installation

Install dependencies using Composer:

   composer require tbaronnat/api-validation-rules-bundle

Usage

Retrieve Validation Rules for API Routes

To retrieve validation rules for all API routes, use the getValidationRules method:

$validationRules = $mainValidationRulesProvider->getValidationRules();

Override method isValidEntityGroup on EntityGroupApiProvider

Actually, the method isValidEntityGroup(string $group): bool is used to check if the group is valid for api validation. 
You can override this method to add your own logic.
Group is valid if the name contains "api:[...]"

Override method isValidApiRoute on RouteApiProvider

Actually, the method isValidApiRoute(string $route): bool is used to check if the route is valid for api validation. 
You can override this method to add your own logic.
Route is valid if the name contains "api_"

Override method getValidationGroupsForRoute on RouteValidationGroupsProvider

Actually, the method getValidationGroupsForRoute(string $route): array is used to get the validation groups for a route. 
You can override this method to add your own logic (for example, get validation groups from annotation if you use ApiPlatform).)
Validation groups are retrieved from configuration option "validation_groups" for the route.
Exemple of configuration:

```yaml
routes:
    api_users_create:
        path: api/user/create
        methods: [GET, POST]
        options:
            validation_groups: ["api:user_validation:create"]

```

Return example

```json
{
  "api_users_create": {
    "api:user_validation:create": {
      "lastName": [
        {
          "type": "notblank",
          "payload": null,
          "groups": ["api:user_validation:create"],
          "message": "The name should not be blank.",
          "allowNull": false,
          "normalizer": null
        }
      ]
    }
  }
}
```

License

This project is licensed under the MIT License.