From 6fca021c3aac04bbfd01e186708788ab0188f2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Sallai?= Date: Mon, 10 Feb 2020 15:12:22 +0100 Subject: [PATCH 1/2] Add `ifNoRegexMatch` function for validating against a custom regular expression --- elm.json | 6 +++--- src/Validate.elm | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/elm.json b/elm.json index cef4d35..bdf2b0b 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": "1.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..ec441ac 100644 --- a/src/Validate.elm +++ b/src/Validate.elm @@ -241,6 +241,35 @@ ifInvalidEmail subjectToEmail errorFromEmail = Validator getErrors +ifNoRegexMatch : String -> (subject -> String) -> (String -> error) -> Validator error subject +ifNoRegexMatch pattern subjectToString error = + let + getErrors subject = + let + inputString = + subjectToString subject + in + if matchesRegex pattern inputString then + [] + + else + [ error inputString ] + 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. From cf25cfb3434c40cedd1c6596930a49099aa98cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Sallai?= Date: Mon, 10 Feb 2020 15:31:50 +0100 Subject: [PATCH 2/2] Export ifNoRegexMatch function --- elm.json | 2 +- src/Validate.elm | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/elm.json b/elm.json index bdf2b0b..e7b8079 100644 --- a/elm.json +++ b/elm.json @@ -3,7 +3,7 @@ "name": "whage/elm-validate", "summary": "Validate data", "license": "BSD-3-Clause", - "version": "1.0.0", + "version": "2.0.0", "exposed-modules": [ "Validate" ], diff --git a/src/Validate.elm b/src/Validate.elm index ec441ac..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,7 +241,18 @@ ifInvalidEmail subjectToEmail errorFromEmail = Validator getErrors -ifNoRegexMatch : String -> (subject -> String) -> (String -> error) -> Validator error subject +{-| 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 = @@ -253,7 +264,7 @@ ifNoRegexMatch pattern subjectToString error = [] else - [ error inputString ] + [ error ] in Validator getErrors