Req #30814 [Opn->Csd]: more detailed feedback from DOMDocument::schemaValidate

From: Date: Fri, 08 Apr 2022 08:07:06 +0000
Subject: Req #30814 [Opn->Csd]: more detailed feedback from DOMDocument::schemaValidate
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-240801@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=30814&edit=1 ID: 30814 Updated by: ilutov@php.net Reported by: pecoes at web dot de Summary: more detailed feedback from DOMDocument::schemaValidate -Status: Open +Status: Closed Type: Feature/Change Request -Package: Feature/Change Request +Package: *General Issues Operating System: windows xp PHP Version: 5.0.2 -Assigned To: +Assigned To: ilutov Block user comment: N Private report: N New Comment: This issue is very old. If it's still relevant please create a new issue on GitHub. Previous Comments: ------------------------------------------------------------------------ [2004-12-06 08:11:43] 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. ------------------------------------------------------------------------ [2004-11-17 16:55:14] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=30814&edit=1

« previous php.bugs (#240801) next »