Skip to content

index signature including literals (e.g. string.uuid) treated as having required keys #1481

Closed

Description

@WZSE

Report a bug

string.uuid.v4 is validating the invalid input as valid. This is same with every other version from v1 to v8.
Weirdly just string.uuid is throwing error but for incorrect keys. (keys not present in actual json)

🔎 Search Terms

  • string.uuid
  • string.uuid.v4
  • dynamic keys

🧩 Context

  • ArkType version: 2.1
  • TypeScript version (5.1+): Playground version
  • Other context you think may be relevant (JS flavor, OS, etc.): N/A

🧑‍💻 Repro

Playground Link: https://arktype.io/playground?code=import%20%7B%20type%20%7D%20from%20%22arktype%22%0A%0Aconst%20Thing%20%3D%20type(%7B%0A%09%22%5Bstring.uuid.v4%5D%22%3A%20%22(number%20%7C%20string)%22%0A%7D)%0A%0Aconst%20out%20%3D%20Thing(%7B%0A%20%20%20%20name%3A%20%22TypeScript%22%2C%0A%20%20%20%20versions%3A%207%0A%7D)%0A

// Paste reproduction code here

Activity

  1. added
    bug
    on Jul 22, 2025
  2. ssalbdivad commented on Jul 22, 2025

    @ssalbdivad
    Member

    Thanks for taking the time to log this!

    The behavior is actually based on how index signatures work in TS.

    When you have a constraint like [string.uuid.v4]: string | number, what this means is:

    all keys that match the uuid v4 pattern must have a string or numeric value

    However, it doesn't prescribe anything for keys that do not match the pattern by default. As a structural type system, TypeScript will always allow extra properties to be specified (unless you're assigning from a literal).

    Luckily in ArkType, there are ways to explicitly set undeclared property behavior for an object or at a config-level:

    import { type } from "arktype"
    
    const Thing = type({
    	"+": "reject",
    	"[string.uuid.v4]": "(number | string)"
    })
    
    // ArkErrors:
    // name must be removed
    // versions must be removed
    const out = Thing({
    	name: "TypeScript",
    	versions: 7
    })

    Playground

    See the docs for configuring this behavior for individual objects here:
    https://arktype.io/docs/objects#properties-undeclared

    Or at a config-level here:
    https://arktype.io/docs/configuration#onundeclaredkey

  3. moved this from To do to Done (merged or closed) in arktypeioon Jul 22, 2025
  4. WZSE commented on Jul 23, 2025

    @WZSE
    Author

    Thank you, but why the behaviour is different for plain string.uuid?

    ArkErrors
    value at ["00000000-0000-0000-0000-000000000000"] must be a number or a string (was missing)
    value at ["ffffffff-ffff-ffff-ffff-ffffffffffff"] must be a number or a string (was missing)
    

    Playground for string.uuid

  5. ssalbdivad commented on Jul 23, 2025

    @ssalbdivad
    Member

    @WZSE Ahh, so it's that behavior that is unintuitive.

    It is actually correct based on how TS treats literal keys in index signatures:

    type Ids = Record<"fff" | "000" | `${number}`>
    // Error: "fff" and "000" are required
    const ids: Ids = {}

    However, this behavior is unintuitive in TypeScript and is extremely unintuitive here as its basically a side effect of the fact that those two UUIDs happen to be expressed as literal values instead of patterns in the validator.

    I'm going to re-open this and use it to track revisiting this behavior, potentially to diverge it from TS and instead translate those keys top optional.

  6. moved this from Done (merged or closed) to To do in arktypeioon Jul 23, 2025
  7. changed the title [-]string.uuid.vx not finding invalid strings.[/-] [+]index signature including literals (e.g. string.uuid) treated as having required keys[/+] on Jul 23, 2025
  8. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    Member

    Fixed in 3.0.0-beta.0 (#1728). Try it with npm i arktype@next.

  9. moved this from To do to Done (merged or closed) in arktypeioon Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions