Skip to content

Commit

Permalink
Merge pull request #720 from postmanlabs/feature/fix-stuck-conversion
Browse files Browse the repository at this point in the history
Fixed issue where conversion was stuck for certain schemas with pattern.
  • Loading branch information
VShingala committed May 12, 2023
2 parents 22871ad + 9efb817 commit 4f2dc7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion assets/json-schema-faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23846,8 +23846,10 @@ function extend() {
/**
* CHANGE: This Makes sure that we're not adding extra spaces in generated value,
* As such behaviour generates invalid data when pattern is mentioned.
*
* To avoid infinite loop, make sure we keep adding spaces in cases where value is empty string
*/
value += (schema.pattern ? '' : ' ') + value;
value += ((schema.pattern && value.length !== 0) ? '' : ' ') + value;
}
if (value.length > max) {
value = value.substr(0, max);
Expand Down
15 changes: 15 additions & 0 deletions test/unit/faker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,19 @@ describe('JSON SCHEMA FAKER TESTS', function () {
expect(value.name).to.be.a('string');
});
});

it('Should successsfully generate data for certain patterns that can generate empty string', function () {
const schema = {
'maxLength': 63,
'minLength': 2,
'pattern': '^[A-Za-z !#$%&0-9,\'*+\\-.()/:;=@\\\\_\\[\\]`{}]*$',
'type': 'string',
'description': 'The exact name on the credit card.'
};

var fakedData = schemaFaker(schema);
expect(fakedData).to.be.an('string');
expect(fakedData.length >= 2).to.be.true;
expect(fakedData.length <= 63).to.be.true;
});
});

0 comments on commit 4f2dc7f

Please sign in to comment.