Skip to content

Fix duplicate singleton subtag being treated as valid - #26

Open
youdie006 wants to merge 1 commit into
wooorm:mainfrom
youdie006:fix/23-duplicate-singleton
Open

Fix duplicate singleton subtag being treated as valid#26
youdie006 wants to merge 1 commit into
wooorm:mainfrom
youdie006:fix/23-duplicate-singleton

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #23.

Problem

parse('en-a-bbb-a-ccc') returns a fully valid schema even though the singleton a appears twice. Per RFC 5646, each singleton subtag MUST appear at most once (other than in a private-use sequence), so this tag is invalid.

Root cause

The extensions loop in lib/parse.js pushes each extension without ever checking whether its one-character singleton was already used, so a tag with the same singleton twice parses as valid -- no warning, and the end-of-input check passes.

Fix

Track the singletons seen while walking the extensions loop; on a repeat, return the existing fail(..., 7, 'Duplicate singleton, singletons must not be repeated'). This reuses the same fail() / warning-code convention the sibling spec violations already use (empty extension, superfluous content), and adds a warning -- matching the steer in #23 that "a warning is probably a good idea at least". Warning code 7 is added to the readme table.

The check is scoped to extension singletons only, so cases that stay valid per RFC are unaffected and covered by regression tests:

  • en-a-bbb-x-a-ccc -- the second a is in a private-use sequence (valid)
  • en-a-bbb-t-ccc -- two different singletons (valid)
  • de-DE-u-co-phonebk -- a normal extension (valid)

Verification

  • Red/green: reverting the fix makes the new Duplicate singleton test fail (not ok); with the fix, all tests pass.
  • npm run test-api: 10/10 pass.
  • Coverage gate c8 --100: 100% across all files.
  • tsc build, 100% type-coverage, prettier, and xo all clean.

This change was written with AI assistance and reviewed by me before submitting.

Per RFC 5646 a singleton subtag must appear at most once (outside a
private-use sequence), but the extensions loop in parse.js never checked
for a repeated singleton, so `en-a-bbb-a-ccc` parsed as fully valid with
no warning.

Track the singletons seen while walking the extensions loop and return
the existing fail(..., 7, 'Duplicate singleton, singletons must not be
repeated') on a repeat, reusing the same warning-code convention the
sibling spec violations (empty extension, superfluous content) already
use. The check is scoped to extension singletons, so private-use
duplicates and different singletons stay valid.

Fixes wooorm#23.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Singleton subtag appearing twice is considered valid

1 participant