A
arktype2mo ago
Wallce

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.
ArkType
ArkType Playground
TypeScript's 1:1 validator, optimized from editor to runtime
6 Replies
TizzySaurus
TizzySaurus2mo ago
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:
{
index: [
{
signature: {
domain: "string",
pattern: [
{
flags: "i",
rule: "^[\\da-f]{8}-[\\da-f]{4}-4[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$",
meta: "a UUIDv4"
}
]
},
value: [
"number",
"string"
]
}
],
domain: "object"
}
{
index: [
{
signature: {
domain: "string",
pattern: [
{
flags: "i",
rule: "^[\\da-f]{8}-[\\da-f]{4}-4[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$",
meta: "a UUIDv4"
}
]
},
value: [
"number",
"string"
]
}
],
domain: "object"
}
(Which states the signature (i.e. keys) must be a string that matches that pattern, which the provided keys quite clearly aren't...)
TizzySaurus
TizzySaurus2mo ago
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
Wallce
WallceOP2mo ago
Ok, raise a bug here https://github.com/arktypeio/arktype/issues/1481 @TizzySaurus thank you
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...
ssalbdivad
ssalbdivad2mo ago
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
})
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
TizzySaurus
TizzySaurus2mo ago
Ah yes, ofc, I forgot about additional properties :blobsweats: See above for a solution to your problem @Wallce
ssalbdivad
ssalbdivad2mo ago
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...

Did you find this page helpful?