Hi everyone. I need your advices in the next question:
when you write the code, are you using over type safety or not? let me explain, what I mean:
For example:
I want that when I parse data from stdin, I get the the valid data, non empty strings in that case and nonEmpty numeric values
data Http'
= Http'
{ getHttpHost :: Maybe Text
, getHttpPort :: Maybe Word16
, getHttpTimeout :: Maybe Text
} deriving stock Show
Then I want to see error message of validation like this
-- ("Http", "http host must not be empty" :| [])
type Error = (String, NonEmpty String)
also I want that validated data type will be signed as validated in type level
data Http'
= Http'
{ getHttpHost :: Maybe Text
, getHttpPort :: Maybe Word16
, getHttpTimeout :: Maybe Text
} deriving stock Show
newtype Http = Http Http' deriving stock Show
-- or
newtype ValidatedHttp = ValidatedHttp Http' deriving stock Show
validate :: Http' -> Either (NonEmpty Error) Http
-- or
validate :: Http' -> Either (NonEmpty Error) ValidatedHttp
and in the end it looks like this
-- if Http is Valid
Right (Http (Http' {getHttpHost = Just "fdsfs", getHttpPort = Just 0, getHttpTimeout = Just "fdsfsd"}))
-- if Http is not Valid
Left (("Http","host must not be empty" :| ["port must not be empty","timeout must not be empty"]) :| [])
Is it okay that I do so many type safety things
If we use Either we assume that Left has some kind of an error, right? and we don’t need to emphasize that Left might be empty or not?
What about ValidatedHttp? Is it okay to return validated structure or not?
What about getHttpHost :: Maybe Text? Is it okay to move Maybe Text to NonEmpty Text
I’ve read many times these articles, but I’m not sure that what I’m doing is a right way
https://www.parsonsmatt.org/2017/10/11/type_safety_back_and_forth.html
https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
Thanks in advance
Sorry for English, it’s not my native