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:
15 Replies
['instanceof', File]
[['instanceof', File], '&', { size: 'number < 10000' }]
maybeYeah there is also just a
File
keyword:
Thanks!
Another unrelated question, is there a way to reference to another type's attributes when declaring a new type? something like this:
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 itRight ok, yeah probably gonna think about the performance side as the main reason i'm trying to switch from Zod is this
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
Ok, thanks for the quick response
No problem! Good luck
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/Just a matter of preference though unless you need cyclic types either approach will work
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:
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
Then you can reference those keywords from anywheregreat 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"?
It's by default
You don't need
'+': 'reject'
or '+': 'delete'
Iircright thanks