papy0977
papy0977
Aarktype
Created by papy0977 on 1/30/2025 in #questions
Fail fast on union type
Hi i try to fail fast on union type as i know if there is an error , it's pointless to continue checking the union and get the right message and not the full list of all union message. For this i throw an error in the union branch and do a try catch on the type generated. Is there a clever/official way to do that:
function arkTry<T extends (...args: unknown[]) => unknown>(fn: T) {
return (...args: Parameters<T>): ReturnType<T> | ArkError => {
try {
return fn(...args) as ReturnType<T>;
} catch (e) {
return e as ArkError;
}
};
}

function arkValidOrThrow<T>(x: T): Exclude<T, ArkErrors | ArkError> | never {
if (x instanceof ArkErrors || x instanceof ArkError) {
throw new Error(x.message);
}
return x as Exclude<T, ArkErrors | ArkError>;
}
function arkTry<T extends (...args: unknown[]) => unknown>(fn: T) {
return (...args: Parameters<T>): ReturnType<T> | ArkError => {
try {
return fn(...args) as ReturnType<T>;
} catch (e) {
return e as ArkError;
}
};
}

function arkValidOrThrow<T>(x: T): Exclude<T, ArkErrors | ArkError> | never {
if (x instanceof ArkErrors || x instanceof ArkError) {
throw new Error(x.message);
}
return x as Exclude<T, ArkErrors | ArkError>;
}
40 replies
Aarktype
Created by papy0977 on 1/25/2025 in #questions
Scope and narrow
Hello, need to have nested objects but with only 1 key in each, i tried using narrow:
export const SQLBoolOperatorSchema = type({
_eq: "string|number|boolean",
})
.or({ _neq: "string|number|boolean" })
.or({ _gt: "string|number" });

const $boolField = scope({
ops: SQLBoolOperatorSchema,
record: {
"[string]": "value",
},
value: "ops | record",
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")
);
})
});

export const { field: SQLBoolFieldSchema } = $boolField.export();

// should error as access have multiple field
const foo = SQLBoolFieldSchema({
access: { id: { _eq: "$id" }, foo: { _eq: "12" } },
} as const);
if (foo instanceof ArkErrors) {
throw new Error(foo.summary);
}
export const SQLBoolOperatorSchema = type({
_eq: "string|number|boolean",
})
.or({ _neq: "string|number|boolean" })
.or({ _gt: "string|number" });

const $boolField = scope({
ops: SQLBoolOperatorSchema,
record: {
"[string]": "value",
},
value: "ops | record",
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")
);
})
});

export const { field: SQLBoolFieldSchema } = $boolField.export();

// should error as access have multiple field
const foo = SQLBoolFieldSchema({
access: { id: { _eq: "$id" }, foo: { _eq: "12" } },
} as const);
if (foo instanceof ArkErrors) {
throw new Error(foo.summary);
}
it doesnt work after 1st level, as i think i also need to narrow the "value" field.
9 replies
Aarktype
Created by papy0977 on 1/24/2025 in #questions
Recursive type problem:
Hello, I'm trying to define a recursive type with the library, but without success, i have the error remoteFieldSchema is unresolvable. What can i do: const recTypes = type.module({ remoteFieldSchema: { arguments: type({ "[string]": "string" }), field: type({ "[string]": "remoteFieldSchema" }).optional(), }, }); thanks in advance.
6 replies
Aarktype
Created by papy0977 on 1/20/2025 in #questions
Hi,
new to arktype, i have two questions: 1. i can't add a default for array ? like : foo:"string[]=['bar']" 2. given a type with all it's default can i create a concrete object from this type with all the default like: const foo=type({bar:"number.integer=42"}) and get back const defObj={bar: 42}
5 replies