Prism - Dynamic mocks with Faker.js

Is it possible to use Faker.js properties in Prism?

Example: https://json-schema-faker.js.org/#gist/1902737e7bef9573af02a3fc49761c13

I see Prism uses (currently a custom version of) json-schema-faker to create dynamic and static mocks of the OpenApi schema. It however does not extend the faker keyword, as you can see in JSONSchema.ts

Is it because the keyword faker is not part of the OpenAPI JSONSchema spec?

1 Like

Hey @mrtnvh,

thanks for the message! I see this is your first one in our forum so welcome!

Let’s unpack your questions:

I see Prism uses (currently a custom version of) json-schema-faker

This was true until…today. We’ve just been able to contribute back to the original project removing a dependency that was a bit problematic, so Prism right now is using the official project again.

It however does not extend the faker keyword

Correct. This keyword does not exist nor in the OpenAPI’s JSON Schema Version they use (draft4 for OAS2, draft 5 for OAS3) nor in any other newer draft that hopefully Open API 3.1 will effectively use (draft8?) — therefore we do not support it.

That said — since it’s JavaScript I think you can technically still provide such keyword and, given the input goes to JSON Schema Faker anyway, you might be able to get the narrowed data type you are looking for. Did you try it?

V.

1 Like

Hey Vincenzo,

Thank you for your swift answer.

This was true until…today. We’ve just been able to contribute back to the original project removing a dependency that was a bit problematic, so Prism right now is using the official project again.

Ok. I pulled in the latest commit in my fork.

Correct. This keyword does not exist nor in the OpenAPI’s JSON Schema Version they use (draft4 for OAS2, draft 5 for OAS3) nor in any other newer draft that hopefully Open API 3.1 will effectively use (draft8?) — therefore we do not support it.

Correct. The faker keyword is not a standard keyword supported by the OpenAPI JSON Schema.

However, the OpenAPI spec allows extensions, which on it’s turn, JSON-schema-faker supports, as seen in this issue.

That said — since it’s JavaScript I think you can technically still provide such keyword and, given the input goes to JSON Schema Faker anyway, you might be able to get the narrowed data type you are looking for. Did you try it?

To get this working within Prism, you would only need to extend JSON-schema-faker in JSONSchema.ts to use Faker.js

I created a PR with the necessary changes to get faker support up and running. I propose we continue this conversation there.

1 Like

For the records — the initial PR that @mrtnvh put together has now been merged and the feature will be released soon.

This is a double win!

1 Like