Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions elm.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"type": "package",
"name": "rtfeldman/elm-validate",
"name": "whage/elm-validate",
"summary": "Validate data",
"license": "BSD-3-Clause",
"version": "4.0.1",
"version": "2.0.0",
"exposed-modules": [
"Validate"
],
Expand All @@ -15,4 +15,4 @@
"test-dependencies": {
"elm-explorations/test": "1.0.0 <= v < 2.0.0"
}
}
}
44 changes: 42 additions & 2 deletions src/Validate.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Validate exposing
( Validator, Valid, validate, fromValid
, ifBlank, ifNotInt, ifNotFloat, ifEmptyList, ifEmptyDict, ifEmptySet, ifNothing, ifInvalidEmail, ifTrue, ifFalse, fromErrors
, ifBlank, ifNotInt, ifNotFloat, ifEmptyList, ifEmptyDict, ifEmptySet, ifNoRegexMatch, ifNothing, ifInvalidEmail, ifTrue, ifFalse, fromErrors
, all, any, firstError
, isBlank, isInt, isFloat, isValidEmail
)
Expand Down Expand Up @@ -35,7 +35,7 @@ module Validate exposing

# Creating validators

@docs ifBlank, ifNotInt, ifNotFloat, ifEmptyList, ifEmptyDict, ifEmptySet, ifNothing, ifInvalidEmail, ifTrue, ifFalse, fromErrors
@docs ifBlank, ifNotInt, ifNotFloat, ifEmptyList, ifEmptyDict, ifEmptySet, ifNoRegexMatch, ifNothing, ifInvalidEmail, ifTrue, ifFalse, fromErrors


# Combining validators
Expand Down Expand Up @@ -241,6 +241,46 @@ ifInvalidEmail subjectToEmail errorFromEmail =
Validator getErrors


{-| Return an error if the given pattern is not matched.

import Validate exposing (Validator, ifBlank, ifNotInt)

modelValidator : Validator Model String
modelValidator =
Validate.all
[ ifNoRegexMatch "\\d{4}-\\d{2}-\\d{2}" .patientBirthDate PatientBirthDate
]

-}
ifNoRegexMatch : String -> (subject -> String) -> error -> Validator error subject
ifNoRegexMatch pattern subjectToString error =
let
getErrors subject =
let
inputString =
subjectToString subject
in
if matchesRegex pattern inputString then
[]

else
[ error ]
in
Validator getErrors


matchesRegex : String -> String -> Bool
matchesRegex pattern inputString =
Regex.contains (matchPattern pattern) inputString


matchPattern : String -> Regex
matchPattern pattern =
pattern
|> Regex.fromStringWith { caseInsensitive = True, multiline = False }
|> Maybe.withDefault Regex.never


{-| Create a custom validator, by providing a function that returns a list of
errors given a subject.

Expand Down