File type
I noticed you can do types like File, I was wondering if it's possible to do things like validate file size?
30 Replies
You can use
.narrow
for that to add custom validation logicwhen using narrow, how do you set an error message?
Thanks!
No problem. I've seen that question before. I would actually consider supporting a builtin
<
operator for File
as well, not sure what the default units would be though. Bytes I guess since that's what it is in Node?Yeah I was thinking bytes would be fine
Would be kinda awkward though to have a long value since comma separators aren't supported yet.
Guess we could allow stuff like
File < 1M
For now, narrow seems fine, but I was wondering if there's more documentation regarding custom error messages or test examples?
That's an area where the docs really need to be fleshed out. There are a lot of options though. You can kinda see what you can configure when you do
ctx.
and see what is suggested. ctx.reject
allows you to add an error and return false like mustBe
but you can pass an object with a bunch of config options.reject works perfectly, thanks a lot!
The error customization API is super rich so it should be a big advantage but since it's not fully documented yet it's actually the opposite for most users.
I promise I will address that ASAP 🙏
Sounds good! I really like ArkType compared to my experience with other validators so far but it's been hard figuring out things like this.
Yeah definitely understand that. I hope that once you do figure something out, it's kind of generalizable- things are composable and there are very few edge cases.
But I totally realize that getting started is the biggest hurdle now and want to address that by ensuring the most important parts of the API have a decent reference
I would definitely be using a utility function (similar to ms: https://www.npmjs.com/package/ms) to convert MB to bytes and using
as const
Adding MB
and/or KB
would be a lot smoother DXI don't think you need
as const
, wouldn't be narrowed anyways I assume
Also you probably don't need a dep for dividing by a constant right? 😛Actually now that I'm thinking about it, only Arktype knows what the return value from the string would be 😁
ms
only returns string
or number
AT lets you interpolate non-narrowed numbers for ranges/divisors
Why would it be a string?
When you insert
1000
it will convert to 1s
If 1s
it'll convert to 1000
Hmm I guess it depends on how many conversions you need, but if you just want MB you could probably just implement it in a couple lines
I really like having numbers be written in natural language. Had my share of
5 * 60 * 1000
A well named function makes sense, i'd just expect most people would want to specify constraints with a single unit (maybe MB) so I'd probably just implement a function to do that
Would look great, but it's not critical right now. It'd just be sugar. When handling uploads, you'd also check the MIME type and probably the actual contents, so at that point you'd create a
narrow
anywaysYeah definitely not prioritizing now haha. Would conisider creating an issue since it's the kind of thing that can be good for external contributors
Should I file it?
Sure, would be happy to have it in the backlog
@PIat thanks!
Thank you too 😊
Would you have that distingiuish between
Mb
and Mib
etc.?
Where Mb = 1000^2 bytes and Mib = 1024^2 bytes.mb = 1000^2 bits (╯°□°)╯︵ ┻━┻
But yes that's an interesting question
I'd say it should be Bytes only, base1024
If you need 10-powered just use
123e3
Yeah there'd have to be a suffix but I'd probably just pick one or the other not both haha
It would be the technically correct way
Pretty readable too..?