valtype::luhn(n)
π 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 π
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
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2011 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms