winterbolt
winterbolt
Aarktype
Created by winterbolt on 3/29/2025 in #questions
How can I validate a `Set` of certain item type?
Hello, I am trying to create a schema that validates that a property is a set of strings:
const schema = type({
items: "Set<string>"
});
const schema = type({
items: "Set<string>"
});
But that gives me an error. I checked the docs, and nothing seems to cover Maps or Sets. Am I missing something, or aren't they supported? Thanks.
3 replies
Aarktype
Created by winterbolt on 3/15/2025 in #questions
Is there an equivalent to Zod's `coerce`?
Hello, I am trying to convert the following Zod schema to ArkType:
const Params = z.object({
sort: z
.union([
z.literal("name"),
z.literal("newest"),
z.literal("oldest"),
z.literal("modified"),
])
.default("newest"),
limit: z.coerce.number().int().min(1).max(10).default(10),
page: z.coerce.number().int().min(0).default(0),
});
const Params = z.object({
sort: z
.union([
z.literal("name"),
z.literal("newest"),
z.literal("oldest"),
z.literal("modified"),
])
.default("newest"),
limit: z.coerce.number().int().min(1).max(10).default(10),
page: z.coerce.number().int().min(0).default(0),
});
So far, I have this:
const Params = type({
sort: type("'name' | 'newest' | 'oldest' | 'modified'").default("newest"),
limit: type("string.integer")
.pipe(Number)
.and("1 <= number.integer <= 10")
.default(10),
page: type("string.integer")
.pipe(Number)
.and("number.integer >= 0")
.default(0),
});
const Params = type({
sort: type("'name' | 'newest' | 'oldest' | 'modified'").default("newest"),
limit: type("string.integer")
.pipe(Number)
.and("1 <= number.integer <= 10")
.default(10),
page: type("string.integer")
.pipe(Number)
.and("number.integer >= 0")
.default(0),
});
But I get errors when using default for limit and page saying: Argument of type 'number' is not assignable to parameter of type '() => never'.ts(2345) and also the type when hovering over it doesn't seem right:
const Params: Type<{
sort: Default<"name" | "newest" | "oldest" | "modified", "newest">;
limit: Default<(In: never) => Out<number>, never>;
page: Default<(In: never) => Out<number>, never>;
}, {}>
const Params: Type<{
sort: Default<"name" | "newest" | "oldest" | "modified", "newest">;
limit: Default<(In: never) => Out<number>, never>;
page: Default<(In: never) => Out<number>, never>;
}, {}>
I assume it's not inferring the types correctly or am I doing something wrong? Is this a right way to emulate Zod's coerce?
4 replies