Skip to content

Commit

Permalink
fix(ajv): Fix Ajv Injection in AjvValidationPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis committed May 5, 2020
1 parent 5b375e8 commit 2a7690b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Expand Up @@ -12,9 +12,7 @@ node_js:
- 12
- 10

script:
- yarn test
- yarn travis:coveralls
script: yarn test && (yarn travis:coveralls || true)

jobs:
include:
Expand Down
6 changes: 3 additions & 3 deletions packages/ajv/src/pipes/AjvValidationPipe.ts
@@ -1,6 +1,6 @@
import {ConverterService, getJsonSchema, Inject, IPipe, OverrideProvider, ParamMetadata, ParamTypes, ValidationPipe} from "@tsed/common";
import {isEmpty} from "@tsed/core";
import {Ajv} from "../services/Ajv";
import {AJV} from "../services/Ajv";
import {AjvErrorFormatterPipe} from "./AjvErrorFormatterPipe";

@OverrideProvider(ValidationPipe)
Expand All @@ -11,8 +11,8 @@ export class AjvValidationPipe extends ValidationPipe implements IPipe {
@Inject()
formatter: AjvErrorFormatterPipe;

@Inject()
ajv: Ajv;
@Inject(AJV)
ajv: AJV;

transform(value: any, metadata: ParamMetadata): any {
const {schema} = metadata.store.get(AjvValidationPipe) || {};
Expand Down
4 changes: 2 additions & 2 deletions packages/ajv/src/services/Ajv.spec.ts
@@ -1,13 +1,13 @@
import {TestContext} from "@tsed/testing";
import * as AjvKlass from "ajv";
import {expect} from "chai";
import {Ajv} from "./Ajv";
import {AJV} from "./Ajv";

describe("Ajv", () => {
beforeEach(() => TestContext.create());
afterEach(() => TestContext.reset());
it("should create a new Ajv instance", async () => {
const ajv = await TestContext.invoke<Ajv>(Ajv);
const ajv = await TestContext.invoke<AJV>(AJV);

expect(ajv).to.instanceof(AjvKlass);
expect(
Expand Down
10 changes: 5 additions & 5 deletions packages/ajv/src/services/Ajv.ts
@@ -1,19 +1,19 @@
import {Configuration, ProviderScope, registerProvider} from "@tsed/di";
import * as AjvKlass from "ajv";
import * as Ajv from "ajv";
import {IAjvSettings} from "../interfaces/IAjvSettings";

// tslint:disable-next-line:variable-name
export const Ajv: any = AjvKlass;
export type Ajv = AjvKlass.Ajv;
export const AJV: any = Symbol.for("AJV");
export type AJV = Ajv.Ajv;

registerProvider({
provide: Ajv,
provide: AJV,
deps: [Configuration],
scope: ProviderScope.SINGLETON,
useFactory(configuration: Configuration) {
const {errorFormat, errorFormatter, options = {}, ...props} = configuration.get<IAjvSettings>("ajv") || {};

return new AjvKlass({
return new Ajv({
verbose: false,
...props,
...options
Expand Down

0 comments on commit 2a7690b

Please sign in to comment.