feat(utilities): add validateKvsKey with named illegal-character diagnostics - #666
Draft
DaveHanns wants to merge 1 commit into
Draft
feat(utilities): add validateKvsKey with named illegal-character diagnostics#666DaveHanns wants to merge 1 commit into
DaveHanns wants to merge 1 commit into
Conversation
…nostics
Add `validateKvsKey` / `assertValidKvsKey` in `@apify/utilities` so callers
that reject key-value store record keys can produce a message that names
the actual problem instead of restating the entire allowed charset plus
length limit.
When a key like `price:yamaha-ysp-5600` is rejected, the current pattern
for callers of `KEY_VALUE_STORE_KEY_REGEX` in `@apify/consts` is to say
"must be at most 256 characters long and only contain the following
characters: a-zA-Z0-9!-_.'()" — which forces the user to diff their key
against the charset by hand. `validateKvsKey` returns the deduplicated
list of illegal characters (in order of first appearance), a length flag,
and an empty flag, plus a composed `message` like:
Key-value store record key is invalid (illegal character(s):
":" (colon)). Allowed characters are a-zA-Z0-9 and !-_.'(),
max length 256.
Length and charset problems are reported independently, so keys that
exceed 256 chars AND contain a bad char get both diagnostics; keys with
only a bad char no longer falsely blame length.
Surfaced during an evaluation of Apify surfaces for agent-driven Actor
development.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
DaveHanns
marked this pull request as draft
July 5, 2026 11:21
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.
Summary
Add
validateKvsKeyandassertValidKvsKeyin@apify/utilitiesso callers that reject key-value store record keys can produce an error message that names the actual problem instead of restating the entire allowed charset plus the length limit.Motivation
The current pattern for validating a KVS record key is to test it against
KEY_VALUE_STORE_KEY_REGEXin@apify/constsand, on failure, throw a message like:That message conflates two independent checks (charset AND length) and forces the caller of the API to diff their key against the allowed set by hand. In the example above, the length is fine and the only problem is a single colon — but the user has to figure that out themselves. This is especially painful when the key contains an invisible character (a stray tab, a trailing newline, a non-breaking space).
What this change adds
validateKvsKey(key)returns a discriminated union:{ valid: true }.{ valid: false, illegalCharacters, tooLong, empty, message }whereillegalCharactersis the deduplicated list of characters not ina-zA-Z0-9!-_.'(), in order of first appearance;tooLongistruewhenkey.length > 256;emptyistruewhen the key is the empty string;messageis a ready-to-use human-readable string, e.g.Length and charset are reported independently: a key that both contains a bad character AND exceeds 256 chars now gets both diagnostics; a key with only a bad character no longer falsely blames length.
Whitespace and control characters get a friendly name (
(space),(tab),(newline),U+0007…) so the failure is unambiguous even when the offending char is invisible in a terminal.assertValidKvsKey(key)is a thin throwing wrapper for callers that just want to guard.Test plan
test/kvs_key_validator.test.tscovers:it's-fine,INPUT,file.json,a,0, and a key at exactly 256 chars).empty: true, no length/charset noise.price:yamaha-ysp-5600) →illegalCharacters: [':'], message containsillegal character(s): ":" (colon), message does NOT match/length \d+ >/.illegalCharacters: [], message containslength 257 > 256, no charset noise.(space),(tab),(newline)).assertValidKvsKeythrowsTypeErrorwith the composed message.Ran locally:
Follow-ups (not in this PR)
Consumers that currently reject KVS keys by hand-rolling their own error message (notably the storage error in the Apify core API) can be migrated to
validateKvsKeyin a separate change — this PR only adds the utility and its tests.Surfaced during an evaluation of Apify surfaces for agent-driven Actor development.