Created
May 23, 2013 01:45
-
-
Save agrif/5632261 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import parseyaml | |
| import validators | |
| import annotations | |
| class WidgetValidator(validators.ObjectValidator): | |
| foo = validators.StringValidator() | |
| bar = validators.StringValidator() | |
| def validate_object(self): | |
| if self.bar != "racuda": | |
| raise validators.ValidationError("bar must be 'racuda'") | |
| return self.validated | |
| p = parseyaml.YAMLParser(open('data.yml')) | |
| v = WidgetValidator() | |
| try: | |
| print v.validate(p.parse()) | |
| except annotations.AnnotatedError, e: | |
| print e | |
| # some example input files (<--) and output (-->) | |
| # | |
| # (<--) foo: something (-->) {'foo': u'something', 'bar': u'racuda'} | |
| # bar: racuda | |
| # | |
| # (<--) foo: something (-->) in data.yml, lines 1-2: | |
| # bar: else invalid widget: bar must be 'racuda' | |
| # | |
| # (<--) foo: something (-->) in data.yml, line 1: | |
| # bar: racuda duplicate setting foo in widget (first was in data.yml, line 3) | |
| # foo: another | |
| # | |
| # (<--) foo: something (-->) in data.yml, line 2: | |
| # baz: racuda unknown setting baz in widget (did you mean bar?) | |
| # | |
| # (<--) foo: 1.0 (-->) in data.yml, lines 1-2: | |
| # bar: racuda invalid string | |
| # in data.yml, lines 1-3: | |
| # validation failed for setting foo in widget | |
| # | |
| # (<--) bar: racuda (-->) in data.yml, lines 1-2: | |
| # missing setting foo in widget: expected string | |
| # |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
input:
current output: