swagger-axios-codegen
swagger-axios-codegen copied to clipboard
Creating objects with strong type validation
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?
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.
check with validtionModel
?