Skip to content

Commit

Permalink
feat: Allow JSON Schema Faker configuration in specification
Browse files Browse the repository at this point in the history
  • Loading branch information
shrink authored and EdVinyard committed Oct 21, 2021
1 parent a0f54eb commit b72dd03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions docs/guides/01-mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ The more descriptive your description is, the better job Prism can do at creatin

> **Tip:** If your team needs help creating better quality API description documents, take a look at [Spectral](https://stoplight.io/spectral/). You could enforce the use of `example` properties, or similar.

##### Configure JSON Schema Faker

At the top level of your API Specification, create an `x-json-schema-faker`
member containing a map of [JSON Schema Faker Options](https://github.com/json-schema-faker/json-schema-faker/tree/master/docs#available-options) and their values. An
additional `locale` option is accepted to configure the `locale` of the
underlying Faker instance.

```yaml
openapi: 3.1.0
x-json-schema-faker:
locale: de
min-items: 2
max-items: 10
resolve-json-path: true
```

### Request Validation

Having a mock server which only gave responses would not be very useful, which is why
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { dereference } from 'json-schema-ref-parser';
import { decycle } from '@stoplight/json';
import { get, camelCase, forOwn } from 'lodash';
import * as jsf from 'json-schema-faker';

export async function configureExtensionsFromSpec(specFilePathOrObject: string | object): Promise<void> {
const result = decycle(await dereference(specFilePathOrObject));

forOwn(get(result, 'x-json-schema-faker', {}), (value: any, option: string) => {
if (option === 'locale') return jsf.locate('faker').setLocale(value);

jsf.option(camelCase(option), value);
});
}
2 changes: 2 additions & 0 deletions packages/cli/src/util/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createExamplePath } from './paths';
import { attachTagsToParamsValues, transformPathParamsValues } from './colorizer';
import { CreatePrism } from './runner';
import { getHttpOperationsFromSpec } from '../operations';
import { configureExtensionsFromSpec } from '../extensions';

type PrismLogDescriptor = LogDescriptor & { name: keyof typeof LOG_COLOR_MAP; offset?: number; input: IHttpRequest };

Expand Down Expand Up @@ -66,6 +67,7 @@ const createSingleProcessPrism: CreatePrism = options => {

async function createPrismServerWithLogger(options: CreateBaseServerOptions, logInstance: Logger) {
const operations = await getHttpOperationsFromSpec(options.document);
await configureExtensionsFromSpec(options.document);

if (operations.length === 0) {
throw new Error('No operations found in the current file.');
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/mocker/generator/JSONSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker/locale/en_US';
import * as faker from 'faker';
import { cloneDeep } from 'lodash';
import { JSONSchema } from '../../types';

Expand Down

0 comments on commit b72dd03

Please sign in to comment.