Skip to content
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

Handle sequentially validator constraints when generating property metadata #4139

Conversation

norkunas
Copy link
Contributor

@norkunas norkunas commented Mar 16, 2021

Q A
Branch? main
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #...
License MIT
Doc PR -

Currently property metadata from validator constraints (length/regex etc.) is only taken when these constraints are applied directly. Since Symfony 5.1 there is Sequentially constraint which allows to validate as a group sequence but much easier. But api platform doesn't take them because they're nested inside the Sequentially constraint.

I have updated to include all nested constraints from the Composite, but maybe that's too wide?
Also not sure about that maybe constraint flattening should be recursive?

@norkunas norkunas force-pushed the validator-property-metadata-with-composite-constraints branch 2 times, most recently from 73a91ad to dea8150 Compare March 16, 2021 11:04
@alanpoulain
Copy link
Member

Hello,
Thanks for your PR, seems legit to me.
What do you mean by:

I have updated to include all nested constraints from the Composite, but maybe that's too wide?

@norkunas norkunas force-pushed the validator-property-metadata-with-composite-constraints branch from dea8150 to b9d7812 Compare March 16, 2021 11:11
@norkunas
Copy link
Contributor Author

Hello,
Thanks for your PR, seems legit to me.
What do you mean by:

I have updated to include all nested constraints from the Composite, but maybe that's too wide?

For example there is an AtLeastOneOf constraint which is also Composite, but it has a different use case than Sequentially..

@alanpoulain
Copy link
Member

For example there is an AtLeastOneOf constraint which is also Composite, but it has a different use case than Sequentially..

I see. It seems OK to me to use all the nested constraints of AtLeastOneOf too.

Shouldn't it be recursive instead of a loop? It seems to me that you can combine the composite constraints.

@norkunas
Copy link
Contributor Author

For example there is an AtLeastOneOf constraint which is also Composite, but it has a different use case than Sequentially..

I see. It seems OK to me to use all the nested constraints of AtLeastOneOf too.

Shouldn't it be recursive instead of a loop? It seems to me that you can combine the composite constraints.

So you mean I should do recursive constraint resolving? :)

@alanpoulain
Copy link
Member

Yes, does it seem correct to you?

@norkunas
Copy link
Contributor Author

Yes, does it seem correct to you?

Maybe.. added a second commit with flattening


foreach ($propertyConstraints as $propertyConstraint) {
if ($propertyConstraint instanceof AtLeastOneOf) {
continue;
Copy link
Member

Choose a reason for hiding this comment

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

Why continuing in this case? I think we should take the constraints of AtLeastOneOf too for the property metadata, don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm.. Not sure for real, because:

@Assert\AtLeastOneOf({
    @Assert\Length(min=1, max=2),
    @Assert\Length(min=5, max=6)
})

The last one would override the first one and then the schema would be wrong 🤔

@norkunas
Copy link
Contributor Author

Also with the flattening I think this could go wrong, because the restrictions would be directly applied to the property, not to the array values

@Assert\Sequentially({
    @Assert\Type(type="array"),
    @Assert\All({
        @Assert\Length(min=1, max=32),
        @Assert\Regex(pattern="/^[a-z]$/")
    })
})

@alanpoulain
Copy link
Member

Also with the flattening I think this could go wrong, because the restrictions would be directly applied to the property, not to the array values

@Assert\Sequentially({
    @Assert\Type(type="array"),
    @Assert\All({
        @Assert\Length(min=1, max=32),
        @Assert\Regex(pattern="/^[a-z]$/")
    })
})

You're right, I think we should blacklist (or maybe it would be preferable to whitelist the wanted constraints) All and Collection.

@norkunas
Copy link
Contributor Author

Maybe for now would be good to handle the Sequentially at the first level and in future if someone come up with another use cases then could make follow-up PR?

@alanpoulain
Copy link
Member

Yes, please do, sorry for the unwanted change.
I think the AtLeastOneOf constraint is manageable with https://json-schema.org/understanding-json-schema/reference/combining.html#oneof, but we can do it in another PR.

@norkunas norkunas force-pushed the validator-property-metadata-with-composite-constraints branch from b834eb7 to 6fb68e1 Compare March 17, 2021 09:36
@norkunas
Copy link
Contributor Author

Yes, please do, sorry for the unwanted change.
I think the AtLeastOneOf constraint is manageable with https://json-schema.org/understanding-json-schema/reference/combining.html#oneof, but we can do it in another PR.

All good :) I agree, one problem at a time :)

@norkunas norkunas force-pushed the validator-property-metadata-with-composite-constraints branch from 6fb68e1 to f2c84af Compare March 17, 2021 09:37
@alanpoulain alanpoulain changed the title Handle composite validator constraints when generating property metadata Handle sequentially validator constraints when generating property metadata Mar 17, 2021
@norkunas norkunas force-pushed the validator-property-metadata-with-composite-constraints branch from f2c84af to 0564555 Compare March 17, 2021 09:40
@alanpoulain
Copy link
Member

I'm checking if we shouldn't merge this PR as a bugfix.

@norkunas
Copy link
Contributor Author

I'm checking if we shouldn't merge this PR as a bugfix.

@soyuka said to submit as a feature 🙂

@chalasr
Copy link
Contributor

chalasr commented Mar 17, 2021

While this looks like an enhancement (as it's adding support for a Symfony feature that is currently not supported), I think it qualifies as a bugfix as it is actually fixing compatibility with Symfony 5.1 (at least one way to use Symfony 5.1).
Merging in 2.6 would probably be helpful for developers embracing Symfony 5.1 features and does not seem harmful in any way.

@soyuka soyuka changed the base branch from main to 2.6 March 18, 2021 14:03
@soyuka soyuka force-pushed the validator-property-metadata-with-composite-constraints branch from 0564555 to cae1758 Compare March 18, 2021 14:03
@soyuka soyuka merged commit 47be2c5 into api-platform:2.6 Mar 18, 2021
@soyuka
Copy link
Member

soyuka commented Mar 18, 2021

Thanks @norkunas !

@norkunas
Copy link
Contributor Author

Thanks for merging :)

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.

None yet

4 participants