arktypea
arktype11mo ago
24 replies
Maxiviper117

Key validation on arbitrary number of properties.

I'm working on validating an object where:

- Some property keys are known and expected.
- Other extra/unknown keys are allowed only if they follow a specific pattern.

Pattern for extra keys:

Keys should match the format:
metafield.<namespace>.<key>


Where:
-
namespace
and
key
can contain alphanumeric characters, underscores, or hyphens.

Here's the regex I'm using:
const regex = /^metafield\.[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+$/;


Example payload:

{
    "knownkey": "value",
    "metafield.test.key": "value",
    "metafield.test.key2": "value",
    "metafield.test.key3": "value",
    "metafield.test.key4": "value"
}


Question:

How can I define a Arktype type that allows:
- Some known, explicitly typed keys.
- And any number of extra keys that match the regex pattern above?
Was this page helpful?