I would like to make a validation function taking a name and outputting a validName type. I don't want to be able to construct values of type ValidName outside the module without using the function validateName.
I am trying to make the ValidName type private, but it makes it impossible for me to use it in the validateName function (event if it is in the same Module).
What is the right way to do this in rescript?
Here is the code:
module MyModule = {
type notValidatedName = NotValidatedName(string)
type validName = private ValidName(string)
type errorMessage = string
let validateName = (~name: notValidatedName): Belt.Result.t<validName, errorMessage> => {
switch name {
| NotValidatedName(name) when Js.String2.length(name) <= 2 => Belt.Result.Ok(ValidName(name)) // not possible because ValidName is private
| _ => Belt.Result.Error("String is too short to be a name")
}
}
}
let nameToShort = MyModule.ValidName("aa") // I don't want this to be possible
let notValidName = MyModule.NotValidatedName("aa") // This is fine
let nameResult = MyModule.validateName(~name=notValidName)