typed-encoding-0.5.2.2: Type safe string transformations

Safe HaskellSafe
LanguageHaskell2010

Data.TypedEncoding.Instances.Restriction.BoundedAlphaNums

Contents

Description

Restrictions "r-ban:" cover commonly used fixed (short) size strings with restricted characters such as GUID, credit card numbers, etc.

Alphanumeric chars are ordered: 0-9 followed by A-Z, followed by a-z. Annotation specifies upper character bound. Any non alpha numeric characters are considered fixed delimiters and need to be present exactly as specified. For example "r-ban:999-99-9999" could be used to describe SSN numbers, @"r-ban:FFFF" would describe strings consisting of 4 hex digits.

This is a simple implementation that converts to String, should be used only with short length data.

Since: 0.2.1.0

Synopsis

Documentation

>>> :set -XOverloadedStrings -XMultiParamTypeClasses -XDataKinds -XTypeApplications
>>> import qualified Data.Text as T
>>> import           Data.TypedEncoding

type family IsBan (s :: Symbol) :: Bool where ... Source #

Equations

IsBan s = AcceptEq (Text "Not ban restriction encoding " :<>: ShowType s) (CmpSymbol (TakeUntil s ":") "r-ban") 

type Ban s = (KnownSymbol s, IsBan s ~ True) Source #

encFBan :: forall s c str. (IsStringR str, Ban s, Algorithm s "r-ban") => Encoding (Either EncodeEx) s "r-ban" c str Source #

>>> runEncoding' encFBan . toEncoding () $ "C59F9FB7-4621-44D9-9020-CE37BF6E2BD1" :: Either EncodeEx (Enc '["r-ban:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"] () T.Text)
Right (UnsafeMkEnc Proxy () "C59F9FB7-4621-44D9-9020-CE37BF6E2BD1")
>>> recreateFAll' @'["r-ban"] . toEncoding () $ "211-22-9934" :: Either RecreateEx (Enc '["r-ban:999-99-9999"] () T.Text)
Right (UnsafeMkEnc Proxy () "211-22-9934")

Decoding

Validation

Implementation

verifyBoundedAlphaNum :: forall s str. (KnownSymbol s, IsStringR str) => Proxy s -> str -> Either String str Source #

>>> verifyBoundedAlphaNum (Proxy :: Proxy "r-ban:FF-FF") (T.pack "12-3E")
Right "12-3E"
>>> verifyBoundedAlphaNum (Proxy :: Proxy "r-ban:FF-FF") (T.pack "1G-3E")
Left "'G' not bounded by 'F'"
>>> verifyBoundedAlphaNum (Proxy :: Proxy "r-ban:FF-FF") (T.pack "13G3E")
Left "'G' not matching '-'"
>>> verifyBoundedAlphaNum (Proxy :: Proxy "r-ban:FFяFF") (T.pack "13я234")
Left "Not ASCII char in annotation '\\1103'"

Orphan instances

(KnownSymbol s, Ban s, Algorithm s "r-ban", IsStringR str, RecreateErr f, Applicative f) => Validate f s "r-ban" c str Source # 
Instance details

Methods

validation :: Validation f s "r-ban" c str Source #

(KnownSymbol s, Restriction s, Algorithm s "r-ban", Applicative f) => Decode f s "r-ban" c str Source # 
Instance details

Methods

decoding :: Decoding f s "r-ban" c str Source #

(Ban s, Algorithm s "r-ban", IsStringR str) => Encode (Either EncodeEx) s "r-ban" c str Source # 
Instance details

Methods

encoding :: Encoding (Either EncodeEx) s "r-ban" c str Source #