A
arktype•4mo ago
CodeHz

How to handle checkbox in type('parse.formData').to(...)

I want turn the checkbox into boolean, but the checkbox field only produce "on" when checked, and missing when unchecked...
29 Replies
CodeHz
CodeHzOP•4mo ago
["'on' | 'off'", "=", "off"] works, but I cannot convert it to boolean
TizzySaurus
TizzySaurus•4mo ago
You need to tell ArkType how to convert the string to a boolean. I guess what you want is
type(["'on'|'off'", "=", "off"]).pipe(v => v === "on")
type(["'on'|'off'", "=", "off"]).pipe(v => v === "on")
CodeHz
CodeHzOP•4mo ago
but how to embed it with other propertities?
TizzySaurus
TizzySaurus•4mo ago
Wdym?
CodeHz
CodeHzOP•4mo ago
No description
CodeHz
CodeHzOP•4mo ago
it just dont work
TizzySaurus
TizzySaurus•4mo ago
What's the error(s), and what version of ArkType are you ussing?
CodeHz
CodeHzOP•4mo ago
arktype=2.0.0-beta.5
CodeHz
CodeHzOP•4mo ago
error is chinese (but it basically mean cannot assign '=' to ....
No description
TizzySaurus
TizzySaurus•4mo ago
Do you actually need the default value?
CodeHz
CodeHzOP•4mo ago
'default?' also doen't work
TizzySaurus
TizzySaurus•4mo ago
^ ???
const t = type({foo: ["'on' | 'off' = 'on'", "=>", v => v === "on"]});
const t = type({foo: ["'on' | 'off' = 'on'", "=>", v => v === "on"]});
this seems to work nvm, it fails at runtime
CodeHz
CodeHzOP•4mo ago
runtime error: Default values must be specified on objects like { isAdmin: 'boolean = false' }
TizzySaurus
TizzySaurus•4mo ago
Yeah, I just got the same when actually running it That's probably a bug If you don't need the default you can remove it and then it works properly If you do need the default, then I guess the best option is to just make it optional
type({"foo?": ["'on' | 'off'", "=>", v => v === "on"]})
type({"foo?": ["'on' | 'off'", "=>", v => v === "on"]})
would work
CodeHz
CodeHzOP•4mo ago
yes, I think it would be perfect
TizzySaurus
TizzySaurus•4mo ago
Although in fairness there's no reason to use a default here Because you're morphing to a boolean anyway, and so whether the value is undefined or "off" the outcome is the same (both will give false)
CodeHz
CodeHzOP•4mo ago
the backend API use boolean, and I don't want to use two different types for it..
CodeHz
CodeHzOP•4mo ago
just created a new workaround: fix the formdata api
No description
TizzySaurus
TizzySaurus•4mo ago
Fwiw I've created a bug report: https://github.com/arktypeio/arktype/issues/1089
GitHub
Unable to morph object values with default · Issue #1089 · arktypei...
Report a bug It seems it's currently impossible to morph and object value that has a default. 🔎 Search Terms object default, morph default 🧩 Context ArkType version: 2.0.0-beta.5 TypeScript ver...
ssalbdivad
ssalbdivad•4mo ago
Added a couple comments to the issue Will definitely consider loosening some of these restrictions if I can find a sensible way to do it
TizzySaurus
TizzySaurus•4mo ago
Yeah, that's fair. I actually said above it doesn't really make sense to default here I suppose the issue should be closed as "Won't Do" or w/e rather than "Completed"?
ssalbdivad
ssalbdivad•4mo ago
I think I already changed it
TizzySaurus
TizzySaurus•4mo ago
Still showing "Completed" for me :Shrug:
ssalbdivad
ssalbdivad•4mo ago
Oh I think I clicked that button but it only does something if I actually wrote a comment But still, I would like to handle it better ideally, because I could just optimize the redundancy away like the type system does in a lot of other cases by applying the morph to the default value to update it Just not worth handling as a one-off right now
TizzySaurus
TizzySaurus•4mo ago
Is it worth leaving the issue open for future tracking then?
ssalbdivad
ssalbdivad•4mo ago
Hmm Could you create a brief feature request instead like "Precomputed morphs for default values"
TizzySaurus
TizzySaurus•4mo ago
Sure
TizzySaurus
TizzySaurus•4mo ago
Typed on my phone, so probs not perfect but here you go: https://github.com/arktypeio/arktype/issues/1090
GitHub
Precomputed morphs for defaults · Issue #1090 · arktypeio/arktype
Request a feature 🤷 Motivation Relates to #1089. This would optimise the redundancy away from morphed default types by moving the default assignment into the morph. For example: type({foo: ["&...
ssalbdivad
ssalbdivad•4mo ago
Yeah I already get notifications from https://discord.com/channels/957797212103016458/1090878931558731848 😛 Thank you! I have a private channel that notifies me for anything on the repo, even just someone starring it 😅
Want results from more Discord servers?
Add your server