Skip to content

Commit 4f2dc7f

Browse files
authored
Merge pull request #720 from postmanlabs/feature/fix-stuck-conversion
Fixed issue where conversion was stuck for certain schemas with pattern.
2 parents 22871ad + 9efb817 commit 4f2dc7f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

assets/json-schema-faker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23846,8 +23846,10 @@ function extend() {
2384623846
/**
2384723847
* CHANGE: This Makes sure that we're not adding extra spaces in generated value,
2384823848
* As such behaviour generates invalid data when pattern is mentioned.
23849+
*
23850+
* To avoid infinite loop, make sure we keep adding spaces in cases where value is empty string
2384923851
*/
23850-
value += (schema.pattern ? '' : ' ') + value;
23852+
value += ((schema.pattern && value.length !== 0) ? '' : ' ') + value;
2385123853
}
2385223854
if (value.length > max) {
2385323855
value = value.substr(0, max);

test/unit/faker.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,19 @@ describe('JSON SCHEMA FAKER TESTS', function () {
121121
expect(value.name).to.be.a('string');
122122
});
123123
});
124+
125+
it('Should successsfully generate data for certain patterns that can generate empty string', function () {
126+
const schema = {
127+
'maxLength': 63,
128+
'minLength': 2,
129+
'pattern': '^[A-Za-z !#$%&0-9,\'*+\\-.()/:;=@\\\\_\\[\\]`{}]*$',
130+
'type': 'string',
131+
'description': 'The exact name on the credit card.'
132+
};
133+
134+
var fakedData = schemaFaker(schema);
135+
expect(fakedData).to.be.an('string');
136+
expect(fakedData.length >= 2).to.be.true;
137+
expect(fakedData.length <= 63).to.be.true;
138+
});
124139
});

0 commit comments

Comments
 (0)