Parsix: Parse Don't Validate
I have to be honest, I&#x27;m not seeing what problem this is trying to solve. Anyone can enlighten me?<p>Edit: Ok I think I understand...<p>It seems the problem would be that if you&#x27;re implementing a function that takes a user email as a string, and that function is in a lower layer of the application, like inside the data access layer. It is difficult at this point to know if the email string you will be passed as input has already been validated or not. Thus you might be tempted to re-implement validation for it at your level inside this function as well and have an assertValidEmail check.<p>This can lead to a littering of validation throughout the code base, as each implemented function worries that the input isn&#x27;t validated and re-validates it, possibly using slightly different rules each time.<p>Furthermore, if you decide to not validate it again, you might be left wondering, but am I sure it&#x27;ll have been validated prior? How can I be sure? Someone in the future could easily start calling my function and forget to validate the email before calling it? This could eventually lead to a security issue or just a bug, by introducing a code path that doesn&#x27;t ever validate the email string.<p>Thus if instead you&#x27;d re-write your function so it takes an email as a ValidEmail type (or object), and not as a string, you force the caller to for sure remember to validate the email first. And you also can safely assume if you&#x27;re getting an email as a ValidEmail type that it has been validated. It could also technically allow you to localize the validation logic to the ValidEmail type constructor, avoiding possible duplicate attempts at validating email with different rules.<p>And it seems the latter &quot;style&quot; the author calls &quot;Parsing&quot; while the former they call &quot;Validating&quot;, in the sense that since the function validating returns a modified structure it &quot;parsed&quot; it, because a string became a ValidEmail, thus parsing a string into a ValidEmail, as opposed to simply validating that the string is valid as an email.<p>And finally, this is a little library to help make use of this pattern in Kotlin.