Accept number in string type
Hello! I'm parsing each value in a form with
JSON.parse
to convert string values like false
to actual booleans. Unfortunately, this introduces the issue, that if someone inputs 123
into a text field, it gets parsed into a number type by JSON.parse
. Then if the field is type string
. An error is thrown, since number is not assignable to string.
How should a type look like, if it should handle such a situation?120 Replies
Could you write a code snippet showing showing this?
Result:
What's this doing?
generateFormData
runs JSON.parse
on every value. The left-hand site is what it would return given the request
So.... just change
generateFormData
to not JSON.parse when the text field type is "string"?
This doesn't seem like an AT issueIt's not an AT issue, I am asking advice how it could be handled
Right, then this is how ig ^
I am converting it in such a way, since
multipart/form-data
has to send everything as stringsYou presumably have a way of knowing which input fields are going to be a text type
Yes
So just have your
generateFormData
function take that into account
In a very verbose way, something like
Or as one line, something likeI guess this could be use for the comparison
But will this also work when the type has contraints? (
string>0
)Possibly. I'm not familiar with the library you're using for forms
Yeah; why wouldn't it? This is all before ArkType
formType
is an Arktype type
It means that having a name of
"123"
is valid (which arguably shouldn't be a valid name, but I don't have the context etc.) but it will still apply the constraint of the length being >0
Oh I think I see what you mean now
Hmm
Let me get an ArkType environment open