Cobain
Cobain
Explore posts from servers
Aarktype
Created by Cobain on 10/9/2024 in #questions
Function which receives a arktype's 'type'
I'm trying to do something like this:
async rateLimitedCallApi<T>(url: string, schema: Type<T>): Promise<T> {
await this.rate_limiter.acquire();

const response = await fetch(url, this.request_data);
if (!response.ok) {
throw Error(`Invalid request: ${response}`)
}

const parsedResponse = schema(await response.json());
async rateLimitedCallApi<T>(url: string, schema: Type<T>): Promise<T> {
await this.rate_limiter.acquire();

const response = await fetch(url, this.request_data);
if (!response.ok) {
throw Error(`Invalid request: ${response}`)
}

const parsedResponse = schema(await response.json());
But the parsedResponse isn't of type T, so how do I make this function accept as schema anything created using type({...})
13 replies
Aarktype
Created by Cobain on 9/20/2024 in #questions
Use native ts type
I was wondering if I could use an already created type:
type BlockainsName = "bitcoin" | "ethereum" | "solana" | "polygon-pos" | "binance-smart-chain" | "avalanche"
type BlockainsName = "bitcoin" | "ethereum" | "solana" | "polygon-pos" | "binance-smart-chain" | "avalanche"
inside an arktype type declaration:
export const walletType = type({
address: "string",
blockchain: "BlockainsName",
{...}
export const walletType = type({
address: "string",
blockchain: "BlockainsName",
{...}
I would go on and create an arktype literal as suggested in the docs, but this type is being declared from another object:
export type BlockainsName = keyof typeof blockchains
export type BlockainsName = keyof typeof blockchains
And I want it to be tighly coupled with that object
5 replies
Aarktype
Created by Cobain on 9/9/2024 in #questions
Multiple versions
No description
3 replies
Aarktype
Created by Cobain on 9/6/2024 in #questions
Represent the File class
Hi! I want to make a schema for validating a form, and one of the elements of the form is going to be a file upload, so I would like to validate that the form is sending an instance of the File class, and then validate that this file size is smaller than a certain amount. Is there a way to represent class instances in Arktype? An example of doing this in Zod would be:
const schema = z.object({
image: z
.instanceof(File, { message: 'Please upload a file.'})
.refine((f) => f.size < 100_000, 'Max 100 kB upload size.')
});
const schema = z.object({
image: z
.instanceof(File, { message: 'Please upload a file.'})
.refine((f) => f.size < 100_000, 'Max 100 kB upload size.')
});
23 replies