gl_gtin/validation
Validation module for GTIN codes.
Implements core validation logic for GTIN codes including format detection, check digit verification, and GTIN normalization.
Types
GTIN format type
pub type GtinFormat {
Gtin8
Gtin12
Gtin13
Gtin14
}
Constructors
-
Gtin8 -
Gtin12 -
Gtin13 -
Gtin14
Error type for validation operations
pub type ValidationError {
InvalidLength(got: Int)
InvalidCheckDigit
InvalidCharacters
InvalidFormat
}
Constructors
-
InvalidLength(got: Int) -
InvalidCheckDigit -
InvalidCharacters -
InvalidFormat
Values
pub fn normalize(code: String) -> Result(String, ValidationError)
Convert a GTIN-13 to GTIN-14 format.
Prepends the indicator digit “1” and recalculates the check digit.
Only works with GTIN-13 codes; other formats return an error. This is
equivalent to normalize_with_indicator(code, 1).
Arguments
code- GTIN-13 string to normalize
Returns
Ok(gtin_14) if successful, Error otherwise.
Examples
normalize("6291041500213")
// -> Ok("16291041500210")
normalize("012345678905")
// -> Error(InvalidFormat)
pub fn normalize_with_indicator(
code: String,
indicator: Int,
) -> Result(String, ValidationError)
Convert a GTIN-13 to GTIN-14 format using a configurable indicator digit.
Prepends the supplied indicator digit (0 through 9) to the 12 data digits of
the GTIN-13 and recalculates the check digit. Leading and trailing whitespace
is trimmed before validation. Only works with GTIN-13 codes; other formats
return an error, and an indicator outside 0 through 9 returns
Error(InvalidFormat).
Arguments
code- GTIN-13 string to normalizeindicator- Indicator digit for the GTIN-14 packaging level (0 through 9)
Returns
Ok(gtin_14) if successful, Error otherwise.
Examples
normalize_with_indicator("6291041500213", 1)
// -> Ok("16291041500210")
normalize_with_indicator("6291041500213", 2)
// -> Ok("26291041500217")
normalize_with_indicator("6291041500213", 10)
// -> Error(InvalidFormat)
pub fn to_gtin12(code: String) -> Result(String, ValidationError)
Convert a GTIN-14 with indicator digit 0 to its base GTIN-12 (UPC-A).
Requires a valid GTIN-14 (14 digits, correct check digit). The indicator
digit (first digit) must be 0; a non-zero indicator returns
Error(InvalidFormat). When the indicator is 0, the base 13-digit code
(after dropping the indicator) must itself be a UPC-A padded with an
implicit leading zero — that is, the second digit of the GTIN-14 must also
be 0. In that case both leading zeros are dropped to yield the 12-digit
UPC-A, preserving the existing check digit without recomputation. When the
base code cannot be represented as a GTIN-12, Error(InvalidFormat) is
returned. Leading and trailing whitespace is trimmed before validation, and
the invalid-input error ordering matches validate.
Arguments
code- GTIN-14 string to down-convert
Returns
Ok(gtin_12) if the indicator digit is 0 and the base code is a UPC-A with an implicit leading zero, Error otherwise.
Examples
to_gtin12("00042100005264")
// -> Ok("042100005264")
to_gtin12("00629104150021") // base is EAN-13, not a UPC-A
// -> Error(InvalidFormat)
pub fn to_gtin13(code: String) -> Result(String, ValidationError)
Convert a GTIN-14 with indicator digit 0 to its base GTIN-13.
Requires a valid GTIN-14 (14 digits, correct check digit). When the leading
indicator digit is 0, the leading 0 is dropped and the existing check
digit is preserved without recomputation, yielding a 13-digit string. A
non-zero indicator returns Error(InvalidFormat). Leading and trailing
whitespace is trimmed before validation, and the invalid-input error
ordering matches validate.
Arguments
code- GTIN-14 string to down-convert
Returns
Ok(gtin_13) if the indicator digit is 0, Error otherwise.
Examples
to_gtin13("06291041500213")
// -> Ok("6291041500213")
to_gtin13("16291041500210")
// -> Error(InvalidFormat)
pub fn validate(
code: String,
) -> Result(GtinFormat, ValidationError)
Validate a GTIN code string.
Checks that the input is a valid GTIN (8, 12, 13, or 14 digits) with a correct check digit. Automatically trims leading and trailing whitespace before validation.
Arguments
code- GTIN string to validate
Returns
Ok(GtinFormat) if valid, Error otherwise.
Examples
validate("6291041500213")
// -> Ok(Gtin13)
validate("012345678905")
// -> Ok(Gtin12)
validate("invalid")
// -> Error(InvalidCharacters)