Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

Creating objects with strong type validation #153

Closed fairking closed 11 months ago

fairking commented 3 years ago

In my case the codegen is generating the following class code:

export class LookupItemVm {
  'id'?: string;
  'name'?: string;
  'description'?: string;

  constructor(data: undefined | any = {}) {
    this['id'] = data['id'];
    this['name'] = data['name'];
    this['description'] = data['description'];
  }
}

When I create objects, it looses the strong type due to constructor(data: undefined | any = {}). The following code will be valid and won't throw any compiler errors:

var obj = new LookupItemVm({ id2: 'abc', name2: 'My ABC' });

So if my class' property name changes to title I have no validation over whether there any issues across the entire project.

Is there any idea how to force to use only strong type syntax?

For example if we change the constructor(data: LookupItemVm = {}) the type check will complain if property has changed.

Any other ideas?

fairking commented 3 years ago

I was also thinking about empty constructor and then I found somewhere the following sintax:

const waldo = <Foo> {
    bar: "Hello",
    qux: 7
}

But it doesn't work in lambda expressions:

// there will be an error:
this.colDefs.filter(x => x.visible === true).map(x => <Foo> { name: x.colName, visible: x.visible }));

IMHO how constructor is generated by codegen is way too loose and I think we can improve that part to make it more strong.

Manweill commented 3 years ago

check with validtionModel ?

fairking commented 11 months ago

check with validtionModel ?

I have removed that part from the example to avoid confusion. I was referring to the constructor and the way how we can make it more strongly typed. ValidationModel would not contain all properties (optional are missing).

fairking commented 10 months ago

Thank you @Manweill for your hard work on the project. It is very useful for many devs.