Refactor the validation part of from_str into its own function as the behavior for try_from is identical.