Skip to content
Advertisement

Json schema of interface – serialization missing some fields

For this code, where I have an user defined interface and schema definition is guided.

JavaScript

There are two schemas here. 1. for programming language specific container that is IUser interface 2. the one that I want to send it to backend that is composed by Schema object something like

JavaScript

now I am trying to serialize this Schema object to string using JSON.stringify() but type is skipped, how can I have it serialized or how can I translate this IUser into JSON schema in the best way in TS.

Edit:

I was able to retrieve the type name by doing something like this

JavaScript

if there is an array field in the interface – how do I get the type of the array?

Advertisement

Answer

You just need to use values that are serializable to JSON because String and Number are functions, and therefore are not serializable.

For example, maybe you want to test the typeof obj[prop] for particular string.

JavaScript

And validate would now look like:

JavaScript

Which serializes fine:

JavaScript

See playground

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement