ParseError: An unordered union of a type including a morph and a type with overlapping input is indeterminate #1094
-
I want to load the following structure (simplified): defs:
- baz: quo
- qux:
default: foo The loaded data shall be: {
"defs": [
{ "id": "baz", "default": "quo" },
{ "id": "qux", "default": "foo" }
]
} The following arktype definition fails with an error: import { ArkErrors, scope } from 'arktype';
type ArgDefSchema = {
id: string,
default?: string
};
const schema = scope({
id: 'string',
argdef_default: [
'Record<id, string>', '=>',
(entry, ctx): ArgDefSchema => {
const keys = Object.keys(entry);
if (keys.length !== 1) {
ctx.error(`definition must have exactly one key, got "${keys.join(', ')}"`);
}
const id = keys.shift() as string;
return {
id,
default: entry[id]
};
}
],
argdef_entry: {
'default?': 'string'
},
argdef_full: [
'Record<id, argdef_entry>', '=>',
(entry, ctx): ArgDefSchema => {
const keys = Object.keys(entry);
if (keys.length !== 1) {
ctx.error(`definition must have exactly one key, got "${keys.join(', ')}"`);
}
const id = keys.shift() as string;
return {
id,
... entry[id]
};
}
],
argdef: 'argdef_default|argdef_full',
value: {
defs: 'argdef[]'
}
}).export();
const input = {
defs: [
{ 'baz': 'quo' },
{ 'qux': { default: 'foo' } }
]
};
const result = schema.value(input);
if (result instanceof ArkErrors) {
throw new Error(result.summary);
}
console.log(result); The error:
What am I doing wrong? If the issue is similar to #1080, then is there a way to brute force around the limitation? |
Beta Was this translation helpful? Give feedback.
Answered by
ssalbdivad
Aug 22, 2024
Replies: 1 comment
-
The problem is as the error states, we can't tell which morph we are supposed to execute from that union. Instead, just handle the branching logic in your morph implementation: const schema = scope({
id: "string",
argdef: [
"Record<id, string | argdef_entry>",
"=>",
(entry, ctx): ArgDefSchema => {
const keys = Object.keys(entry)
if (keys.length !== 1) {
ctx.error(
`definition must have exactly one key, got "${keys.join(", ")}"`
)
}
const id = keys.shift() as string
const v = entry[id]
return typeof v === "string" ?
{
id,
default: v
}
: { id, ...v }
}
],
argdef_entry: {
"default?": "string"
},
value: {
defs: "argdef[]"
}
}).export() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
agladysh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is as the error states, we can't tell which morph we are supposed to execute from that union.
Instead, just handle the branching logic in your morph implementation: