diff --git a/docs/guides/01-mocking.md b/docs/guides/01-mocking.md index b9088a549..334578a84 100644 --- a/docs/guides/01-mocking.md +++ b/docs/guides/01-mocking.md @@ -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 diff --git a/packages/cli/src/extensions.ts b/packages/cli/src/extensions.ts new file mode 100644 index 000000000..4ae0c3734 --- /dev/null +++ b/packages/cli/src/extensions.ts @@ -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 { + 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); + }); +} diff --git a/packages/cli/src/util/createServer.ts b/packages/cli/src/util/createServer.ts index 449333210..8e598adec 100644 --- a/packages/cli/src/util/createServer.ts +++ b/packages/cli/src/util/createServer.ts @@ -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 }; @@ -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.'); diff --git a/packages/http/src/mocker/generator/JSONSchema.ts b/packages/http/src/mocker/generator/JSONSchema.ts index 8af707beb..2dc37b43a 100644 --- a/packages/http/src/mocker/generator/JSONSchema.ts +++ b/packages/http/src/mocker/generator/JSONSchema.ts @@ -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';