GoogleFeud / ts-runtime-checks

A typescript transformer that automatically generates validation code from your types.
https://googlefeud.github.io/ts-runtime-checks/
MIT License
312 stars 7 forks source link

[BUG] #37

Closed zeeeeby closed 1 year ago

zeeeeby commented 1 year ago

Describe the bug Hello, i didn't understand, why this code does not generate validators for body: { user: User }. Can you please help?

Playground link link

image

GoogleFeud commented 1 year ago

Hi, the Assert markers don't generate any code because they're inside of a function type definition, they should be removed as they don't do anything.

Type parameters by themselves don't generate any validation code, you need to wrap the types you pass to the check function with Infer - playground

It's also a good idea to create a discriminated union of the objects that could be User - otherwise passing different objects breaks the validation code:

enum PossibleEndpointBodies {
    User,
    DBUser
}

let get = endpoint<{ user: User, kind: PossibleEndpointBodies.User }>(async (req, res, extra) =>
{
    let id = extra.body.user.id
})

let get1 = endpoint<{ user: DBUser, kind: PossibleEndpointBodies.DBUser }>(async (req, res, extra) =>
{
    let id = extra.body.user.id
})
zeeeeby commented 1 year ago

That's awesome! Thanks for explaining!