From 8bc4f063206e81cea20b2842021b05d0a20e0902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Str=C3=B6mkvist?= Date: Wed, 3 Jan 2018 10:28:25 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + LICENSE | 11 +++ README.md | 31 +++++++++ elm-package.json | 17 +++++ src/Validate/SSN/Swedish.elm | 127 +++++++++++++++++++++++++++++++++++ tests/SwedishTests.elm | 54 +++++++++++++++ tests/elm-package.json | 18 +++++ 7 files changed, 260 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 elm-package.json create mode 100644 src/Validate/SSN/Swedish.elm create mode 100644 tests/SwedishTests.elm create mode 100644 tests/elm-package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec5e17a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +elm-stuff/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..523518f --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2018 Anton Strömkvist + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cfcffbc --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# elm-ssn-validation + +Validate (currently only Swedish) social security numbers (personnummer). + +## Installation + +```sh +elm-package install ahstro/elm-ssn-validation +``` + +## Usage + +```elm +import Validation.SSN.Swedish as SSN + + +case SSN.validate "811218-9876" of + Ok ssn -> + ssn ++ " is valid" -- "811218-9876 is valid" + Err error -> + error + + +if SSN.isValid "811218-9876" then + "Yay" +else + "Nay" + +``` + +More examples are available in the [/tests](https://github.com/ahstro/elm-ssn-validation/tree/master/tests) folder. diff --git a/elm-package.json b/elm-package.json new file mode 100644 index 0000000..cc71d98 --- /dev/null +++ b/elm-package.json @@ -0,0 +1,17 @@ +{ + "version": "1.0.0", + "summary": "Validate social security numbers", + "repository": "https://github.com/ahstro/elm-ssn-validation.git", + "license": "BSD3", + "source-directories": [ + "src/" + ], + "exposed-modules": [ + "Validate.SSN.Swedish" + ], + "dependencies": { + "ahstro/elm-luhn": "1.0.0 <= v < 2.0.0", + "elm-lang/core": "5.0.0 <= v < 6.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +} diff --git a/src/Validate/SSN/Swedish.elm b/src/Validate/SSN/Swedish.elm new file mode 100644 index 0000000..e7d80ac --- /dev/null +++ b/src/Validate/SSN/Swedish.elm @@ -0,0 +1,127 @@ +module Validate.SSN.Swedish exposing (isValid, validate) + +{-| This library allows you to validate Swedish social security +numbers ("personnummer"). + +It supports the following formats: + + - `"8112189876"` + - `"811218-9876"` + - `"811218+9876"` + - `"198112189876"` + - `"19811218-9876"` + - `"19811218+9876"` + + +# Validation + +@docs validate, isValid + +-} + +import Luhn +import Regex exposing (Regex, regex) + + +{-| Accepts a string and returns a `Result String String` indicating +whether the string was a valid Swedish SSN ("personnummer"). + + validate "811218-9876" == Ok "811218-9876" + validate "811218" == Err "Invalid Swedish SSN" + +-} +validate : String -> Result String String +validate ssn = + let + match = + case Regex.find Regex.All ssnRegex ssn of + validSSN :: [] -> + Ok validSSN + + _ -> + Err "Invalid Swedish SSN" + in + match + |> Result.map .submatches + |> Result.andThen normalize + |> Result.andThen Luhn.validate + |> Result.map (\_ -> ssn) + + +{-| Same as `validate` but returns a `Bool` instead, for easy use in +`if`-expressions. + + isValid "811218-9876" == True + isValid "811218" == False + +-} +isValid : String -> Bool +isValid ssn = + case validate ssn of + Ok _ -> + True + + Err _ -> + False + + +{-| Takes the `.submatches` of a `ssnRegex` match and returns a either +an `Ok` with a normalized string (with the "YYMMDDXXXX" format) or an +`Err`. +-} +normalize : List (Maybe String) -> Result String String +normalize ssn = + case ssn of + (Just year) :: (Just month) :: (Just day) :: (Just controlDigits) :: [] -> + Ok <| String.right 2 year ++ month ++ day ++ controlDigits + + _ -> + Err "Invalid input to normalize" + + +{-| Regex for valid inputs. + +Matches the following formats: + + - `"8112189876"` + - `"811218-9876"` + - `"811218+9876"` + - `"198112189876"` + - `"19811218-9876"` + - `"19811218+9876"` + +The regex is made up of seven parts + + - `^` + + Denotes the start of the line + + - `((?:\d{2})?\d{2})` + + The year part. Matches two optional digits, e.g. "19", and two + obligatory digits, e.g. "81". + + - `(\d{2})` + + The month part. Matches two obligatory digits, e.g. "12". + + - `(\d{2})` + + The day part. Matches two obligatory digits, e.g. "18". + + - `[-+]?` + + The separator part. Matches one optional separator, e.g. "-". + + - `(\d{4})` + + The control-digits part. Matches four obligatory digits, e.g. "9876". + + - `$` + + Denotes the start of the line + +-} +ssnRegex : Regex +ssnRegex = + regex "^((?:\\d{2})?\\d{2})(\\d{2})(\\d{2})[-+]?(\\d{4})$" diff --git a/tests/SwedishTests.elm b/tests/SwedishTests.elm new file mode 100644 index 0000000..8abb9f3 --- /dev/null +++ b/tests/SwedishTests.elm @@ -0,0 +1,54 @@ +module SwedishTests exposing (suite) + +import Expect +import Result.Extra exposing (isErr, isOk) +import Test exposing (Test, describe, test) +import Validate.SSN.Swedish as SSN + + +validSSNs : List String +validSSNs = + [ "8112189876" + , "811218-9876" + , "811218+9876" + , "198112189876" + , "19811218-9876" + , "19811218+9876" + ] + + +invalidSSNs : List String +invalidSSNs = + [ "98112189876" + , "9811218-9876" + , "9811218+9876" + , "19811218" + , "19811218-" + , "19811218+" + , "811218" + , "811218-" + , "811218+" + , "asdf+" + , "asdf" + , "1" + , "0123456789" + , "" + ] + + +suite : Test +suite = + describe "Validate.SSN.Swedish" + [ describe "validate" + [ test "Returns Ok for valid SSNs" <| + \_ -> + Expect.equal + (List.map (isOk << SSN.validate) validSSNs) + (List.map (always True) validSSNs) + , test "Returns Err for invalid SSNs" <| + \_ -> + Expect.equal + (List.map (isErr << SSN.validate) invalidSSNs) + (List.map (always True) invalidSSNs) + ] + ] diff --git a/tests/elm-package.json b/tests/elm-package.json new file mode 100644 index 0000000..b407774 --- /dev/null +++ b/tests/elm-package.json @@ -0,0 +1,18 @@ +{ + "version": "1.0.0", + "summary": "Validate social security numbers", + "repository": "https://github.com/ahstro/elm-ssn-validation.git", + "license": "BSD3", + "source-directories": [ + "../src/", + "." + ], + "exposed-modules": [], + "dependencies": { + "ahstro/elm-luhn": "1.0.0 <= v < 2.0.0", + "elm-community/elm-test": "4.0.0 <= v < 5.0.0", + "elm-community/result-extra": "2.2.0 <= v < 3.0.0", + "elm-lang/core": "5.0.0 <= v < 6.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +}