Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
validate / validate-gtinOpenRuns on Liberas

GTIN (EAN-8, UPC-A, EAN-13, GTIN-14)

Validates a GS1 GTIN by its check digit. input must be exactly 8, 12, 13 or 14 ASCII digits and nothing else (no spaces, hyphens or trimming), else format; its kind is ean8, upca, ean13 or gtin14 by that length. The last digit must be the GS1 check digit of the others: weights 3, 1, 3, 1, … from the rightmost of the others leftward, check = (10 - sum mod 10) mod 10; else checksum. Valid gives kind and gtin14: the input left-padded with zeros to 14 digits. Leading zeros are kept as given (00012345600012 is a valid gtin14, not reinterpreted as a shorter kind); prefixes (GS1 country codes, restricted circulation numbers) are not checked.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init validate-gtin --lang rust
cd validate-gtin && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)33 lines›
validate-gtin.wit
package root:component;

world root {
  export liberas:validate/gtin@0.1.0;
}
package liberas:validate@0.1.0 {
  interface gtin {
    enum gtin-kind {
      ean8,
      upca,
      ean13,
      gtin14,
    }

    record gtin-parts {
      kind: gtin-kind,
      gtin14: string,
    }

    enum gtin-error {
      format,
      checksum,
    }

    variant gtin-check {
      valid(gtin-parts),
      invalid(gtin-error),
    }

    check: func(input: string) -> gtin-check;
  }
}
Test vectors16›
validate-gtin.cases.json
[
  {
    "args": {
      "input": "4006381333931"
    }
  },
  {
    "args": {
      "input": "4006381333932"
    }
  },
  {
    "args": {
      "input": "036000291452"
    }
  },
  {
    "args": {
      "input": "036000291453"
    }
  },
  {
    "args": {
      "input": "73513537"
    }
  },
  {
    "args": {
      "input": "73513538"
    }
  },
  {
    "args": {
      "input": "10614141000415"
    }
  },
  {
    "args": {
      "input": "00012345600012"
    }
  },
  {
    "args": {
      "input": "0000000000000"
    }
  },
  {
    "args": {
      "input": "400638133393"
    }
  },
  {
    "args": {
      "input": "4006381333931 "
    }
  },
  {
    "args": {
      "input": "4006-381333931"
    }
  },
  {
    "args": {
      "input": "40063813339310"
    }
  },
  {
    "args": {
      "input": "123456789012345"
    }
  },
  {
    "args": {
      "input": ""
    }
  },
  {
    "args": {
      "input": "4006381333931"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 16 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 1,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.