valtype::luhn(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– valtype::luhn β€” the "Validation types" era of the manual, bestie edition πŸ’–

okay so this is the manual page for valtype::luhn(n), from the Validation types corner of the world, and I am obsessed. it's a snit validation type that checks a plain number with a LUHN checkdigit. that's the whole vibe. let's get into it πŸŽ€


✨ NAME ✨

valtype::luhn β€” validation for a plain number with a LUHN checkdigit. that's it, that's the name, iconic.


🌸 SYNOPSIS 🌸

package require Tcl 8.5 9

package require snit 2

package require valtype::common

package require valtype::luhn ?1.1?

valtype::luhn validate value

valtype::luhn checkdigit value

like, yes, there are two whole methods and a stack of package requires. we love a low-effort high-impact queen πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so this package implements a snit validation type for a plain number with a LUHN checkdigit. periodt.

and what even is a validation type, you ask? great question bestie 🫢 a validation type is an object you can use to validate Tcl values of one particular kind. for example, snit::integer β€” a validation type that the snit package itself defines β€” is used to validate that some Tcl value is, in fact, an integer. see? same energy.

every validation type has a validate method, and that's the one doing the actual validating. it must take a single argument β€” the value to be validated β€” and then, listen closely:

  • if the value is valid, it does nothing πŸŽ‰
  • if the value is invalid, it throws an error 😭

like this:

valtype::luhn validate .... ;# Does nothing
valtype::luhn validate .... ;# Throws an error (bad luhn checkdigit).

and on success the validate method will always return the validated value. on error it throws with -errorcode INVALID, possibly with extra elements attached that give you more deets about what went wrong. the way it just tells you what's wrong? helpful king behavior πŸ‘‘


πŸ‘‘ API πŸ‘‘

the API this package hands you satisfies the spec of snit validation types, as found in the documentation of Snit's Not Incr Tcl. no cap.

  • valtype::luhn validate value β€” validates the value and returns it, possibly in a canonical form, if it passes. if it does not pass, an error gets thrown. simple as that πŸ’…
  • valtype::luhn checkdigit value β€” computes a check digit for the value. but before it does any of that, the value gets validated β€” except for a checkdigit, that part is skipped. and if the value doesn't pass? no check digit for you, an error gets thrown instead. sorry bestie 😭

πŸ’€ ERROR CODES πŸ’€

like the package description said: when input validation fails, the errors thrown by this package's commands use -errorcode INVALID, which is how they distinguish themselves from the package's own internal errors. clean separation, we stan 🌈

but to give you more detail about why the validation flopped, the -errorCode actually goes beyond that basic code. first it contains a code detailing the type itself β€” here that's LUHN. and then it's followed by values detailing the reason for the failure. here's the full set of -errorCodes this package can throw:

  • INVALID LUHN CHARACTER β€” the input value contained one or more bad characters, i.e. characters that must not occur in the input for it to count as a plain number with a LUHN checkdigit. rude little characters, honestly πŸ™ˆ
  • INVALID LUHN CHECK-DIGIT β€” the check digit of the input value is wrong. this usually signals a data-entry error, with digits transposed, forgotten, whatever. and of course, the input may just be an outright fake too πŸ’€ no cap, sometimes the number is lying to you.

πŸ“ BUGS, IDEAS, FEEDBACK πŸ“

ngl, this document and the package it describes will undoubtedly contain bugs and other problems. like, obviously. please report them in the category valtype of the Tcllib Trackers, over at http://core.tcl.tk/tcllib/reportlist. and please also send any ideas for enhancements you may have for either the package and/or the documentation β€” we want the glow-up ✨

when you're proposing code changes, please provide unified diffs, i.e. the output of diff -u.

and note further, bestie: attachments are strongly preferred over inlined patches. you can make an attachment by going to the Edit form of the ticket right after it's created, and then using the left-most button in the secondary navigation bar. that's the move 🫢


⭐ KEYWORDS ⭐

Checking, Testing, Type checking, Validation, Value checking, isA, luhn


πŸŽ€ CATEGORY πŸŽ€

Validation, Type checking


Copyright (c) 2011 Andreas Kupries <andreas_kupries@users.sourceforge.net>


okay that's the whole page, you ate it, go validate some numbers responsibly πŸ¦‹βœ¨

this was valtype::luhn(n) from tcllib, version 1.1, and that's the sign-off β€” mwah πŸ’…πŸ’–

πŸ“¦ where this page comes from