Skip to content

Commit 4fafe00

Browse files
committed
Use a local cache for checking the validation of schema for each example
1 parent 7bd21b7 commit 4fafe00

File tree

3 files changed

+51
-33
lines changed

3 files changed

+51
-33
lines changed

assets/json-schema-faker.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var _ = require('lodash'),
1515
{
1616
handleExclusiveMaximum,
1717
handleExclusiveMinimum
18-
} = require('./../lib/common/schemaUtilsCommon');
18+
} = require('./../lib/common/schemaUtilsCommon'),
19+
hash = require('object-hash'),
20+
seenSchemaMap = new Map();
1921

2022
(function (global, factory) {
2123
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@@ -24667,28 +24669,38 @@ function extend() {
2466724669
function traverse(schema, path, resolve, rootSchema) {
2466824670
schema = resolve(schema);
2466924671
if (!schema) {
24670-
return;
24672+
return;
2467124673
}
2467224674
if (optionAPI('useExamplesValue') && 'example' in schema) {
2467324675
var clonedSchema,
24674-
result;
24676+
result,
24677+
finalResult,
24678+
hashSchema = hash(schema);
2467524679

24676-
// avoid minItems and maxItems while checking for valid examples
24677-
if (optionAPI('avoidExampleItemsLength') && _.get(schema, 'type') === 'array') {
24678-
clonedSchema = _.clone(schema);
24679-
_.unset(clonedSchema, 'minItems');
24680-
_.unset(clonedSchema, 'maxItems');
24681-
24682-
// avoid validation of values that are in pm variable format (i.e. '{{userId}}')
24683-
result = validateSchema(clonedSchema, schema.example, { ignoreUnresolvedVariables: true });
24680+
if(seenSchemaMap.has(hashSchema)) {
24681+
finalResult = seenSchemaMap.get(hashSchema);
2468424682
}
2468524683
else {
24686-
// avoid validation of values that are in pm variable format (i.e. '{{userId}}')
24687-
result = validateSchema(schema, schema.example, { ignoreUnresolvedVariables: true });
24684+
// avoid minItems and maxItems while checking for valid examples
24685+
if (optionAPI('avoidExampleItemsLength') && _.get(schema, 'type') === 'array') {
24686+
clonedSchema = _.clone(schema);
24687+
_.unset(clonedSchema, 'minItems');
24688+
_.unset(clonedSchema, 'maxItems');
24689+
24690+
// avoid validation of values that are in pm variable format (i.e. '{{userId}}')
24691+
result = validateSchema(clonedSchema, schema.example, { ignoreUnresolvedVariables: true });
24692+
}
24693+
else {
24694+
// avoid validation of values that are in pm variable format (i.e. '{{userId}}')
24695+
result = validateSchema(schema, schema.example, { ignoreUnresolvedVariables: true });
24696+
}
2468824697
}
2468924698

24699+
finalResult = result && result.length === 0;
24700+
24701+
seenSchemaMap.set(hashSchema, finalResult);
2469024702
// Use example only if valid
24691-
if (result && result.length === 0) {
24703+
if (finalResult) {
2469224704
return schema.example;
2469324705
}
2469424706
}

package-lock.json

Lines changed: 24 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"json-schema-merge-allof": "0.8.1",
125125
"lodash": "4.17.21",
126126
"oas-resolver-browser": "2.5.6",
127+
"object-hash": "3.0.0",
127128
"path-browserify": "1.0.1",
128129
"postman-collection": "4.1.5",
129130
"swagger2openapi": "7.0.8",

0 commit comments

Comments
 (0)