[Zope3-dev] Re: Add function for schema validation in zope.schema
On 2007-08-20 14:59:52 +0200, Fred Drake [EMAIL PROTECTED] said: On 8/20/07, Christian Zagrodnick [EMAIL PROTECTED] wrote: I think we should add a function to validate a given schema on a given class. This should include constraints and invariants: I do presume you mean object, rather than class, as your example implies. validateSchema(IMySchema, myobject) [or alike] +1 I'm not sure about return values or exceptions. There are different use cases: 1. Is the object valid? - True/False 2. What's wrong with the object - {attribute/field-name: what's wrong}, what's wrong with invariants. There should probably be a specific exception that's defined for this purpose, and it could directly support the mapping interface to allow detailed information to be extracted. I suspect a common use would be to simply verify the object and raise the exception in the invalid case, denying whatever operation was being attempted. This also suggests that there's no interesting return value; either the exception is raised or it isn't. Code that wants to introspect the exception can catch that and act accordingly. Yes, I like the idea. Will add it with the exception as time comes by. -- Christian Zagrodnick gocept gmbh co. kg · forsterstrasse 29 · 06112 halle/saale www.gocept.com · fon. +49 345 12298894 · fax. +49 345 12298891 ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
[Zope3-dev] Re: Add function for schema validation in zope.schema
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Fred Drake wrote: On 8/20/07, Christian Zagrodnick [EMAIL PROTECTED] wrote: I think we should add a function to validate a given schema on a given class. This should include constraints and invariants: I do presume you mean object, rather than class, as your example implies. validateSchema(IMySchema, myobject) [or alike] +1 I'm not sure about return values or exceptions. There are different use cases: 1. Is the object valid? - True/False 2. What's wrong with the object - {attribute/field-name: what's wrong}, what's wrong with invariants. There should probably be a specific exception that's defined for this purpose, and it could directly support the mapping interface to allow detailed information to be extracted. I suspect a common use would be to simply verify the object and raise the exception in the invalid case, denying whatever operation was being attempted. This also suggests that there's no interesting return value; either the exception is raised or it isn't. Code that wants to introspect the exception can catch that and act accordingly. - -1. Detecting the schema violation is mechanism, raising an exception is policy; they shouldn't be mixed here. Let the caller raise an exception if that is appropriate Tres. - -- === Tres Seaver +1 540-429-0999 [EMAIL PROTECTED] Palladion Software Excellence by Designhttp://palladion.com -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGyZ9V+gerLs4ltQ4RAvEkAJwKK50BjjTwzNE39gsw1nXXq+JNVACcDKWK 2jxn+16Ax1Lx2sXf6vAy2EM= =Y25B -END PGP SIGNATURE- ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
[Zope3-dev] Re: Add function for schema validation in zope.schema
Christian Theune wrote: Hi, Am Montag, den 20.08.2007, 08:59 -0400 schrieb Fred Drake: On 8/20/07, Christian Zagrodnick [EMAIL PROTECTED] wrote: I think we should add a function to validate a given schema on a given class. This should include constraints and invariants: I do presume you mean object, rather than class, as your example implies. validateSchema(IMySchema, myobject) [or alike] +1 I'm not sure about return values or exceptions. There are different use cases: 1. Is the object valid? - True/False 2. What's wrong with the object - {attribute/field-name: what's wrong}, what's wrong with invariants. There should probably be a specific exception that's defined for this purpose, and it could directly support the mapping interface to allow detailed information to be extracted. I suspect a common use would be to simply verify the object and raise the exception in the invalid case, denying whatever operation was being attempted. This also suggests that there's no interesting return value; either the exception is raised or it isn't. Code that wants to introspect the exception can catch that and act accordingly. From my latest experience and research of when to use exceptions and when to use return values I'd say let's not use an exception. The report of which fields are wrong is the normal result of this function. Invalid data is not an uncommon output, rather, it's the sole purpose of this function. An exception should be raised if the validation could not be performed. The result could be a structure that lists all errors. Eventually a result that equals False could be used to signal no errors, e.g. an empty dict or an empty list. This is exactly what I would suggest: The function should return a list of validation errors. Similar semantics are used in zope.formlib's validate() method. I agree with Wichert's objection about being a bit confusing, though. Perhaps a better name than validateSchema could solve this problem, though I can't think of a good one right now... getValidationErrors() seems to naive... Having access to the original ValidationError exceptions (incl. the field name that they were thrown for) is probably all the information you're ever going to want (and get). Currently I suspect that a list as the result would be more useful, if we do care for invariants. What about this: [(Field xy, u'Invalid foo'), (Invariant ab, u'Invalid bar')] If the list is empty then there were no errors. -- http://worldcookery.com -- Professional Zope documentation and training ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
[Zope3-dev] Re: Add function for schema validation in zope.schema
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Wichert Akkerman wrote: Previously Christian Theune wrote: Hi, Am Montag, den 20.08.2007, 08:59 -0400 schrieb Fred Drake: On 8/20/07, Christian Zagrodnick [EMAIL PROTECTED] wrote: I think we should add a function to validate a given schema on a given class. This should include constraints and invariants: I do presume you mean object, rather than class, as your example implies. validateSchema(IMySchema, myobject) [or alike] +1 I'm not sure about return values or exceptions. There are different use cases: 1. Is the object valid? - True/False 2. What's wrong with the object - {attribute/field-name: what's wrong}, what's wrong with invariants. There should probably be a specific exception that's defined for this purpose, and it could directly support the mapping interface to allow detailed information to be extracted. I suspect a common use would be to simply verify the object and raise the exception in the invalid case, denying whatever operation was being attempted. This also suggests that there's no interesting return value; either the exception is raised or it isn't. Code that wants to introspect the exception can catch that and act accordingly. From my latest experience and research of when to use exceptions and when to use return values I'd say let's not use an exception. The report of which fields are wrong is the normal result of this function. Invalid data is not an uncommon output, rather, it's the sole purpose of this function. An exception should be raised if the validation could not be performed. The result could be a structure that lists all errors. Eventually a result that equals False could be used to signal no errors, e.g. an empty dict or an empty list. That would be confusing though: I would expect the result of a method that checks validaty to return something that evaluates to True if everything is valid. Code like this just messes up my brain: if not zope.schema.validate(obj, IMySchema): print Everything validates correctly! to me that is very non-intuitive and looks like the if condition is incorrect. Returning boolean discards the information which only this function should know, and which the caller (potentially) needs: the list of errors. The name 'validate' doesn't imply a boolean return to me at all. I would expect to see it used as follows:: errors = zope.schema.validate(obj, IMySchema) if not errors: print 'OK else: print 'Errors: \n%s' % '\n'.join([str(x) for x in errors]) Tres. - -- === Tres Seaver +1 540-429-0999 [EMAIL PROTECTED] Palladion Software Excellence by Designhttp://palladion.com -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGyaBB+gerLs4ltQ4RAjDQAJ9e1gY/MxrAF7dOpTcATtQPjLAQ7gCgqy4X 2QztzPecPxDIKEqfvJUr0AI= =IxRz -END PGP SIGNATURE- ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
[Zope3-dev] Re: Add function for schema validation in zope.schema
Am Montag, den 20.08.2007, 16:05 +0200 schrieb Philipp von Weitershausen: This is exactly what I would suggest: The function should return a list of validation errors. Similar semantics are used in zope.formlib's validate() method. I agree with Wichert's objection about being a bit confusing, though. Perhaps a better name than validateSchema could solve this problem, though I can't think of a good one right now... getValidationErrors() seems to naive... Having access to the original ValidationError exceptions (incl. the field name that they were thrown for) is probably all the information you're ever going to want (and get). Right. The validation error objects are probably enough as they have enough context to retrieve the field or invariant for which they apply. ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
Re: [Zope3-dev] Re: Add function for schema validation in zope.schema
On 20 Aug 2007, at 16:12 , Benji York wrote: Philipp von Weitershausen wrote: This is exactly what I would suggest: The function should return a list of validation errors. Similar semantics are used in zope.formlib's validate() method. I agree with Wichert's objection about being a bit confusing, though. Perhaps a better name than validateSchema could solve this problem, though I can't think of a good one right now... getValidationErrors() seems to naive... I feel myself slipping into a bikeshed, so this may be my last comment on this. I like getValidationErrors. It's use would probably normally look something like this: errors = getValidationErrors(...) if errors: ... or: errors = getValidationErrors(...) if not errors: ... Both look good to me. +1 ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com
Re: [Zope3-dev] Re: Add function for schema validation in zope.schema
On 8/20/07, Benji York [EMAIL PROTECTED] wrote: I like getValidationErrors. It's use would probably normally look something like this: ... Both look good to me. Ok, agreed, for reasons people have already given. -Fred -- Fred L. Drake, Jr.fdrake at gmail.com Chaos is the score upon which reality is written. --Henry Miller ___ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com