I discovered that it's possilbe to define complex data schemes in Nim, and being able to validate it against Nim types.
The problem is that it's validated only at runtime. Is there a way to make it to be type-safe and validated at compile time?
Example, in the code below, there's a mistake, the name defined as int not as string. Is it possible to make it fail at compile time? The jinit is macro that does the magic.
macro jinit*[T](TT: type[T], x: untyped): T =
let jobject = toJoImpl(x)
quote do:
`jobject`.json_to(`TT`)
type Unit = object
name: string
echo Unit.jinit { name: 1 } # Error, name is int instead of string
Check playground for full code.
If the data is known at compile-time, it can be done with doAssert or similar.
If the data is not known at compile-time, it may require time travel.