diff --git a/elm.json b/elm.json index cef4d35..e7b8079 100644 --- a/elm.json +++ b/elm.json @@ -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" ], @@ -15,4 +15,4 @@ "test-dependencies": { "elm-explorations/test": "1.0.0 <= v < 2.0.0" } -} \ No newline at end of file +} diff --git a/src/Validate.elm b/src/Validate.elm index 5ff3e34..e308688 100644 --- a/src/Validate.elm +++ b/src/Validate.elm @@ -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 ) @@ -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 @@ -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.