question 283 Advanced OOP & Design · easy
Validation rules from one base class
The abstract Rule class is given: each rule checks one field of a record, and its concrete check(record) handles missing fields and message formatting, calling the abstract problem(value). Write two rules, MaxLength(field, limit) (text longer than limit) and OneOf(field, choices) (a value not among the choices), and validate(record, rules) returning every message, in rule order.
`MaxLength(field, limit)`
problem → "longer than <limit> characters", or None`OneOf(field, choices)`
problem → "<value> is not one of <choices joined with ', '>", or None`validate(record, rules)`the non-None results of
rule.check(record), in orderGiven
Rule.check returns "<field>: missing", "<field>: <problem>", or None