A
arktype•2mo 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
CodeHz•2mo ago
["'on' | 'off'", "=", "off"] works, but I cannot convert it to boolean
TizzySaurus
TizzySaurus•2mo 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
CodeHz•2mo ago
but how to embed it with other propertities?
TizzySaurus
TizzySaurus•2mo ago
Wdym?
CodeHz
CodeHz•2mo ago
No description
CodeHz
CodeHz•2mo ago
it just dont work
TizzySaurus
TizzySaurus•2mo ago
What's the error(s), and what version of ArkType are you ussing?
CodeHz
CodeHz•2mo ago
arktype=2.0.0-beta.5
CodeHz
CodeHz•2mo ago
error is chinese (but it basically mean cannot assign '=' to ....
No description
TizzySaurus
TizzySaurus•2mo ago
Do you actually need the default value?
CodeHz
CodeHz•2mo ago
'default?' also doen't work
TizzySaurus
TizzySaurus•2mo 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
CodeHz•2mo ago
runtime error: Default values must be specified on objects like { isAdmin: 'boolean = false' }
TizzySaurus
TizzySaurus•2mo 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
CodeHz•2mo ago
yes, I think it would be perfect
Want results from more Discord servers?
Add your server