Enforce header entity length in grammar#15
Conversation
|
Doesn't work so simple, have to take another look for |
|
Some things are easier when you do them not in the grammar, but on the parsed ast tree. e.g |
126c1d1 to
083f962
Compare
__init__.py
Outdated
| observed = header.get(field.upper(), []) | ||
| expected = HEADER_FIELDS.get(field)._fields | ||
| if len(header.get(field.upper(), [])) != len(expected): | ||
| raise HeaderFieldError(field.upper(), len(observed), len(expected)) |
There was a problem hiding this comment.
Do we want to really raise an exception and terminate or wait for potentially additional errors?
aothms
left a comment
There was a problem hiding this comment.
Really nice, just would prefer without the getattr() if possible, but understand where it's coming from: exceptions are for exceptional control flow, but we are accumulating them.
__main__.py
Outdated
| print(exc, file=sys.stderr) | ||
| else: | ||
| json.dump(exc.asdict(), sys.stdout) | ||
| json.dump([e.asdict() for e in getattr(exc, "errors", [exc])], sys.stdout, indent=2) |
There was a problem hiding this comment.
Can we actually catch both cases separately CollectedValidationErrors, ValidationError instead, or refactor ValidationError to always point to a list of errors.
There was a problem hiding this comment.
I designed it in a way that ValidationErrors is never raised directly; the the issues are represented in subclasses (e.g. SyntaxError, DuplicateNameError, etc) and collected into a single CollectedValidationErrors instance, which is then raised. As a result, the getattr was already redundant and we can rely on exc.errors directly
json.dump([e.asdict() for e in exc.errors], sys.stdout, indent=2)
ValidationError is now only used for testing as it covers all sub classes with pytest.raises(_ValidationError).
I've also renamed it to _ValidationErrors and added documentation, clarifying that we only use it internally. The parser itself also doesn't import it directly.
No description provided.