php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30814 more detailed feedback from DOMDocument::schemaValidate
Submitted: 2004-11-17 16:55 UTC Modified: 2022-04-08 08:07 UTC
Votes:6
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:1 (20.0%)
From: pecoes at web dot de Assigned: ilutov (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.2 OS: windows xp
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pecoes at web dot de
New email:
PHP Version: OS:

 

 [2004-11-17 16:55 UTC] pecoes at web dot de
Description:
------------
A lot can go wrong during schema validation:

1.) the schema cannot be opened
2.) the schema is not well-formed
3.) the schema itself is invalid
4.) the $dom in question is invalid

Currently there is neither a way to distinguish between these four problems, nor a way to capture an actual validation error(4). Imho schemaValidate really needs some refinements. Here's an idea:

schemaValidate (string file [, integer errType [, string errMsg]])

errType and errMsg would describe the error (if any) and schemaValidate would STOP triggering errors and return false instead. Usage:

if (!$dom->schemaValidate($schema, $errType, $errMsg)) {
    switch ($errType) {
        case SCHEMA_NOT_FOUND:
            throw new Exception("$schema not found");
        case SCHEMA_NOT_WELLFORMED:
            throw new Exception("$schema not well-formed");
        case SCHEMA_INVALID:
            throw new Exception("$schema is invalid");
        case SCHEMA_DOM_INVALID:
            throw new Exception("XML invalid: $errMsg");
    }
}

or simply:

if (!$dom->schemaValidate($schema, $errType, $errMsg)) {
    throw new Exception($errMsg ? $errMsg : "$schema not usable");
}

Whether you like my idea or not, I hope you'll agree that:

if (!@$dom->schemaValidate($schema)) {
    throw new Exception("cannot validate or invalid input");
}

is not enough.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-06 08:11 UTC] anthony at ectrolinux dot com
I second that this issue needs to be addressed, as it's quite tiresome to code work-arounds for this problem. While not the most elegant solution, the errors DOM emits when validating an XML document against a schema can be captured, parsed and processed at the script level to emulate the functionality you described. 

From an implementational standpoint, I agree with your idea in principle, though I believe that a few modifications are necessary:

Let's assume that a user is going to design an XML Schema to validate user-submitted form data, which will be validated with DOM (using XPath): this data will be grouped into a set of elements, and then validated. 

In order to best represent the errors for this scenario, it would be helpful for DOM to throw a few basic pieces information, including: The parent path of the faulty element, the name of the element, a description of the error and the error -code-. In place of an error -number-, using the W3C-defined error codes for the XML Schema Structures specification would provide a consistant means of reporting invalidities. You could then, as you demonstrated, use a simple switch statement to process the errors as needed.

Though I believe the above would be well suited to addressing the problem outlined by this bug, as mentioned in the above scenario, it would also be helpful to know only the elements that -failed- to validate. A method of "reverse-validating" a document would be quite helpful, in that it would simply report the elements that failed to validate. This could be done at the script level if the functionality was not provided natively by PHP.
 [2022-04-08 08:07 UTC] ilutov@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: ilutov
 [2022-04-08 08:07 UTC] ilutov@php.net
This issue is very old. If it's still relevant please create a new issue on GitHub.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC