Genshii
Genshii
Aarktype
Created by Genshii on 2/26/2025 in #questions
Configure errors in a scope
Is it possible to set a expected/actual/etc handler for every type in a scope? I have something like this:
const foo = type({
name: "string",
myKey: "number",
})
const fooArray = foo.array()

const validatedFooArray = fooArray([{ name: "fooName", myKey: "should be a number" }])
if (validatedFooArray instanceof type.errors) {
console.log(validatedFooArray.summary)
}
const foo = type({
name: "string",
myKey: "number",
})
const fooArray = foo.array()

const validatedFooArray = fooArray([{ name: "fooName", myKey: "should be a number" }])
if (validatedFooArray instanceof type.errors) {
console.log(validatedFooArray.summary)
}
The error summary looks as follows: value at [0].myKey must be a number (was a string) Ultimately what I want to do is tell the user the name of object that had the error (let's just assume the name property is always on the object). So something like: (for object with name 'foo'): value at [0].myKey must be a number (was a string) I was thinking of doing something like this:
.configure({
message: (ctx) => {
// if the path starts with an array index, prepend the message with the name
if (/^\[\d+\]\.$/.test(ctx.path.stringify())) {
const name = ctx.data[0].name
return `(for object with name '${name}': ${ctx.problem}`
} else {
return ctx.problem
}
.configure({
message: (ctx) => {
// if the path starts with an array index, prepend the message with the name
if (/^\[\d+\]\.$/.test(ctx.path.stringify())) {
const name = ctx.data[0].name
return `(for object with name '${name}': ${ctx.problem}`
} else {
return ctx.problem
}
But I would need to apply that to all types contained within my foo .
44 replies
Aarktype
Created by Genshii on 2/25/2025 in #questions
Get ts type that accepts type arguments
I have a type that looks like this:
interface PluginInstance<Input, TInput, Diff> {
details: PluginDetails
input: PluginInput<Input>
transform: PluginTransform<Input, TInput> | undefined
diff: PluginDiff<TInput, Diff>
handle: PluginHandle<Diff, TInput>
update: PluginUpdate | undefined
}
interface PluginInstance<Input, TInput, Diff> {
details: PluginDetails
input: PluginInput<Input>
transform: PluginTransform<Input, TInput> | undefined
diff: PluginDiff<TInput, Diff>
handle: PluginHandle<Diff, TInput>
update: PluginUpdate | undefined
}
I would prefer to instead define a Type and infer the ts type.
const pluginInstanceSchema = type({
"+": "reject",
details: pluginDetails,
input: type("Function").as<PluginInput>(),
transform: type("Function").as<PluginTransform | undefined>(),
diff: type("Function").as<PluginDiff>(),
handle: type("Function").as<PluginHandle>(),
update: type("Function").as<PluginUpdate | undefined>(),
})
const pluginInstanceSchema = type({
"+": "reject",
details: pluginDetails,
input: type("Function").as<PluginInput>(),
transform: type("Function").as<PluginTransform | undefined>(),
diff: type("Function").as<PluginDiff>(),
handle: type("Function").as<PluginHandle>(),
update: type("Function").as<PluginUpdate | undefined>(),
})
So ultimately I'd want to do this:
type PluginInstance = typeof pluginInstanceSchema.infer
type ReturnsPluginInstance<Input, TInput, Diff> = (input: Input) => PluginInstance<Input, TInput, Diff>
type PluginInstance = typeof pluginInstanceSchema.infer
type ReturnsPluginInstance<Input, TInput, Diff> = (input: Input) => PluginInstance<Input, TInput, Diff>
But of course PluginInstance doesn't take any type arguments. Is it possible to define generics on my schema and have the inferred type accept those type arguments? I tried playing around with scopes, tried doing const pluginInstanceSchema = type("<Input, TInput, Diff>", { ..., but didn't really get anywhere.
25 replies
Aarktype
Created by Genshii on 2/25/2025 in #questions
"Custom" type or equivalent to valibot.custom()?
I might just be missing something obvious, but I want to do something like this:
import type { SomeType } from "some-external-library"

const foo = type({
foo: "string",
bar: "SomeType"
})
import type { SomeType } from "some-external-library"

const foo = type({
foo: "string",
bar: "SomeType"
})
Obviously SomeType isn't a valid value, so I could do something like this:
const foo = type({
foo: "string",
bar: "unknown" as type.cast<SomeType>
})
const foo = type({
foo: "string",
bar: "unknown" as type.cast<SomeType>
})
But I have a feeling that's not the right way to do it. Is there something like valibot's custom?
11 replies