quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.88k stars 2.71k forks source link

constraint mappings specified in META-INF/validation.xml aren't applied #5531

Open rpdeadly opened 5 years ago

rpdeadly commented 5 years ago

Hibernate's Validator implementation fails to catch any violation. I've got all of my validation rules specified in an XML ( because I do not have access to the source code ).


META-INF/validation.xml

<validation-config
        xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration https://beanvalidation.org/xml/ns/validation/validation-configuration-2.0.xsd"
        version="2.0">
    <constraint-mapping >
        META-INF/validation/organization.xml
    </constraint-mapping>
</validation-config>

META-INF/validation/organization.xml

<constraint-mappings
    xmlns="http://xmlns.jcp.org/xml/ns/validation/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/mapping
            http://xmlns.jcp.org/xml/ns/validation/mapping/validation-mapping-2.0.xsd"
    version="2.0">
    <bean class="demo.Organization"
        ignore-annotations="true">
        <field name="owner">
            <constraint
                annotation="javax.validation.constraints.NotNull">
                <message>Org owner must be specifed</message>
            </constraint>
        </field>
    </bean>
</constraint-mappings>  

In my service I'm trying to validate the object

@Inject Validator validator; ... Set<ConstraintViolation> violations = validator.validate( myOrg ); if( ! violations.isEmpty() ) return false;

The violation set is always empty. I've tried my configuration using a standalone implementation of hibernate-validator and it works.

geoand commented 5 years ago

I am not sure that violation.xml is even supported. @gsmet can of course shine all the necessary light here

machi1990 commented 5 years ago

I can see that the file it is hot reloaded https://github.com/quarkusio/quarkus/commit/45f875bd1c4b651f7a43617012a4a2c2ae786895#diff-b325242f07b81465b410fd3c1e40914aL78 but we are not trying to parse it anywhere. Let's wait for @gsmet to give his thoughts.

gsmet commented 5 years ago

Right now, we only support annotations in Quarkus. It should probably be documented if it's not already.

I'm still unsure if we should implement more of the logic in Quarkus or if we should move all this discovery to HV somehow.

rpdeadly commented 5 years ago

I found a partial work around. The rules specified in validation.xml are applied if i create a dummy class that extends the class I'm trying to validate. But validation cascading doesn't work.

    <field name="owner">
        <valid></valid>
        <convert-group
                from="demo.NewOrgCheck"
                to="demo.NewOwnerCheck" />
                    <constraint
                annotation="javax.validation.constraints.NotNull">
                <message>Org owner must be specifed</message>
            </constraint>
    </field>

The fields within the owner class aren't validated.

marko-bekhta commented 3 months ago

A corresponding Hibernate Validator issue: https://hibernate.atlassian.net/browse/HV-2031