dynamic key using string.uuid.v4 validating invalid strings.
Hi,
New to ArkType, so bear with me if this is answered, I couldn't find the relavent information on this topic. Here the link from playground: https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%257B%250A%2509%2522%255Bstring.uuid.v4%255D%2522%253A%2520%2522%28number%2520%257C%2520string%29%2522%250A%257D%29%250A%250Aconst%2520out%2520%253D%2520Thing%28%257B%250A%2520%2520%2520%2520name%253A%2520%2522TypeScript%2522%252C%250A%2520%2520%2520%2520versions%253A%25207%250A%257D%29%250A I would expect the out to show ArkErrors, but it's showing json output. This gives errors for plain "string.uuid" but shows keys like 0000..00 & ffff...fff as invalid, while not accounting the actual incorrect keys. I assume I'm missing something. Please let me know the correct syntax if this is not the right way.
New to ArkType, so bear with me if this is answered, I couldn't find the relavent information on this topic. Here the link from playground: https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%257B%250A%2509%2522%255Bstring.uuid.v4%255D%2522%253A%2520%2522%28number%2520%257C%2520string%29%2522%250A%257D%29%250A%250Aconst%2520out%2520%253D%2520Thing%28%257B%250A%2520%2520%2520%2520name%253A%2520%2522TypeScript%2522%252C%250A%2520%2520%2520%2520versions%253A%25207%250A%257D%29%250A I would expect the out to show ArkErrors, but it's showing json output. This gives errors for plain "string.uuid" but shows keys like 0000..00 & ffff...fff as invalid, while not accounting the actual incorrect keys. I assume I'm missing something. Please let me know the correct syntax if this is not the right way.
6 Replies
Hmm, this actually seems to be a bug in ArkType...
If you do
const out = Thing.json;
(where .json
is the internal ArkType schema representation of the type) then you can see it is parsing the syntax as-expected, so it's just the runtime validation being iffy.
For reference, this is the .json
:
(Which states the signature (i.e. keys) must be a string that matches that pattern, which the provided keys quite clearly aren't...)Could you please raise a bug report on the GitHub repo: https://github.com/arktypeio/arktype
GitHub
GitHub - arktypeio/arktype: TypeScript's 1:1 validator, optimized f...
TypeScript's 1:1 validator, optimized from editor to runtime - arktypeio/arktype
GitHub
string.uuid.vx not finding invalid strings. · Issue #1481 · arkty...
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 n...
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:
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#onundeclaredkeyAh yes, ofc, I forgot about additional properties :blobsweats:
See above for a solution to your problem @Wallce
Actually, @Wallce did point out something unintuitive here which is that those literal keys in the base
string.uuid
are treated as required, which I'd like to revisit:
https://github.com/arktypeio/arktype/issues/1481#issuecomment-3109444308
But yeah the behavior on string.uuid.v4
is expected.GitHub
index signature including literals (e.g. string.uuid) treated as ha...
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 n...