A
arktype2mo ago
ZX

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
ssalbdivad
ssalbdivad2mo ago
You can use .narrow for that to add custom validation logic
ZX
ZXOP2mo ago
when using narrow, how do you set an error message?
ssalbdivad
ssalbdivad2mo ago
ArkType
Adding Constraints
TypeScript's 1:1 validator, optimized from editor to runtime
ZX
ZXOP2mo ago
Thanks!
ssalbdivad
ssalbdivad2mo ago
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?
ZX
ZXOP2mo ago
Yeah I was thinking bytes would be fine
ssalbdivad
ssalbdivad2mo ago
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
ZX
ZXOP2mo ago
For now, narrow seems fine, but I was wondering if there's more documentation regarding custom error messages or test examples?
ssalbdivad
ssalbdivad2mo ago
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
ZX
ZXOP2mo ago
.reject works perfectly, thanks a lot!
ssalbdivad
ssalbdivad2mo ago
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 🙏
ZX
ZXOP2mo ago
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.
ssalbdivad
ssalbdivad2mo ago
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
PIat
PIat2mo ago
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
type({
myFile: `File<${convertToBytes('30mb') as const}`
})
type({
myFile: `File<${convertToBytes('30mb') as const}`
})
Adding MB and/or KB would be a lot smoother DX
ssalbdivad
ssalbdivad2mo ago
I 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? 😛
PIat
PIat2mo ago
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
ssalbdivad
ssalbdivad2mo ago
AT lets you interpolate non-narrowed numbers for ranges/divisors Why would it be a string?
PIat
PIat2mo ago
When you insert 1000 it will convert to 1s If 1s it'll convert to 1000
ssalbdivad
ssalbdivad2mo ago
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
PIat
PIat2mo ago
I really like having numbers be written in natural language. Had my share of 5 * 60 * 1000
ssalbdivad
ssalbdivad2mo ago
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
PIat
PIat2mo ago
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 anyways
ssalbdivad
ssalbdivad2mo ago
Yeah definitely not prioritizing now haha. Would conisider creating an issue since it's the kind of thing that can be good for external contributors
PIat
PIat2mo ago
Should I file it?
ssalbdivad
ssalbdivad2mo ago
Sure, would be happy to have it in the backlog @PIat thanks!
PIat
PIat2mo ago
Thank you too 😊
TizzySaurus
TizzySaurus2mo ago
Would you have that distingiuish between Mb and Mib etc.? Where Mb = 1000^2 bytes and Mib = 1024^2 bytes.
Dimava
Dimava2mo ago
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
// example:
const discordAttachment = type('File < 5M')
const driveSizeInFakeGigabytes = type('File < 100e9')
// example:
const discordAttachment = type('File < 5M')
const driveSizeInFakeGigabytes = type('File < 100e9')
ssalbdivad
ssalbdivad2mo ago
Yeah there'd have to be a suffix but I'd probably just pick one or the other not both haha
PIat
PIat2mo ago
It would be the technically correct way Pretty readable too..?
Want results from more Discord servers?
Add your server