Repository navigation
feat: add examples and format patterns to validators (#108) - #258
Merged
Merged
Conversation
Add optional `examples` (valid numbers in compact form) and `formats` (patterns for the format() output, IBAN registry style: n digit, a letter A-Z, c digit or letter, anything else literal) to the Validator interface, and fill them in for every validator, including gen/ean. Examples come from each validator's existing spec fixtures, except az/pin, which has no valid fixture and uses a made-up 10-digit number. Formats were derived from the format() output of those fixtures, of numbers found by mutating them, and of random numbers of every length from 1 to 30 (with the check character repaired), keeping only the ones validate() accepts. Literal text (CHE, MWST, NO ... MVA, PST, GD/HA, ...) is set by hand where the validator requires it. Aliases (cl/run, sk/rc, pt/nipc, me/mk/rs/si jmbg) inherit from the module they spread. src/examples.spec.ts checks, for every validator reachable from index.ts and gen/ean, that it has examples and formats, that each example passes validate(), that compact(example) is the example, and that format(example) matches a declared pattern. Fixes #108
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two optional fields to
Validator, filled in for every validator, so apps can show a hint ("Enter your TFN, e.g. 123 456 782") or build an input mask:examples: valid numbers in compact form.formats: the shapes of theformat()output, in IBAN registry notation:nis a digit,aa letter A-Z,ca digit or letter, anything else literal. For examplebr/cpfisnnn.nnn.nnn-nn.Fixes #108
Changes
src/types.ts: the two fields, optional so third-partyValidatorobjects still type-check.src/index.ts, plusgen/ean): 353 set the data on theirimpl, and 7 aliases (cl/run,sk/rc,pt/nipc,me/mk/rs/sijmbg) inherit it through their existing spread. Eachexport const { ... } = implalso exportsexamplesandformats, sostdnum.XX.y.examplesworks.src/examples.spec.ts: for every validator, checks that examples and formats are non-empty, that each example passesvalidate()and is unchanged bycompact(), and thatformat(example)matches a declared pattern. Breaking an example, a pattern or a missing entry makes it fail.README.md: an "Examples and formats" section.How the data was made
az/pinhas no valid fixture, so it uses a made-up1234567890.format()output of the fixtures, a mutation search from them, and random strings of length 1–30 with the check digit repaired), then merged by separators and length. Literal letters are set by hand only where the validator requires that exact text (ch/uid,ch/vat,at/uid,ca/pst,no/mva,nl/btw,in/gstin,gb/vat, and a few passports); elsewhere every letter position isa, a deliberate superset.Existing bugs found (not fixed here)
co/nit:format('12345678')returns123.456.78-undefined(8- and 9-digit numbers). Those shapes are left out offormats.isValidDateCompactYYYYMMDD/isValidDateCompactYYMMDDaccept some letters, sotw/tax_codevalidates4KGW8O4GGWandde/pwnraccepts a birth date of6Y08125. Their formats describe the intended shape.il/idnr:validate('0')is valid. That shape is left out offormats.ec/ruc: thecompactreturned byvalidate()can differ fromcompact()(0101016905001validates to0101016905).Not verified
mx/rfc'sÑand&;fi/hetu's+century sign and oldie/vat+/*forms weren't checked.at/businessid,li/peid,gt/nit,ro/cif,ro/cui,sm/coe,py/ruc) list only the lengths found from 1 to 30.src/types.ts,src/gb/nino.ts,src/lk/nic.ts).npx prettier --check README.mdfails because of the passport table added in feat(passport): add passport number validators #175, which was already unformatted.kr/rrnARN PR insrc/kr/rrn.ts(different hunks); whichever merges second needs a small conflict fix.