Skip to content

Commit 0a3c481

Browse files
committed
core refactor
1 parent 3e3858e commit 0a3c481

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

lib/polish_validators/iban.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule PolishValidators.Iban do
2424
{ :error, "Invalid length" }
2525
2626
"""
27-
def validate(iban) do
27+
def validate(iban) when is_binary(iban) do
2828
iban_length = replace_prefix(iban, "PL", "")
2929
|> validate_length(26)
3030

@@ -36,6 +36,10 @@ defmodule PolishValidators.Iban do
3636
end
3737
end
3838

39+
def validate(_) do
40+
throw "IBAN must be a string."
41+
end
42+
3943
defp calculate_checksum(iban) do
4044
"#{slice(iban, 2..-1)}2521#{slice(iban, 0..1)}"
4145
|> to_integer

lib/polish_validators/nip.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule PolishValidators.Nip do
2-
import Enum, only: [zip: 1, reduce: 3, map: 2]
2+
import Enum, only: [zip: 1, reduce: 3]
33
import List, only: [last: 1]
44
import PolishValidators.Common
55

@@ -24,7 +24,7 @@ defmodule PolishValidators.Nip do
2424
{ :error, "Invalid length" }
2525
2626
"""
27-
def validate(nip) do
27+
def validate(nip) when is_binary(nip) do
2828
nip_length = validate_length(nip, 10)
2929

3030
case nip_length do
@@ -38,6 +38,10 @@ defmodule PolishValidators.Nip do
3838
end
3939
end
4040

41+
def validate(_) do
42+
throw "NIP must be a string."
43+
end
44+
4145
defp calculate_checksum(nip) do
4246
reduce(nip, 0, &reduce_checksum/2)
4347
|> rem(11)

lib/polish_validators/pesel.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule PolishValidators.Pesel do
2-
import Enum, only: [zip: 1, reduce: 3, map: 2]
2+
import Enum, only: [zip: 1, reduce: 3]
33
import List, only: [last: 1]
44
import PolishValidators.Common
55

@@ -24,8 +24,7 @@ defmodule PolishValidators.Pesel do
2424
{ :error, "Invalid length" }
2525
2626
"""
27-
#def validate(pesel) do#when is_binary(pesel) do
28-
def validate(pesel) do
27+
def validate(pesel) when is_binary(pesel) do
2928
pesel_length = validate_length(pesel, 11)
3029

3130
case pesel_length do
@@ -39,6 +38,10 @@ defmodule PolishValidators.Pesel do
3938
end
4039
end
4140

41+
def validate(_) do
42+
throw "Pesel must be a string."
43+
end
44+
4245
defp calculate_checksum(pesel) do
4346
(10 - (reduce(pesel, 0, &reduce_checksum/2) |> rem(10)))
4447
|> rem(10)
@@ -47,8 +50,4 @@ defmodule PolishValidators.Pesel do
4750
defp reduce_checksum(pesel, acc) do
4851
acc + elem(pesel, 0) * elem(pesel, 1)
4952
end
50-
51-
#def validate(pesel) do
52-
# throw "Pesel must be a string."
53-
#end
5453
end

lib/polish_validators/regon.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule PolishValidators.Regon do
3333
{ :error, "Invalid length" }
3434
3535
"""
36-
def validate(regon) do
36+
def validate(regon) when is_binary(regon) do
3737
regon_length = validate_length(regon, [9, 14])
3838

3939
case regon_length do
@@ -47,6 +47,10 @@ defmodule PolishValidators.Regon do
4747
end
4848
end
4949

50+
def validate(_) do
51+
throw "Regon must be a string."
52+
end
53+
5054
defp weights(regon) do
5155
case length(regon) do
5256
9 -> @weights9

0 commit comments

Comments
 (0)