Validate login (alphanum), email, password (vchar).

This commit is contained in:
Philippe Pittoli 2024-02-10 20:28:59 +01:00
parent dc7ee7d250
commit 9a19462a99
3 changed files with 87 additions and 2 deletions

View File

@ -20,7 +20,7 @@ data Error
= ParsingError (G.Error EmailParsingError)
min_email_size :: Int
min_email_size = 20
min_email_size = 5
max_email_size :: Int
max_email_size = 100
@ -41,6 +41,5 @@ parse_full_email = do
parserEmail :: String -> V (Array Error) String
parserEmail str = parse parse_full_email str ParsingError
-- | TODO
email :: String -> Either (Array Error) String
email s = toEither $ parserEmail s

View File

@ -0,0 +1,43 @@
module App.Validation.Login where
import Prelude
import Control.Alt ((<|>))
import Data.Either (Either(..))
import Data.String.CodeUnits as CU
import Data.Maybe (Maybe(..))
import Data.Validation.Semigroup (V, invalid, toEither)
import GenericParser.RFC5234 (alpha, digit)
import GenericParser.SomeParsers as SomeParsers
import GenericParser.Parser as G
data LoginParsingError
= CannotParse
| CannotEntirelyParse
| Size Int Int Int
data Error
= ParsingError (G.Error LoginParsingError)
min_login_size :: Int
min_login_size = 1
max_login_size :: Int
max_login_size = 50
parse :: forall e v. G.Parser e v -> String -> ((G.Error e) -> Error) -> V (Array Error) v
parse (G.Parser p) str c = case p { string: str, position: 0 } of
Left x -> invalid $ [c x]
Right x -> pure x.result
login_parser :: G.Parser LoginParsingError String
login_parser = do
l <- G.many1 (alpha <|> digit) <|> G.Parser \i -> G.failureError i.position (Just CannotParse)
_ <- SomeParsers.eof <|> G.Parser \i -> G.failureError i.position (Just CannotEntirelyParse)
pos <- G.current_position
if pos < min_login_size || pos > max_login_size
then G.Parser \i -> G.failureError i.position (Just $ Size min_login_size max_login_size pos)
else pure $ CU.fromCharArray l
login :: String -> Either (Array Error) String
login s = toEither $ parse login_parser s ParsingError

View File

@ -0,0 +1,43 @@
module App.Validation.Password where
import Prelude
import Control.Alt ((<|>))
import Data.Either (Either(..))
import Data.String.CodeUnits as CU
import Data.Maybe (Maybe(..))
import Data.Validation.Semigroup (V, invalid, toEither)
import GenericParser.RFC5234 (vchar)
import GenericParser.SomeParsers as SomeParsers
import GenericParser.Parser as G
data PasswordParsingError
= CannotParse
| CannotEntirelyParse
| Size Int Int Int
data Error
= ParsingError (G.Error PasswordParsingError)
min_password_size :: Int
min_password_size = 1
max_password_size :: Int
max_password_size = 100
parse :: forall e v. G.Parser e v -> String -> ((G.Error e) -> Error) -> V (Array Error) v
parse (G.Parser p) str c = case p { string: str, position: 0 } of
Left x -> invalid $ [c x]
Right x -> pure x.result
password_parser :: G.Parser PasswordParsingError String
password_parser = do
l <- G.many1 vchar <|> G.Parser \i -> G.failureError i.position (Just CannotParse)
_ <- SomeParsers.eof <|> G.Parser \i -> G.failureError i.position (Just CannotEntirelyParse)
pos <- G.current_position
if pos < min_password_size || pos > max_password_size
then G.Parser \i -> G.failureError i.position (Just $ Size min_password_size max_password_size pos)
else pure $ CU.fromCharArray l
password :: String -> Either (Array Error) String
password s = toEither $ parse password_parser s ParsingError