papy0977
papy0977
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
Yep, i was expecting not to go the route of narrowing , just letting arktype do all the work of validating/branching 😉 Hope your release go well 🙂
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
Hi, Following our chat, is there a way to intercept an error ? In my example the throw work because when it goes to narrow the branch is valid, but imagine i enter the branch _add, and there is an error on the operand i have no way to act here as the others branches (sub and mul) will be tried, but it pointless as i know it was an add branch.
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
Thank you very much, i think i will go to sleep for some few hours now.
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
like for example in my case i gave it an expression object and it validate and morph it into another shape. I don't know how describe. What i am doing is transforming a graphql AST into an SQL one
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
can we use arktype as a parser validater, as arktype is already descending all branches for us ?
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
yes it could be handy
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
yes because if a part of an expression is not valid then the whole is also not
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
ok will see what i can achieve as i'm not a type system expert 😉
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
Ok thank you, i will keep it as this for the moment (or maybe it's not the way todo it as you say ?) and see how it's going. For the moment i put 3 operators, but when it will grows that will a hell of a chain or errors 😉 Thank you for your time.
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
what's puzzle me is the error message i can't say at first sight what happens so that why i wanted to terminate early
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
yes i know for the syntax thank you, i just wanted to have more control 😉
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
yes i want to skip the check of other branch as it's poinless if i have enter for example the add branch and there is not enough operand there is no point to continue
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
in my case it'not working as i have a message like:
error: _add must be an array (was missing), _mul[2]._add must be an array (was missing), _mul[2]._col must be a string (was missing), _mul[2]._mul[3] must be _mul[2]._mul[3] must be an array of 2 or more operands (was {"_add":[10],"_sub":[1]}), _mul[2]._mul[3]._col must be a string (was missing), _mul[2]._mul[3]._mul must be an array (was missing) or _mul[2]._mul[3]._var must be a string (was missing) (was {"_add":[10],"_sub":[1]}), _mul[2]._sub must be an array (was missing), _mul[2]._var must be a string (was missing) or _sub must be an array (was missing)
error: _add must be an array (was missing), _mul[2]._add must be an array (was missing), _mul[2]._col must be a string (was missing), _mul[2]._mul[3] must be _mul[2]._mul[3] must be an array of 2 or more operands (was {"_add":[10],"_sub":[1]}), _mul[2]._mul[3]._col must be a string (was missing), _mul[2]._mul[3]._mul must be an array (was missing) or _mul[2]._mul[3]._var must be a string (was missing) (was {"_add":[10],"_sub":[1]}), _mul[2]._sub must be an array (was missing), _mul[2]._var must be a string (was missing) or _sub must be an array (was missing)
my error is lost in all the union
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
?
40 replies
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
export const $mathExpr = scope({
col: { _col: "string" },
var: { _var: "string" },
scalar: "number | col | var",
operand: "scalar | expr",
add: [
{ _add: "operand[]" },
":",
(data, ctx) => {
if (data._add.length >= 2) return true;
throw ctx.error("an array of 2 or more operands");
},
],
sub: [
{ _sub: "operand[]" },
":",
(data, ctx) => {
if (data._sub.length >= 2) return true;
throw ctx.error("an array of 2 or more operands");
},
],
mul: [
{ _mul: "operand[]" },
":",
(data, ctx) => {
if (data._mul.length >= 2) return true;
throw ctx.error("an array of 2 or more operands");
},
],
expr: "add | sub | mul",
}).export();

const $GQLMathExprSchema = $mathExpr.expr;

export const GQLMathExprSchema = arkTry($GQLMathExprSchema);
export type GQLMathExpr = typeof $GQLMathExprSchema.infer;

const foo = arkValidOrThrow(
GQLMathExprSchema({
_mul: [1,2,{_mul: [3, 4, { _sub: [5, 6] }, { _add: [10], _sub: [1, 2] }],},],
}),
);

console.log("_mul" in foo && foo._mul);
export const $mathExpr = scope({
col: { _col: "string" },
var: { _var: "string" },
scalar: "number | col | var",
operand: "scalar | expr",
add: [
{ _add: "operand[]" },
":",
(data, ctx) => {
if (data._add.length >= 2) return true;
throw ctx.error("an array of 2 or more operands");
},
],
sub: [
{ _sub: "operand[]" },
":",
(data, ctx) => {
if (data._sub.length >= 2) return true;
throw ctx.error("an array of 2 or more operands");
},
],
mul: [
{ _mul: "operand[]" },
":",
(data, ctx) => {
if (data._mul.length >= 2) return true;
throw ctx.error("an array of 2 or more operands");
},
],
expr: "add | sub | mul",
}).export();

const $GQLMathExprSchema = $mathExpr.expr;

export const GQLMathExprSchema = arkTry($GQLMathExprSchema);
export type GQLMathExpr = typeof $GQLMathExprSchema.infer;

const foo = arkValidOrThrow(
GQLMathExprSchema({
_mul: [1,2,{_mul: [3, 4, { _sub: [5, 6] }, { _add: [10], _sub: [1, 2] }],},],
}),
);

console.log("_mul" in foo && foo._mul);
40 replies
Aarktype
Created by papy0977 on 1/25/2025 in #questions
Scope and narrow
Yes with jitless: true it works, thank you !
9 replies
Aarktype
Created by papy0977 on 1/25/2025 in #questions
Scope and narrow
ok just try a simple tuple for the field, but got an error: 89 | return true 90 | } 91 | if (!ctx.seen.value2) { 92 | ctx.seen.value2 = [] 93 | } 94 | return this.value2Apply(data, ctx) ^ TypeError: this.value2Apply is not a function. (In 'this.value2Apply(data, ctx)', 'this.value2Apply' is undefined) at alias9Apply (node_modules/@ark/util/out/functions.js:94:28) at structure169Apply (node_modules/@ark/util/out/functions.js:59:29) at intersection601Apply (node_modules/@ark/util/out/functions.js:22:27) at <anonymous> (node_modules\@ark\schema\out\node.js:22:18)
const $tuBoolField = scope({
ops: SQLBoolOperatorSchema,
value: "ops | field",
field: [
{ "[string]": "value" },
":",
(x, ctx) => {
console.log("narrow field", x);
return (
Object.keys(x).length === 1 ||
ctx.mustBe("accepting only one field for column comparison")
);
},
],
});
export const { field: tuBoolFieldSchema } = $tuBoolField.export();

const tuFoo = tuBoolFieldSchema({
access: {
// id: { _eq: "$id", _neq: "foo" },
foo: { bar: { _eq: "12" }, hello: { eq: "hello" } },
},
// id: { _eq: "$id" },
} as const);
if (tuFoo instanceof ArkErrors) {
throw new Error(tuFoo.summary);
}
const $tuBoolField = scope({
ops: SQLBoolOperatorSchema,
value: "ops | field",
field: [
{ "[string]": "value" },
":",
(x, ctx) => {
console.log("narrow field", x);
return (
Object.keys(x).length === 1 ||
ctx.mustBe("accepting only one field for column comparison")
);
},
],
});
export const { field: tuBoolFieldSchema } = $tuBoolField.export();

const tuFoo = tuBoolFieldSchema({
access: {
// id: { _eq: "$id", _neq: "foo" },
foo: { bar: { _eq: "12" }, hello: { eq: "hello" } },
},
// id: { _eq: "$id" },
} as const);
if (tuFoo instanceof ArkErrors) {
throw new Error(tuFoo.summary);
}
9 replies
Aarktype
Created by papy0977 on 1/25/2025 in #questions
Scope and narrow
thanks will look if i can do something
9 replies
Aarktype
Created by papy0977 on 1/25/2025 in #questions
Scope and narrow
Hi, what do you mean by using a tuple expression ?
9 replies
Aarktype
Created by papy0977 on 1/25/2025 in #questions
Scope and narrow
so i tried:
const $boolField = scope({
ops: SQLBoolOperatorSchema,
record: {
"[string]": "n_value",
},
value: "ops | record",
n_value: () =>
$boolField.type("value").narrow((x, ctx) => {
console.log(x);
return (
Object.keys(x).length === 1 ||
ctx.mustBe("accepting only one field for column comparison")
);
}),
field: () =>
$boolField
.type("record")
.narrow((x, ctx) => {
console.log(x);
return (
Object.keys(x).length === 1 ||
ctx.mustBe("accepting only one field for column comparison")
);
})
});
const $boolField = scope({
ops: SQLBoolOperatorSchema,
record: {
"[string]": "n_value",
},
value: "ops | record",
n_value: () =>
$boolField.type("value").narrow((x, ctx) => {
console.log(x);
return (
Object.keys(x).length === 1 ||
ctx.mustBe("accepting only one field for column comparison")
);
}),
field: () =>
$boolField
.type("record")
.narrow((x, ctx) => {
console.log(x);
return (
Object.keys(x).length === 1 ||
ctx.mustBe("accepting only one field for column comparison")
);
})
});
the record is now rejected at runtime but: 1. i loose all the typing ('dvalue' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions) 2. get a warning Type of property '"[string]"' circularly references itself in mapped type 'validateObjectLiteral<{ readonly "[string]": "dvalue"; }...
9 replies