# Fetch Object in combindation with swagger and Aurelia Validation

**URL:** <https://discourse.aurelia.io/t/fetch-object-in-combindation-with-swagger-and-aurelia-validation/1941>\
**Category:** Best Practices\
**Created:** [November 23, 2018, 6:51am UTC](https://discourse.aurelia.io/t/fetch-object-in-combindation-with-swagger-and-aurelia-validation/1941 "2018-11-23T06:51:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)\
**Post date:** [November 23, 2018, 6:51am UTC](https://discourse.aurelia.io/t/fetch-object-in-combindation-with-swagger-and-aurelia-validation/1941/1 "2018-11-23T06:51:26Z")

</div>

I would like to share some experience I made in the last days hoping this can help someone.

I’m using Swagger in combination with NSwagStudio to generate an api-bridge file, that works quite well. The only challange I had was, that fetching objects from api (e.g. for update), the fetched objects won’t get validated by aurelia validation.  
Following my current setup:

My NSwag-Generated API bridge:

```auto
...
export class IMyClass implements IMyClasss{
    ... // Nothing to show because it's automatically generated
}
...

```

To apply the aurelia-validation I’m extending each model from that generated file:

```auto
import {MyClass as DtoMyClass} from '/apis/myapi-bridge-file';

export class MyClass extends DtoMyClass {}

ValidationRules
    .ensure((e : MyClass) => e.someField).required()
    .on(MyClass)

```

That setup works quite well for e.g. creating new Objects. For updating it didn’t work because fetching the object the validation rules didn’t get applied. All fields were populated well.

```auto
public LoadMyClass(id: number){
    this.myClassClient.Get(id)
        .then(myclass => {
            this.Myclass = myclass;
        });
}

```

After some research I found out that (of course) the class needs to get reinstanciated because the generated-code-class doesn’t know anything about validation rules.  
Looking into the Swagger-generated class I saw, that the code always provides a constructor to recreate the object WITH validation rules. So, the solution for the challange was really easy:

```auto
public LoadMyClass(id: number){
    this.myClassClient.Get(id)
        .then(myclass => {
            this.MyClass = new MyClass(myclass); 
        });
}

```

And I’m so happy, that I didn’t need to create big and complex Mapping classes 🙂

---

<div class="post-metadata">

**Author:** ![elitemike](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitemike/32/690_2.png) [@elitemike](https://discourse.aurelia.io/u/elitemike)\
**Post date:** [November 23, 2018, 7:41pm UTC](https://discourse.aurelia.io/t/fetch-object-in-combindation-with-swagger-and-aurelia-validation/1941/2 "2018-11-23T19:41:47Z")

</div>

This might come in handy for my team, thanks!
