Search code examples
xmllogback

XML Schema validation throws two different error messages for same error


For my current project I get some XML data from an API and validate it against an XSD schema.

I use Logback to log errors and to also send an email to selected addresses. Now, my problem is that I always get two error messages per error, like this:

[ERROR] 2018-09-20 09:00:06.619 c.j.d.H.v.CustomValidationEventHandler - ERROR VALIDATING OBJECT:  Result [ /* some data here */ ]
MESSAGE: cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_VoraussichtlicherEintrittsterminResult'.
[ERROR] 2018-09-20 09:00:06.619 c.j.d.H.v.CustomValidationEventHandler - ERROR VALIDATING OBJECT:  Result [ /* some data here */ ]
MESSAGE: cvc-type.3.1.3: The value '' of element 'VoraussichtlicherEintrittstermin' is not valid.

This is element in the XSD schema:

<xs:element name="VoraussichtlicherEintrittstermin">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
            <xs:maxLength value="10" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

Here's another example:

[ERROR] 2018-09-19 13:30:01.981 c.j.d.H.v.CustomValidationEventHandler - ERROR VALIDATING OBJECT:  Result [ ... ]
MESSAGE:  cvc-pattern-valid: Value '0' is not facet-valid with respect to pattern '[0-9]{3,7}' for type '#AnonType_BerufsbezeichnungIDResult'.
[ERROR] 2018-09-19 13:30:01.982 c.j.d.H.v.CustomValidationEventHandler - ERROR VALIDATING OBJECT:  Result [ ... ]
MESSAGE:  cvc-type.3.1.3: The value '0' of element 'BerufsbezeichnungID' is not valid.

And the element in the schema:

<xs:element name="BerufsbezeichnungID">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{3,7}" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

Is there some way I can configure Logback to only send one of those messages? Or is it because of my XSD schema, that counts those errors as two?


Solution

  • I don't know LogBack, but I guess the schema processor is written on the basis that it's better to give you too much information rather than too little, and no-one thought too hard about working out whether two messages relate to the "same error" if indeed there's a well defined concept of what constitutes "one error". In these examples the information in the two messages is complementary: one tells you that there's a value that doesn't conform to the expected type, the second tells you which element that value appears in. Internally in the schema processor it can sometimes be quite hard to aggregate this information without risking losing something important.