swagger-axios-codegen icon indicating copy to clipboard operation
swagger-axios-codegen copied to clipboard

Creating objects with strong type validation

Open fairking opened this issue 2 years ago • 2 comments

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'];
  }

  public static validationModel = {
    id: { required: true, maxLength: 50 },
    name: { required: true, maxLength: 150 },
  };
}

When I create objects, it looses the strong type due to undefined | any.

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

So if my class' property name changes to title I have no validation over how all objects are created.

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

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

Any other ideas?

fairking avatar Oct 05 '21 09:10 fairking

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.

fairking avatar Oct 05 '21 10:10 fairking

check with validtionModel ?

Manweill avatar Oct 09 '21 07:10 Manweill