A
arktype3mo 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
ssalbdivad3mo ago
You can use .narrow for that to add custom validation logic
zx
zxOP3mo ago
when using narrow, how do you set an error message?
ssalbdivad
ssalbdivad3mo ago
ArkType
Adding Constraints
TypeScript's 1:1 validator, optimized from editor to runtime
zx
zxOP3mo ago
Thanks!
ssalbdivad
ssalbdivad3mo 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
zxOP3mo ago
Yeah I was thinking bytes would be fine
ssalbdivad
ssalbdivad3mo 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
zxOP3mo ago
For now, narrow seems fine, but I was wondering if there's more documentation regarding custom error messages or test examples?
ssalbdivad
ssalbdivad3mo 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
zxOP3mo ago
.reject works perfectly, thanks a lot!
ssalbdivad
ssalbdivad3mo 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
zxOP3mo 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
ssalbdivad3mo 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
PIat3mo 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
ssalbdivad3mo 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
PIat3mo 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
ssalbdivad3mo ago
AT lets you interpolate non-narrowed numbers for ranges/divisors Why would it be a string?
PIat
PIat3mo ago
When you insert 1000 it will convert to 1s If 1s it'll convert to 1000
ssalbdivad
ssalbdivad3mo 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
PIat3mo ago
I really like having numbers be written in natural language. Had my share of 5 * 60 * 1000
ssalbdivad
ssalbdivad3mo 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
PIat3mo 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
ssalbdivad3mo 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
PIat3mo ago
Should I file it?
ssalbdivad
ssalbdivad3mo ago
Sure, would be happy to have it in the backlog @PIat thanks!
PIat
PIat3mo ago
Thank you too 😊
TizzySaurus
TizzySaurus3mo ago
Would you have that distingiuish between Mb and Mib etc.? Where Mb = 1000^2 bytes and Mib = 1024^2 bytes.
Dimava
Dimava3mo 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
ssalbdivad3mo ago
Yeah there'd have to be a suffix but I'd probably just pick one or the other not both haha
PIat
PIat3mo ago
It would be the technically correct way Pretty readable too..?
Want results from more Discord servers?
Add your server