Skip to content

Commit

Permalink
document, test per-property faker configuration (stoplightio#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdVinyard authored Mar 18, 2022
1 parent ea5b445 commit cd877dd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/guides/01-mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ 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.

##### Control Generated Fakes for Individual Properties

In the following example there are two properties, each with specific Faker parameters. [datatype.number](https://fakerjs.dev/api/datatype.html#number) uses named parameters while [helpers.slugify](https://fakerjs.dev/api/helpers.html#slugify) uses positional parameters.

```yaml
example:
type: object
properties:
ten-or-eleven:
type: number
example: 10
x-faker:
datatype.number:
min: 10
max: 11
slug:
type: string
example: two-words
x-faker:
helpers.slugify: [ "two words" ]
```

##### Configure JSON Schema Faker

At the top level of your API Specification, create an `x-json-schema-faker`
Expand Down
47 changes: 47 additions & 0 deletions packages/http/src/mocker/generator/__tests__/JSONSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,53 @@ describe('JSONSchema generator', () => {
});
});

describe('when faker is configured per-property', () => {
it('with named parameters', () => {
const schema: JSONSchema & any = {
type: 'object',
properties: {
meaning: {
type: 'number',
'x-faker': {
'random.number': {
min: 42,
max: 42,
},
},
},
},
required: ['meaning'],
};

assertRight(generate({}, schema), instance => {
expect(instance).toHaveProperty('meaning');
const actual = get(instance, 'meaning');
expect(actual).toStrictEqual(42);
});
});

it('with positional parameters', () => {
const schema: JSONSchema & any = {
type: 'object',
properties: {
slug: {
type: 'string',
'x-faker': {
'helpers.slugify': ['two words'],
},
},
},
required: ['slug'],
};

assertRight(generate({}, schema), instance => {
expect(instance).toHaveProperty('slug');
const actual = get(instance, 'slug');
expect(actual).toStrictEqual('two-words');
});
});
});

describe('when used with a schema that is not valid', () => {
const schema: JSONSchema = {
type: 'object',
Expand Down

0 comments on commit cd877dd

Please sign in to comment.