Last active
May 28, 2018 19:26
-
-
Save jkachmar/3c457e0efca9956046370c2244660237 to your computer and use it in GitHub Desktop.
LambdaConf 2018 - Exercise 1.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import Control.Monad.Eff.Console (log, logShow) | |
import Data.Array (fromFoldable) | |
import Data.List.NonEmpty (NonEmptyList, singleton) | |
import Data.String (null, length, toLower, toUpper) | |
import Data.Generic.Rep (class Generic) | |
import Data.Generic.Rep.Show (genericShow) | |
import Data.Validation.Semigroup (V, invalid, unV) | |
import TryPureScript (render, withConsole) | |
-------------------------------------------------------------------------------- | |
-- EXERCISE 1.2 - REPLACE `?solution` WITH YOUR SOLUTION! | |
-- | Validate that an input string contains at least one uppercase character | |
validateContainsUppercase | |
:: String | |
-> V (NonEmptyList InvalidPrimitive) String | |
validateContainsUppercase input = ?solution | |
-------------------------------------------------------------------------------- | |
main = render =<< withConsole do | |
let validTest = validateContainsUppercase "Hello!" | |
logShow (printValidation validTest) | |
log "\n" | |
let invalidTest = validateContainsUppercase "hello" | |
logShow (printValidation invalidTest) | |
-------------------------------------------------------------------------------- | |
-- TYPES AND FUNCTIONS DEFINED EARLIER IN THE PRESENTATION | |
-- | Type of validation errors encountered when validating primitive input | |
data InvalidPrimitive | |
= EmptyField | |
| InvalidEmail String | |
| TooShort Int Int | |
| TooLong Int Int | |
| NoLowercase String | |
| NoUppercase String | |
-- | Validate that an input string is at least as long as some given `Int` | |
validateMinimumLength | |
:: String | |
-> Int | |
-> V (NonEmptyList InvalidPrimitive) String | |
validateMinimumLength input minLength | |
| (length input) < minLength = invalid (singleton (TooShort (length input) minLength)) | |
| otherwise = pure input | |
-- | Validate that an input string contains at least one lowercase character | |
validateContainsLowercase | |
:: String | |
-> V (NonEmptyList InvalidPrimitive) String | |
validateContainsLowercase input | |
| (toUpper input) == input = invalid (singleton (NoLowercase input)) | |
| otherwise = pure input | |
--------------------------------------------------------------------------------- | |
-- !!! BOILERPLATE TO MAKE EVERYTHING A LITTLE EASIER TO WORK WITH !!! | |
--------------------------------------------------------------------------------- | |
-- | Helper function to print validations | |
printValidation | |
:: forall err result | |
. Show err | |
=> V (NonEmptyList err) String | |
-> String | |
printValidation = | |
unV (show <<< fromFoldable) (\result -> "Valid: " <> result) | |
-- | Derive a `Generic` instance for `InvalidPrimitive` so we can get a `Show` | |
-- | instance to print to the console. | |
derive instance genericInvalidPrimitive :: Generic InvalidPrimitive _ | |
-- | Derive `show` for `InvalidPrimitive` using the `Generic` instance. | |
instance showInvalidPrimitive :: Show InvalidPrimitive where | |
show = genericShow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment