Skip to content

Instantly share code, notes, and snippets.

@lucamug
Last active April 2, 2020 01:30
Show Gist options
  • Save lucamug/e5f25f1a6302a79cf3273b47dd2978a2 to your computer and use it in GitHub Desktop.
Save lucamug/e5f25f1a6302a79cf3273b47dd2978a2 to your computer and use it in GitHub Desktop.
module Form.Example exposing (..)
import Form.Key
type alias ValidationMessage =
String
type alias EntityId =
String
type Validation
= OneOf (Maybe ValidationMessage) (List Validation)
| AllOf (Maybe ValidationMessage) (List Validation)
-- ^^^^^
-- This is a Maybe to decide if we want to use one message for the entire
-- group of instead we want to display all single messages for each
-- validation.
| Dependant Form.Key.Key Validation
| Required
| Equal -- Pass if the value is uqual to the value, used for Dependant
| MinLength Int
| MaxLength Int
| Regex String
type FieldType
= BlahBlah
type alias TextConf =
{ title : String
, helperText : Maybe String
, validationSpecs : Maybe ValidationSpecs
}
type alias FieldConf =
{ id : String
, idDom : Maybe String
, type_ : FieldType
, label : String
, helperText : Maybe String
, validationSpecs : Maybe ValidationSpecs
}
type Entity
= EntityNormal EntityId (List Entity)
| EntityWrappable EntityId (List Entity)
| EntityWithBorder EntityId (List Entity)
| EntityWithTabs EntityId (List Entity)
| EntityMulti EntityId (List Entity)
| EntityField FieldConf
| EntityTitle TextConf
| EntitySubTitle TextConf
type alias ValidationSpecs =
{ showAlsoPassedValidations : Bool
, hideCheckmark : Bool
, validation : Validation
}
-- Example
fieldPassword : FieldConf
fieldPassword =
{ id = "fieldPassword"
, idDom = Nothing
, type_ = BlahBlah
, label = "Password"
, helperText = Nothing
, validationSpecs =
Just
{ showAlsoPassedValidations = False
, hideCheckmark = False
, validation =
AllOf
Nothing
[ MinLength 8
, MaxLength 128
, Required
, OneOf (Just "INVALID_FORMAT_INVALID_CHARACTERS") [ Regex "^[!-~]*$" ]
, OneOf (Just "INVALID_FORMAT_NO_UPPERCASE") [ Regex "[A-Z]" ]
, OneOf (Just "INVALID_FORMAT_NO_NUMBER") [ Regex "[0-9]" ]
, OneOf (Just "INVALID_FORMAT_NO_SPECIAL_CHARACTERS") [ Regex "[~!@#$%^&*()_+|}{\\[\\]|\\><?:\\\";',./=-]" ]
]
}
}
fieldRetypePassword : FieldConf
fieldRetypePassword =
{ id = "fieldRetypePassword"
, idDom = Nothing
, type_ = BlahBlah
, label = "Re-type the Password"
, helperText = Nothing
, validationSpecs =
Just
{ showAlsoPassedValidations = False
, hideCheckmark = False
, validation =
OneOf
(Just "SHOULD_BE_SAME_AS_PASSWORD")
[ Dependant (Form.Key.fromString "password") Equal ]
}
}
fieldContractPolicy : FieldConf
fieldContractPolicy =
{ id = "fieldContractPolicy"
, idDom = Nothing
, type_ = BlahBlah
, label = "Contract Policy"
, helperText = Nothing
, validationSpecs = Nothing
}
fieldPrivacyPolicy : FieldConf
fieldPrivacyPolicy =
{ id = "fieldPrivacyPolicy"
, idDom = Nothing
, type_ = BlahBlah
, label = "Privacy Policy"
, helperText = Nothing
, validationSpecs = Nothing
}
titleAgreementURLs : TextConf
titleAgreementURLs =
{ title = "Agreement URLs"
, helperText = Nothing
, validationSpecs =
Just
{ showAlsoPassedValidations = False
, hideCheckmark = True
, validation =
OneOf (Just "AT_LEAST_ONE_URL_IS_REQUIRED")
[ Dependant (Form.Key.fromString "fieldContractPolicy") Required
, Dependant (Form.Key.fromString "fieldPrivacyPolicy") Required
]
}
}
form : List Entity
form =
[ EntityTitle { title = "Password", helperText = Nothing, validationSpecs = Nothing }
, EntityWrappable ""
[ EntityField fieldPassword
, EntityField fieldRetypePassword
]
, EntityTitle titleAgreementURLs
, EntityField fieldContractPolicy
, EntityField fieldPrivacyPolicy
]
-- * Understand how this can be implemented for "EntityMulti"
-- * I put the message as a Maybe. If "Nothing", single messages for each validation are used. I also removed message for Regex. Not sure if this is the way to go
-- * The "outer" connection is a weak point because it can go out of sync
-- * Maybe validation should move under "EntityNormal" for rxample, so that all fields inside can be referenced directly...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment