Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0be47a6

Browse files
karol-maciaszekXVincentX
authored andcommittedOct 14, 2019
fix: support for pattern in random path generation (stoplightio#699)
1 parent bde3903 commit 0be47a6

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
 

‎packages/http/src/mocker/generator/HttpParamGenerator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { pipe } from 'fp-ts/lib/pipeable';
44
import { JSONSchema } from '../../types';
55
import { generate as generateDynamicExample } from './JSONSchema';
66

7-
function improveSchema(schema: JSONSchema) {
7+
export function improveSchema(schema: JSONSchema) {
88
const newSchema = { ...schema };
99

1010
if (newSchema.type === 'integer' || newSchema.type === 'number') {
@@ -18,7 +18,7 @@ function improveSchema(schema: JSONSchema) {
1818
}
1919

2020
if (newSchema.type === 'string') {
21-
if (!newSchema.format && !newSchema.enum) {
21+
if (!newSchema.format && !newSchema.enum && !newSchema.pattern) {
2222
newSchema['x-faker'] = 'lorem.word';
2323
}
2424
}

‎packages/http/src/mocker/generator/__tests__/HttpParamGenerator.spec.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertNone, assertSome } from '@stoplight/prism-core/src/utils/__tests__/utils';
22
import { HttpParamStyles } from '@stoplight/types';
3-
import { generate } from '../HttpParamGenerator';
3+
import { generate, improveSchema } from '../HttpParamGenerator';
44

55
describe('HttpParamGenerator', () => {
66
describe('generate()', () => {
@@ -50,4 +50,50 @@ describe('HttpParamGenerator', () => {
5050
});
5151
});
5252
});
53+
54+
describe('improveSchema()', () => {
55+
describe.each(['number', 'integer'])('when feed with a %s', type => {
56+
// @ts-ignore
57+
const improvedSchema = improveSchema({ type });
58+
59+
it('should have a minimum and a maximum', () => {
60+
expect(improvedSchema).toHaveProperty('minimum', 1);
61+
expect(improvedSchema).toHaveProperty('maximum', 1000);
62+
});
63+
});
64+
65+
describe('when feed with string', () => {
66+
describe('no format and no enum', () => {
67+
const improvedSchema = improveSchema({ type: 'string' });
68+
69+
it('should have the x-faker extension', () => {
70+
expect(improvedSchema).toHaveProperty('x-faker', 'lorem.word');
71+
});
72+
});
73+
74+
describe.each<{ 0: string; 1: object }>([['format', { format: 'email' }], ['enum', { enum: [1, 2, 3] }], ['pattern', { pattern: '^[A-Z]+$' }]])(
75+
'when with %s',
76+
(_a, additional) => {
77+
const improvedSchema = improveSchema({ type: 'string', ...additional });
78+
79+
it('should not have the x-faker extension', () => expect(improvedSchema).not.toHaveProperty('x-faker'));
80+
},
81+
);
82+
});
83+
84+
describe('when feed with object', () => {
85+
describe('no format and no enum', () => {
86+
const improvedSchema = improveSchema({
87+
type: 'object',
88+
properties: { a: { type: 'string' }, b: { type: 'number' } },
89+
});
90+
91+
it('will recursively improve the schema', () => {
92+
expect(improvedSchema).toHaveProperty('properties.a.x-faker', 'lorem.word');
93+
expect(improvedSchema).toHaveProperty('properties.b.minimum', 1);
94+
expect(improvedSchema).toHaveProperty('properties.b.maximum', 1000);
95+
});
96+
});
97+
});
98+
});
5399
});

0 commit comments

Comments
 (0)
Failed to load comments.