Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahstro committed Jan 3, 2018
0 parents commit 8bc4f06
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
elm-stuff/
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
127 changes: 127 additions & 0 deletions src/Validate/SSN/Swedish.elm
Original file line number Diff line number Diff line change
@@ -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})$"
54 changes: 54 additions & 0 deletions tests/SwedishTests.elm
Original file line number Diff line number Diff line change
@@ -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)
]
]
18 changes: 18 additions & 0 deletions tests/elm-package.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 8bc4f06

Please sign in to comment.