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.')
});
15 Replies
Dimava
Dimava2w ago
['instanceof', File] [['instanceof', File], '&', { size: 'number < 10000' }] maybe
ssalbdivad
ssalbdivad2w ago
Yeah there is also just a File keyword:
const myType = type({
image: "File"
})
const myType = type({
image: "File"
})
Cobain
Cobain2w ago
Thanks! Another unrelated question, is there a way to reference to another type's attributes when declaring a new type? something like this:
/** The Token entity */
export const Token = type({
id: "string",
symbol: "string",
"description?": "string",
tickers: Ticker.array(),
"image_url?": "string.url"
})

/** A candle of a coin in a point in time */
export const Candle = type({
coin_id: Token.id
})
/** The Token entity */
export const Token = type({
id: "string",
symbol: "string",
"description?": "string",
tickers: Ticker.array(),
"image_url?": "string.url"
})

/** A candle of a coin in a point in time */
export const Candle = type({
coin_id: Token.id
})
ssalbdivad
ssalbdivad2w ago
Yes, you can do Token.get("id"). That said, I'd recommend trying to avoid doing this where possible as it's generally an antipattern to do extra inference and runtime work to extract a type that probably should have been defined standalone I.e. in this case, probably best to define id on its own if you want to reuse it
Cobain
Cobain2w ago
Right ok, yeah probably gonna think about the performance side as the main reason i'm trying to switch from Zod is this
ssalbdivad
ssalbdivad2w ago
Yeah, for a lot of reasons it's generally best to build your types bottom up rather than extract out things you've already defined
Cobain
Cobain2w ago
Ok, thanks for the quick response
ssalbdivad
ssalbdivad2w ago
No problem! Good luck
ssalbdivad
ssalbdivad2w ago
Should mention as well another option if you want to reuse your types directly is a scope. Needs a bit more docs but it's just a way to define your own set of names that you can reference in string definitions across a set of types, so you could define your types in a scope and have an id type to make it easy to reference https://arktype.io/reference/scopes/
ArkType
Scopes
TypeScript's 1:1 validator, optimized from editor to runtime
ssalbdivad
ssalbdivad2w ago
Just a matter of preference though unless you need cyclic types either approach will work
Cobain
Cobain2w ago
Ok I ended doing that. When I access later a type declared in the scope doing types.example, the typing of example doesn't only include itself but all the other scope data, but will only validate the first part right? here:
const Coin: Type<{
id: string;
symbol: string;
description?: string;
image_url?: string.url;
}, {
id: string;
coin: {
id: string;
symbol: string;
description?: string;
image_url?: string.url;
};
const Coin: Type<{
id: string;
symbol: string;
description?: string;
image_url?: string.url;
}, {
id: string;
coin: {
id: string;
symbol: string;
description?: string;
image_url?: string.url;
};
ssalbdivad
ssalbdivad2w ago
Yes the second part just represents the scope it was defined in so that if you chain .or etc. off it you can still reference those keywords You can also split the scope from the types you export if you want to reuse .type later and still be able to reference those keywords
const $ = scope({
foo: "number"
})

const types = $.export()

$.type("foo[]")
const $ = scope({
foo: "number"
})

const types = $.export()

$.type("foo[]")
Then you can reference those keywords from anywhere
Cobain
Cobain2w ago
great thanks, also if i have to validate an object which has lots of keys, but i only want to validate it has the ones im interested in, is there a way to do like a "partial validation"?
Dimava
Dimava2w ago
It's by default You don't need '+': 'reject' or '+': 'delete' Iirc
Cobain
Cobain2w ago
right thanks
Want results from more Discord servers?
Add your server