API Validation / Serialization
Configure Serialization
You may have to enable the serializing mapping first
# app/config/config.yaml
framework:
serializer:
mapping:
paths:
- '%kernel.project_dir%/config/serialization'
Override Validation
In this example, we want to change the validation of a property (newPassword) in command HeadlessBundle\Domain\Account\Command\ResetPasswordCommand.
instead of the default Assert\Length length of 6, we want to change the length to 10.
# config/headless.yaml
opendxp_headless:
api_routes:
account_reset_password_confirm:
action_request:
validation_groups: [ 'app:api:write' ]
# config/serialization/ResetPasswordCommand.yaml
HeadlessBundle\Domain\Account\Command\ResetPasswordCommand:
attributes:
newPassword: { groups: ['app:api:write'] }
# config/validator/ResetPasswordCommand.yaml
HeadlessBundle\Domain\Account\Command\ResetPasswordCommand:
properties:
newPassword:
- NotBlank: { groups: [ 'app:api:write' ] }
- Email: { groups: [ 'app:api:write' ] }
- Length: { min: 10, groups: [ 'app:api:write' ] }
Override Serialization
In this example, we want to change the Response Entity OpenDxp\Bundle\HeadlessBundle\Domain\Document\ResponseEntity\OpenDxpDocument
to just return the id property.
# config/headless.yaml
opendxp_headless:
api_routes:
document:
action_response:
serialization_context:
groups: [ 'app:api:read' ]
# config/serialization/OpenDxpDocument.yaml
OpenDxp\Bundle\HeadlessBundle\Domain\Document\ResponseEntity\OpenDxpDocument:
attributes:
id: { groups: ['app:api:read'] }