summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-04-06 17:01:14 +0200
committerYorhel <git@yorhel.nl>2020-04-06 17:01:17 +0200
commit91c5ca72e9c90c5a60f71e53a68c980094a59da8 (patch)
tree18fd78ab58ae08440ed9e2f70335851c51093a9e
parent6b04feb79ced4571150a64c19fd70539aa4596fb (diff)
Release::Edit: Consider GTIN code as a string for validation/editing
Problem is that the 'uint' validation does not allow leading zeros, which are very valid as part of GTIN codes, thus resulting in an error when validating a normalized GTIN code.
-rw-r--r--elm/Lib/Util.elm8
-rw-r--r--elm/ReleaseEdit.elm16
-rw-r--r--lib/VNDBUtil.pm1
-rw-r--r--lib/VNWeb/Validation.pm2
4 files changed, 12 insertions, 15 deletions
diff --git a/elm/Lib/Util.elm b/elm/Lib/Util.elm
index a40a997c..f840d003 100644
--- a/elm/Lib/Util.elm
+++ b/elm/Lib/Util.elm
@@ -42,12 +42,8 @@ selfCmd : msg -> Cmd msg
selfCmd m = Task.perform (always m) (Task.succeed True)
-formatGtin : Int -> String
-formatGtin n = if n == 0 then "" else String.fromInt n |> String.padLeft 12 '0'
-
-
-- Based on VNDBUtil::gtintype()
-validateGtin : String -> Int
+validateGtin : String -> Bool
validateGtin =
let check = String.fromInt
>> String.reverse
@@ -60,4 +56,4 @@ validateGtin =
|| (n >= 2000000000000 && n < 3000000000000)
|| n >= 9770000000000
|| modBy 10 (check n) /= 0
- in String.filter Char.isDigit >> String.toInt >> Maybe.andThen (\n -> if inval n then Nothing else Just n) >> Maybe.withDefault 0
+ in String.filter Char.isDigit >> String.toInt >> Maybe.map (not << inval) >> Maybe.withDefault False
diff --git a/elm/ReleaseEdit.elm b/elm/ReleaseEdit.elm
index 765bf8bd..275052d8 100644
--- a/elm/ReleaseEdit.elm
+++ b/elm/ReleaseEdit.elm
@@ -43,8 +43,8 @@ type alias Model =
, plat : Set.Set String
, platDd : DD.Config Msg
, media : List GRE.RecvMedia
- , gtinInput : String
- , gtin : Int
+ , gtin : String
+ , gtinValid : Bool
, catalog : String
, released : D.RDate
, minage : Int
@@ -92,8 +92,8 @@ init d =
, plat = Set.fromList <| List.map (\e -> e.platform) d.platforms
, platDd = DD.init "platforms" PlatOpen
, media = List.map (\m -> { m | qty = if m.qty == 0 then 1 else m.qty }) d.media
- , gtinInput = formatGtin d.gtin
- , gtin = d.gtin
+ , gtin = String.padLeft 12 '0' d.gtin
+ , gtinValid = True
, catalog = d.catalog
, released = d.released
, minage = d.minage
@@ -210,7 +210,7 @@ update msg model =
MediaType n s -> ({ model | media = if s /= "unk" && n == List.length model.media then model.media ++ [{medium = s, qty = 1}] else modidx n (\m -> { m | medium = s }) model.media }, Cmd.none)
MediaQty n i -> ({ model | media = modidx n (\m -> { m | qty = i }) model.media }, Cmd.none)
MediaDel i -> ({ model | media = delidx i model.media }, Cmd.none)
- Gtin s -> ({ model | gtinInput = s, gtin = validateGtin s }, Cmd.none)
+ Gtin s -> ({ model | gtin = s, gtinValid = s == "" || validateGtin s }, Cmd.none)
Catalog s -> ({ model | catalog = s }, Cmd.none)
Released d -> ({ model | released = d }, Cmd.none)
Minage i -> ({ model | minage = i }, Cmd.none)
@@ -262,7 +262,7 @@ isValid model = not
( model.title == model.original
|| Set.isEmpty model.lang
|| hasDuplicates (List.map (\m -> (m.medium, m.qty)) model.media)
- || (model.gtinInput /= "" && model.gtin == 0)
+ || not model.gtinValid
|| List.isEmpty model.vn
)
@@ -333,8 +333,8 @@ viewGen model =
, tr [ class "newpart" ] [ td [ colspan 2 ] [ text "External identifiers & links" ] ]
, formField "gtin::JAN/UPC/EAN"
- [ inputText "gtin" model.gtinInput Gtin [pattern "[0-9]+"]
- , if model.gtinInput /= "" && model.gtin == 0 then b [ class "standout" ] [ text "Invalid GTIN code" ] else text ""
+ [ inputText "gtin" model.gtin Gtin [pattern "[0-9]+"]
+ , if not model.gtinValid then b [ class "standout" ] [ text "Invalid GTIN code" ] else text ""
]
, formField "catalog::Catalog number" [ inputText "catalog" model.catalog Catalog GRE.valCatalog ]
, formField "website::Website" [ inputText "website" model.website Website (style "width" "500px" :: GRE.valWebsite) ]
diff --git a/lib/VNDBUtil.pm b/lib/VNDBUtil.pm
index 5d7850bc..80d45fc6 100644
--- a/lib/VNDBUtil.pm
+++ b/lib/VNDBUtil.pm
@@ -23,6 +23,7 @@ sub shorten {
# Also 'normalizes' the first argument in place
sub gtintype {
$_[0] =~ s/[^\d]+//g;
+ $_[0] =~ s/^0+//;
return undef if $_[0] !~ /^[0-9]{10,13}$/; # I've yet to see a UPC code shorter than 10 digits assigned to a game
$_[0] = ('0'x(12-length $_[0])) . $_[0] if length($_[0]) < 12; # pad with zeros to GTIN-12
my $c = shift;
diff --git a/lib/VNWeb/Validation.pm b/lib/VNWeb/Validation.pm
index f4508d6c..3467c013 100644
--- a/lib/VNWeb/Validation.pm
+++ b/lib/VNWeb/Validation.pm
@@ -30,7 +30,7 @@ TUWF::set custom_validations => {
username => { regex => qr/^(?!-*[a-z][0-9]+-*$)[a-z0-9-]*$/, minlength => 2, maxlength => 15 },
password => { length => [ 4, 500 ] },
language => { enum => \%LANGUAGE },
- gtin => { uint => 1, func => sub { $_[0] eq 0 || gtintype($_[0]) } },
+ gtin => { func => sub { $_[0] = 0 if !length $_[0]; $_[0] eq 0 || gtintype($_[0]) } },
rdate => { uint => 1, func => \&_validate_rdate },
# Accepts a user-entered vote string (or '-' or empty) and converts that into a DB vote number (or undef) - opposite of fmtvote()
vnvote => { required => 0, default => undef, regex => qr/^(?:|-|[1-9]|10|[1-9]\.[0-9]|10\.0)$/, func => sub { $_[0] = $_[0] eq '-' ? undef : 10*$_[0]; 1 } },