This looks like general purpose logic that could be extracted to a `Validator` object or something.