Skip to content

Commit 85c3a38

Browse files
jonathanlkingmaddie927
authored andcommitted
Add date validator (#56)
1 parent 4c15bb3 commit 85c3a38

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Lumi/Components/Form.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import Lumi.Components.FetchCache as FetchCache
6868
import Lumi.Components.Form.Defaults (formDefaults) as Defaults
6969
import Lumi.Components.Form.Internal (FormBuilder(..), SeqFormBuilder, Tree(..), formBuilder, formBuilder_, invalidate, pruneTree, sequential)
7070
import Lumi.Components.Form.Internal (FormBuilder, SeqFormBuilder, formBuilder, formBuilder_, invalidate, listen, parallel, revalidate, sequential) as Internal
71-
import Lumi.Components.Form.Validation (Validated(..), Validator, _Validated, fromValidated, mustBe, mustEqual, nonEmpty, nonEmptyArray, nonNull, validNumber, validInt, optional, setFresh, setModified, validated, warn) as Validation
71+
import Lumi.Components.Form.Validation (Validated(..), Validator, _Validated, fromValidated, mustBe, mustEqual, nonEmpty, nonEmptyArray, nonNull, validNumber, validInt, validDate, optional, setFresh, setModified, validated, warn) as Validation
7272
import Lumi.Components.Input (alignToInput)
7373
import Lumi.Components.Input as Input
7474
import Lumi.Components.LabeledField (RequiredField(..), labeledField, labeledFieldValidationErrorStyles, labeledFieldValidationWarningStyles)

src/Lumi/Components/Form/Validation.purs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Lumi.Components.Form.Validation
22
( Validator
33
, nonEmpty, nonEmptyArray, nonNull
44
, mustEqual, mustBe
5-
, validNumber, validInt
5+
, validNumber, validInt, validDate
66
, optional
77
, Validated(..)
88
, _Validated, _Fresh, _Modified
@@ -18,7 +18,9 @@ import Prelude
1818
import Data.Array as Array
1919
import Data.Array.NonEmpty (NonEmptyArray)
2020
import Data.Array.NonEmpty (fromArray) as NEA
21+
import Data.Date as Date
2122
import Data.Either (Either(..), either, hush, note)
23+
import Data.Enum (toEnum)
2224
import Data.Eq (class Eq1)
2325
import Data.Foldable (foldMap)
2426
import Data.Int as Int
@@ -29,8 +31,11 @@ import Data.Newtype (un)
2931
import Data.Nullable (notNull)
3032
import Data.Number as Number
3133
import Data.Ord (class Ord1)
34+
import Data.String.Common (split)
3235
import Data.String.NonEmpty (NonEmptyString)
3336
import Data.String.NonEmpty (fromString) as NES
37+
import Data.String.Pattern (Pattern(..))
38+
import Data.Traversable (traverse)
3439
import Heterogeneous.Mapping (class HMap, class MapRecordWithIndex, class Mapping, ConstMapping, hmap, mapping)
3540
import Lumi.Components.Column (column_)
3641
import Lumi.Components.Form.Internal (Forest, FormBuilder(..), Tree(..))
@@ -81,6 +86,16 @@ validNumber name = note (name <> " must be a number.") <<< Number.fromString
8186
validInt :: String -> Validator String Int
8287
validInt name = note (name <> " must be a whole number.") <<< Int.fromString
8388

89+
-- | A `Validator` which verifies that its input can be parsed as a date.
90+
-- | Dates are of the format "YYYY-MM-DD".
91+
validDate :: String -> Validator String Date.Date
92+
validDate name input =
93+
note (name <> " must be a date.") result
94+
where
95+
result = case traverse Int.fromString $ split (Pattern "-") input of
96+
Just [y, m, d] -> join $ Date.exactDate <$> toEnum y <*> toEnum m <*> toEnum d
97+
_ -> Nothing
98+
8499
-- | Modify a `Validator` to accept empty strings in addition to anything it
85100
-- | already accepts. The empty string is mapped to `Nothing`, and any other
86101
-- | valid input is mapped to `Just` the result of the original validator.

0 commit comments

Comments
 (0)