How to pass parameter to translated validation error message - validation

In Symfony2, you can translate your validation error messages:
Validation File
# src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
properties:
name:
- NotBlank: { message: "author.name.not_blank" }
Translation File
# validators.en.yml
author.name.not_blank: Please enter an author name.
But how can I pass a parameter to the translation file, if e.g. I want to pass the required min or max length?
author.name.min_length: "Required length: %limit% characters."

What about,
Acme\BlogBundle\Entity\Author:
properties:
name:
- NotBlank: { message: "author.name.not_blank" }
- Length:
min: 3
minMessage: "author.name.min_length"
While your translation file should contain,
# validators.en.yml
author.name.not_blank: Please enter an author name.
author.name.min_length: "Required length: {{ limit }} characters."
The {{ limit }} placeholder here will then fit the min pamarater of the length constraint.

Related

Remove a specific parameter and examples from Swagger Doc

I have a swagger.yaml file having a lot of APIs. I would like to remove all the parameters with the name of user-id from all of the APIs. Also, there are examples and x-examples which are irrelevant for my use-case that I would like to be removed.
I have been experimenting with openapi-filter. However, for it to work I'll have to add a special tag to the parameters and it won't work for examples. I could also be using it incorrectly.
parameters:
- name: action-id
in: path
description: Action ID which needs to be failed
required: true
type: integer
format: int64
x-example: 1
- name: action-category-number
in: path
description: Action category number which needs to be failed
required: true
type: integer
format: int64
example: 2
- name: user-id
in: header
description: User under which the action exists
required: true
type: integer
format: int32
x-example: 1
Expected output:
parameters:
- name: action-id
in: path
description: Action ID which needs to be failed
required: true
type: integer
format: int64
- name: action-category-number
in: path
description: Action category number which needs to be failed
required: true
type: integer
format: int64

Swagger on Apiary editor renders default value 0 as null

I'm using Swagger YAML file for producing interactive documentation on apiary.io.
I've noticed a strange issue where if I specify a default value 0 for an integer, the interactive documentation doesn't render it at all and shows "null" as default value instead. If i specify a non-zero value it renders perfectly fine.
Example :
- name: pageNumber
in: query
description: The page number, starting at 0.
required: true
type: number
format: int32
default: 0
- name: pageSize
in: query
description: The page size (max number of entities that are displayed in the
response).
required: true
type: integer
format: int32
default: 20
Appends ?pageNumber=&pageSize=20 to my path whereas it should've added ?pageNumber=0&pageSize=20 instead.
When I add a non-zero as follows :
- name: pageNumber
in: query
description: The page number, starting at 0.
required: true
type: number
format: int32
default: 1
- name: pageSize
in: query
description: The page size (max number of entities that are displayed in the
response).
required: true
type: integer
format: int32
default: 20
This is rendered fine, and appends ?pageNumber=1&pageSize=20 as expected.
Helen's comment resolved my issue.
When documenting using YAML on apiary.io, for setting default/initial values in the interactive documentation, x-example needs to be used. Now, the following works as expected :
- name: pageNumber
in: query
description: The page number, starting at 0.
required: true
type: number
format: int32
x-example: 0
- name: pageSize
in: query
description: The page size (max number of entities that are displayed in the
response).
required: true
type: integer
format: int32
x-example: 20
Successfully gets rendered as ?pageNumber=0&pageSize=20

Create complex types (definitions) in Swagger [duplicate]

This question already has answers here:
Swagger: How to have a property reference a model in OpenAPI 2.0 (i.e. nest the models)?
(2 answers)
Closed 2 years ago.
I created a definition called Product and another called Text (see code).
On parameters of paths I can not use the type Text created in definitions. On the definition Product I have a property called message and I want that property to be the type Text too.
(...)
paths:
/products:
get:
summary: Product Types
description: |
Description text
parameters:
- name: latitude
in: query
description: Latitude component of location.
required: true
### The type Text was not found here
type: Text ### The type Text was not found here
(...)
definitions:
Product:
properties:
message:
### The type Text was not found here
type: Text ### Compilation Error in this line ####
name:
type: string
description: Data description.
Text:
properties:
code:
type: string
But this error occurs:
Swagger Error:
Data does not match any schemas from 'anyOf'.
How can I reference the type Text on the type Product?
Please use $ref instead. Here is an example
type: object
required:
- name
properties:
name:
type: string
address:
$ref: '#/definitions/Address'
age:
type: integer
format: int32
minimum: 0
Ref: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#simple-model

How to use symfony callback constraint with validation group in yaml?

I'm using Symfony 2.6 and I'm following these tutorials how to use the validation callback constraint:
http://symfony.com/blog/new-in-symfony-2-4-a-better-callback-constraint
http://symfony.com/doc/current/reference/constraints/Callback.html#external-callbacks-and-closures
To invoke an external validation call I'm trying to use following yaml configuration:
App\APIBundle\Entity\Order:
properties:
id:
- Type:
type: integer
message: "Der Wert {{ value }} ist kein gültiger {{ type }}."
amount:
- Type:
type: integer
message: "Der Wert {{ value }} ist kein gültiger {{ type }}."
groups: [ "AppOrder", "AppOrderbasket" ]
- Callback: [App\APIBundle\Validator\Validator, validate]
groups: [ "AppOrder", "AppOrderbasket" ]
I run into following problems when trying to validate the amount property with an external callback validation class:
The function "validate" within the validation class App\APIBundle\Validator\Validator doesn't get invoked at all. I've tried to add validation groups by adding the "groups" property to the callback constraint. This seems not to be valid as i get this warning (Warning: trim() expects parameter 1 to be string, array given);
If I remove the "groups" property the warning dissapears but the validator is still not invoked.
Any ideas?
Thanks in advance
ninsky
You are now mixing the default option syntax with the normal syntax. That doesn't work.
If you only need to specify the default option (which is the callback option in case of the Callback constraint), you can use Callback: [App\APIBundle\Validator\Validator, validate]. However, if you have to define 2 options (in your case callback and groups), you have to use the normal syntax:
- Callback:
callback: [App\APIBundle\Validator\Validator, validate]
groups: [AppOrder, AppOrderbasket]

Symfony-2 gives more than one validation error message

My validation.yml is given:
task:
- Email:
message: The email "{{ value }}" is not a valid email.
- MinLength: { limit: 50, message: You must be 50 or under to enter. }
My issue is that if I give "wrong-email" in the task-field it gives two error messages:
The email "wrong-email" is not a valid email.
You must be 50 or under to enter.
Actually, I want to show only one error-message at a time.
That means it should check for the validation "MinLength" only if it is a valid email.
Validation sequencing can be done using group sequences. I fixed group sequences for the YAML driver only today, so you might need to wait for the next release of the 2.0 or master branch.
MyEntity:
group_sequence: [MyEntity, Extra]
properties:
task:
- Email: { message: ... }
- MinLength { limit: 50, message: ..., groups: Extra }
Now the constraints in group "Extra" will only be validated if all constraints in group "MyEntity" (i.e. the default group) succeed.

Resources