Custom validator

Ts.ED provide by default a AJV package to perform a validation on a Model. But, you can choose another library as model validator.

To do that, you need to create a custom validation service that will inherit from the ValidationService and override this service with the OverrideService decorator.

Create your service

In your project, create a new file named CustomValidationService.ts and create a class based on this example:

import {BadRequest} from "ts-httpexceptions";
import {OverrideService, JsonSchemesService, ValidationService} from "@tsed/common";

@OverrideService(ValidationService)
export class CustomValidationService extends ValidationService {
    constructor(private jsonSchemaService: JsonSchemesService) {
        super();
    }

    public validate(obj: any, targetType: any, baseType?: any): void {
        // JSON service contain tool to build the Schema definition of a model.
        const schema = this.jsonSchemaService.getSchemaDefinition(targetType);

        if (schema) {
            const valid = myLibraryValidation.validate(schema, obj);

            if (!valid) {
                throw(new BadRequest(`{{name}} is wrong`));
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Import your service

Edit your server.ts and import manually your CustomValidationService:

import {ServerLoader, ServerSettings} from "@tsed/common";
import "./services/override/CustomValidationService";

@ServerSettings({
  // ...
})
export class Server extends ServerLoader {
    
}
1
2
3
4
5
6
7
8
9

Now your custom validation service will be used when a model must be validated.