Skip to content

Feat: Add validation groups on operations #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2018
Merged

Feat: Add validation groups on operations #516

merged 1 commit into from
Jun 29, 2018

Conversation

toofff
Copy link
Contributor

@toofff toofff commented Jun 27, 2018

No description provided.

@@ -167,6 +167,70 @@ Of course, you can use XML or YAML configuration format instead of annotations i
You may also pass in a [group sequence](http://symfony.com/doc/current/validation/sequence_provider.html) in place of
the array of group names.

## Using Validation Groups on operations

There are [5 types of operations](operations.md) in API Platform and you can have different validation for each.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reword There are [5 types of operations](operations.md). It's not exact: there are 2 builtin collection operations, and 4 item operations (PATCH is JSONAPI specific), but API Platform supports any type and any number of operations.

/**
* @ApiResource(
* collectionOperations={
* "get"={"method"="GET"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can remove the explicit method declaration to be clearer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @dunglas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When declaring the POST method, we have to declare the GET method to access it.

That's why I wanted to put it, because we often need the GET on the collection

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm saying you can do like this:

 *     collectionOperations={
 *         "get",
 *         "post"={"validation_groups"={"Default", "postValidation"}}
 *     },


`postValidation` contains the constraints on the name and author (length from 2 to 50) fields only.

`putValidation` contains the constraints on the author (length from 2 to 70) fields only.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field


With this configuration, there are three validation groups:

`Default` contains the constraints in the current class and all referenced classes that belong to no other group.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the all referenced classes part. You need a Valid assert for this kind of behavior, don't you?

@@ -167,6 +167,70 @@ Of course, you can use XML or YAML configuration format instead of annotations i
You may also pass in a [group sequence](http://symfony.com/doc/current/validation/sequence_provider.html) in place of
the array of group names.

## Using Validation Groups on operations

There are [2 builtin collection operations, and 4 item operations](operations.md) (PATCH is JSONAPI specific), but API Platform supports any type and any number of operations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reword it like this:

You can have different validation for each [operation](operations.md) related to your resource.

Like this you don't need to duplicate the explanation about the concept of operations in API Platform.

* @ApiResource(
* collectionOperations={
* "get",
* "post"={"method"="POST", "validation_groups"={"Default", "postValidation"}}
Copy link
Member

@alanpoulain alanpoulain Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's working if you remove the method part but I'm not sure about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is.

* itemOperations={
* "delete",
* "get",
* "put"={"method"="PUT", "validation_groups"={"Default", "putValidation"}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, you can remove "method"="PUT"

@@ -167,6 +167,70 @@ Of course, you can use XML or YAML configuration format instead of annotations i
You may also pass in a [group sequence](http://symfony.com/doc/current/validation/sequence_provider.html) in place of
the array of group names.

## Using Validation Groups on operations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operations

@toofff
Copy link
Contributor Author

toofff commented Jun 29, 2018

@dunglas @alanpoulain Done

@dunglas dunglas merged commit 96c9cc5 into api-platform:2.2 Jun 29, 2018
@dunglas
Copy link
Member

dunglas commented Jun 29, 2018

Thanks @toofff

@toofff toofff deleted the feature/update-validation-groups branch June 29, 2018 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants