Search code examples
javascriptjsonangularjson-schema-faker

How to implement JSON Schema Faker with Angular 4 and above


Is it possible to use JSON Schema faker as a third party dependency in Angular. I tried to use the dependency injection to Angular however in the providers I am not able to import jsonSchemaFaker.

angular.json

"scripts": [
    "./node_modules/json-schema-faker/dist/json-schema-faker.bundle.min.js"
]

jsonSchemaFaker.service.ts

import { InjectionToken } from '@angular/core';
export const JSF_Token = new InjectionToken ('jsonSchemaFaker');

app.module.ts

providers: [
    { provide: JSF_Token, useValue: jsf }
]

...

declare let jsf: any;

This is what I tried to Inject json schema faker as a dependency in my angular app.. I am getting .. Uncaught ReferenceError: jsf is not defined


Solution

  • I had to change my providers and the declaration to

    providers: [
        { provide: JSF_Token, useValue: JSONSchemaFaker }
    ]
    
    declare let JSONSchemaFaker: any;
    

    Reason: the global name for JSON Schema Faker mentioned in that library is "JSONSchemaFaker". It was a mistake on my part to declare it as jsf.