Typescript is very well suited for adding type safety to a JavaScript program, but on its own, it's not enough to guarantee that it won't crash at ...
For further actions, you may consider blocking this person and/or reporting abuse
This seems very similar to using a runtime schema validation library like Joi.
I've been using this helper function to typecheck API payloads. It returns the payload cast to the type it is supposed to be when the schema validates. If it doesn't validate the function throws an error with messages explaining what part of the schema failed to comply.
Never used Joi before but it looks much more powerful (and heavy).
This library is just a few bytes, although it has all the basic pieces you need to build more Joi-ish stuff (I guess).
Something you can do with
ts.data.json
that I don't see in the Joi docs is mapping over a decoded value. For instance:Very useful.
Thanks for pointing that out Thijs.
Cheers!
Mapping seems useful indeed :)
I use Joi on the server so size is not an issue there. For the browser I use yup. Similar API but much smaller footprint.
Interesting! smaller footprint but powerful too.
I might add an alternatives section in the
ts.data.json
docs.Thanks!
HI everybody.
I am using the JsonDecoder and I have two issues that I do not know how to solv.
(alias) namespace JsonDecoder
import JsonDecoder
Argument of type '{ optionID: Decoder; name: Decoder; study: any; workflowId: Decoder; workflow: Decoder; scenarios: Decoder; disabled: Decoder; defaultTask: Decoder<...>; }' is not assignable to parameter of type 'DecoderObject'.
Type '{ optionID: Decoder; name: Decoder; study: any; workflowId: Decoder; workflow: Decoder; scenarios: Decoder; disabled: Decoder; defaultTask: Decoder<...>; }' is missing the following properties from type 'DecoderObject': getTaskByID, getScenarioByID, addScenario, updateScenario, getPreviousTaskts
Best,
Hmendezm
You can decode the methods with
JsonDecoder.succeed
:but the class will lose all the instance information during the decoding process. If you are using the
instanceof
operator later on it won't work. Aside from that, you are good to go.Hey Joan, sorry for bothering you again.
I have a case where the columns from the JSON can be with different for instance the JSON can have _color: yellow or color: yellow.
I am using the keyMap for the _color and I thought that if the column does not exist will take color but it is not the case. How can I have more the one keymap option?
Best
Hmendezm
Hi Joan, sorry for bothering you. U have a case when the property in the class can be a number or null. How I can set a default value when it is null?
Best
Hmendezm
Thanks, Joan for the help.
Have you seen io-ts? io-ts also removes the duplication by creating a "decoder" and a type at the same time.
what would happen in this case?
I'm aware of io-ts but I haven't jumped on the fp-ts bandwagon yet. The learning curve seems quite steep.
The decoder you just mentioned would fail at compile-time, because
number
andstring
are different types.If the data you want to decode is a
number
but you want astring
you could do:Cheers!
I had a similar idea.
github.com/kofno/jsonous
Looks very nice 👌
Very Elmish, which I like!
It's in the related libraries section now
Awesome! Thank you!
actually one thing i observed while testing this package, userDecoder.decodeToPromise is the method name for getting javascript object from specified JSON object, but in your article you have written as decodePromise, this method not exist on useDecoder
Hi Joan, sorry for bothering you. U have a case when the property in the class can be a number or null. How I can set a default value when it is null?
Best
Hmendezm
Hi! You can use
JsonDecoder.failover
: github.com/joanllenas/ts.data.json...There are other more exotic options but I think this is enough in most cases.
Thanks Joan.
I did the failover and it is working as intending. I got problems with the JsonDecoder.Success in functions as you recommended posts back (Sep 27).
Uncaught (in promise): TypeError: Cannot read property 'toApiDTO' of undefined.
Best
Hmendezm
Zod enters the room