Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sampler): introduce minLength support for string pattern format sample #217

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/openapi-sampler/src/samplers/StringSampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export class StringSampler implements Sampler {
'relative-json-pointer': () => '1/relative/json/pointer',
'regex': () => '/regex/',
'pattern': (
_min: number,
min: number,
max: number,
{ pattern }: { pattern: string | RegExp }
) => this.patternSample(pattern, max),
) => this.patternSample(pattern, min, max),
'default': (min: number, max: number) =>
this.adjustLength('lorem', min, max)
};
Expand All @@ -47,11 +47,23 @@ export class StringSampler implements Sampler {
return this.checkLength(sampler(min || 0, max, schema), format, min, max);
}

private patternSample(pattern: string | RegExp, max?: number): string {
private patternSample(
pattern: string | RegExp,
min?: number,
max?: number
): string {
const randExp = new RandExp(pattern);
randExp.randInt = (a, b) => Math.floor((a + b) / 2);

if (min) {
randExp.max = 2 * min;

const result = randExp.gen();

return max && result.length > max ? result.substring(0, max) : result;
pmstss marked this conversation as resolved.
Show resolved Hide resolved
}

randExp.max = max ?? randExp.max;
randExp.randInt = (a, b) => Math.floor((a + b) / 2);

return randExp.gen();
}
Expand Down
60 changes: 60 additions & 0 deletions packages/openapi-sampler/tests/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ describe('StringSampler', () => {
},
expected: 'EEEEE44444'
},
{
input: {
maxLength: 4,
minLength: 4,
pattern: '^[0-9]+$',
type: 'string'
},
expected: '4444'
},
{
input: {
minLength: 5,
maxLength: 6,
pattern: '^[0-9]+$',
type: 'string'
},
expected: '444444'
},
{
input: {
minLength: 5,
maxLength: 100,
pattern: '^[0-9]+$',
type: 'string'
},
expected: '444444'
},
{
input: {
minLength: 4,
pattern: '^[0-9]+$',
type: 'string'
},
expected: '44444'
},
{
input: {
minLength: 4,
pattern: '^[0-9]{4,5}$',
type: 'string'
},
expected: '4444'
},
{
input: {
minLength: 5,
pattern: '^[0-9]{3,5}$[A-Z]{3}',
type: 'string'
},
expected: '4444MMM'
},
{
input: {
type: 'string',
Expand Down Expand Up @@ -266,6 +317,15 @@ describe('StringSampler', () => {
expected:
'Sample string cannot be generated by boundaries: maxLength=5, format=pattern'
},
{
input: {
minLength: 5,
pattern: '^[0-9]{3}',
type: 'string'
},
expected:
'Sample string cannot be generated by boundaries: minLength=5, format=pattern'
},
{
input: {
type: 'string',
Expand Down