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.

Arguments

  • code - GTIN-13 string to normalize

Returns

Ok(gtin_14) if successful, Error otherwise.

Examples

normalize("6291041500213")
// -> Ok("16291041500214")

normalize("012345678905")
// -> 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)
Search Document