easy +8 pts

Validation result object

Build a class that collects validation errors and reports whether the input is valid.

Implement a class `ValidationResult` that collects validation error messages and reports whether validation passed. The class must have: - `__init__(self)`: initializes an empty collection of error messages. - `add_error(self, message: str) -> None`: appends `message` to the collection. - `is_valid(self) -> bool`: returns `True` if there are no errors, `False` otherwise. - `errors(self) -> list[str]`: returns a NEW list containing the error messages in the order they were added. The returned list must be a copy, not a reference to the internal state. Use these methods to accumulate errors and then check validity.

Constraints

No external packages. The number of errors is unlimited. Error messages are non-empty strings. The returned list from `errors()` must be independent of internal state.

Example

>>> result = ValidationResult()
>>> result.is_valid()
True
>>> result.add_error('name is required')
>>> result.add_error('email is invalid')
>>> result.is_valid()
False
>>> result.errors()
['name is required', 'email is invalid']
8 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Store error messages in a list attribute inside __init__.
add_error should append the message to that list.
is_valid should check whether the list is empty.
errors should return a copy (e.g., list(self._errors)) so external mutations don't affect internal state.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.