Skip to content

Commit 8d76e35

Browse files
committed
Use schemaCache for using seenSchemaCache in jsf
1 parent b7b194f commit 8d76e35

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

assets/json-schema-faker.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ var _ = require('lodash'),
1616
handleExclusiveMaximum,
1717
handleExclusiveMinimum
1818
} = require('./../lib/common/schemaUtilsCommon'),
19-
hash = require('object-hash'),
20-
seenSchemaMap = new Map();
19+
hash = require('object-hash');
2120

2221
(function (global, factory) {
2322
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@@ -24102,7 +24101,7 @@ function extend() {
2410224101
var nullType = nullGenerator;
2410324102

2410424103
// TODO provide types
24105-
function unique(path, items, value, sample, resolve, traverseCallback) {
24104+
function unique(path, items, value, sample, resolve, traverseCallback, seenSchemaCache) {
2410624105
var tmp = [], seen = [];
2410724106
function walk(obj) {
2410824107
var json = JSON.stringify(obj);
@@ -24115,15 +24114,15 @@ function extend() {
2411524114
// TODO: find a better solution?
2411624115
var limit = 10;
2411724116
while (tmp.length !== items.length) {
24118-
walk(traverseCallback(value.items || sample, path, resolve));
24117+
walk(traverseCallback(value.items || sample, path, resolve, null, seenSchemaCache));
2411924118
if (!limit--) {
2412024119
break;
2412124120
}
2412224121
}
2412324122
return tmp;
2412424123
}
2412524124
// TODO provide types
24126-
var arrayType = function arrayType(value, path, resolve, traverseCallback) {
24125+
var arrayType = function arrayType(value, path, resolve, traverseCallback, seenSchemaCache) {
2412724126
var items = [];
2412824127
if (!(value.items || value.additionalItems)) {
2412924128
if (utils.hasProperties(value, 'minItems', 'maxItems', 'uniqueItems')) {
@@ -24138,7 +24137,7 @@ function extend() {
2413824137
if (tmpItems instanceof Array) {
2413924138
return Array.prototype.concat.call(items, tmpItems.map(function (item, key) {
2414024139
var itemSubpath = path.concat(['items', key + '']);
24141-
return traverseCallback(item, itemSubpath, resolve);
24140+
return traverseCallback(item, itemSubpath, resolve, null, seenSchemaCache);
2414224141
}));
2414324142
}
2414424143
var minItems = value.minItems;
@@ -24167,11 +24166,11 @@ function extend() {
2416724166
sample = typeof value.additionalItems === 'object' ? value.additionalItems : {};
2416824167
for (var current = items.length; current < length; current++) {
2416924168
var itemSubpath = path.concat(['items', current + '']);
24170-
var element = traverseCallback(value.items || sample, itemSubpath, resolve);
24169+
var element = traverseCallback(value.items || sample, itemSubpath, resolve, null, seenSchemaCache);
2417124170
items.push(element);
2417224171
}
2417324172
if (value.uniqueItems) {
24174-
return unique(path.concat(['items']), items, value, sample, resolve, traverseCallback);
24173+
return unique(path.concat(['items']), items, value, sample, resolve, traverseCallback, seenSchemaCache);
2417524174
}
2417624175
return items;
2417724176
};
@@ -24235,7 +24234,7 @@ function extend() {
2423524234
var anyType = { type: ['string', 'number', 'integer', 'boolean'] };
2423624235
// TODO provide types
2423724236
// Updated objectType definition to latest version (0.5.0-rcv.41)
24238-
var objectType = function objectType(value, path, resolve, traverseCallback) {
24237+
var objectType = function objectType(value, path, resolve, traverseCallback, seenSchemaCache) {
2423924238
const props = {};
2424024239

2424124240
const properties = value.properties || {};
@@ -24271,7 +24270,7 @@ function extend() {
2427124270
}
2427224271
});
2427324272

24274-
return traverseCallback(props, path.concat(['properties']), resolve, value);
24273+
return traverseCallback(props, path.concat(['properties']), resolve, value, seenSchemaCache);
2427524274
}
2427624275

2427724276
const optionalsProbability = optionAPI('alwaysFakeOptionals') === true ? 1.0 : optionAPI('optionalsProbability');
@@ -24327,7 +24326,7 @@ function extend() {
2432724326

2432824327
return traverseCallback({
2432924328
allOf: _defns.concat(value),
24330-
}, path.concat(['properties']), resolve, value);
24329+
}, path.concat(['properties']), resolve, value, seenSchemaCache);
2433124330
}
2433224331
}
2433324332

@@ -24480,7 +24479,7 @@ function extend() {
2448024479
}
2448124480
}
2448224481

24483-
return traverseCallback(props, path.concat(['properties']), resolve, value);
24482+
return traverseCallback(props, path.concat(['properties']), resolve, value, seenSchemaCache);
2448424483
};
2448524484

2448624485
/**
@@ -24666,7 +24665,7 @@ function extend() {
2466624665
};
2466724666

2466824667
// TODO provide types
24669-
function traverse(schema, path, resolve, rootSchema) {
24668+
function traverse(schema, path, resolve, rootSchema, seenSchemaCache) {
2467024669
schema = resolve(schema);
2467124670
if (!schema) {
2467224671
return;
@@ -24677,8 +24676,8 @@ function extend() {
2467724676
isExampleValid,
2467824677
hashSchema = hash(schema);
2467924678

24680-
if(seenSchemaMap.has(hashSchema)) {
24681-
isExampleValid = seenSchemaMap.get(hashSchema);
24679+
if(seenSchemaCache && seenSchemaCache.has(hashSchema)) {
24680+
isExampleValid = seenSchemaCache.get(hashSchema);
2468224681
}
2468324682
else {
2468424683
// avoid minItems and maxItems while checking for valid examples
@@ -24697,7 +24696,7 @@ function extend() {
2469724696

2469824697
// Store the final result that needs to be used in the seen map
2469924698
isExampleValid = result && result.length === 0;
24700-
seenSchemaMap.set(hashSchema, isExampleValid);
24699+
seenSchemaCache && seenSchemaCache.set(hashSchema, isExampleValid);
2470124700
}
2470224701

2470324702
// Use example only if valid
@@ -24720,7 +24719,7 @@ function extend() {
2472024719
}
2472124720
// thunks can return sub-schemas
2472224721
if (typeof schema.thunk === 'function') {
24723-
return traverse(schema.thunk(), path, resolve);
24722+
return traverse(schema.thunk(), path, resolve, null, seenSchemaCache);
2472424723
}
2472524724
if (typeof schema.generate === 'function') {
2472624725
return utils.typecast(schema, function () { return schema.generate(rootSchema); });
@@ -24748,7 +24747,7 @@ function extend() {
2474824747
}
2474924748
else {
2475024749
try {
24751-
var result = typeMap[type](schema, path, resolve, traverse);
24750+
var result = typeMap[type](schema, path, resolve, traverse, seenSchemaCache);
2475224751
var required = schema.items
2475324752
? schema.items.required
2475424753
: schema.required;
@@ -24768,7 +24767,7 @@ function extend() {
2476824767
}
2476924768
for (var prop in schema) {
2477024769
if (typeof schema[prop] === 'object' && prop !== 'definitions') {
24771-
copy[prop] = traverse(schema[prop], path.concat([prop]), resolve, copy);
24770+
copy[prop] = traverse(schema[prop], path.concat([prop]), resolve, copy, seenSchemaCache);
2477224771
}
2477324772
else {
2477424773
copy[prop] = schema[prop];
@@ -24838,7 +24837,7 @@ function extend() {
2483824837
return obj;
2483924838
}
2484024839
// TODO provide types
24841-
function run(refs, schema, container) {
24840+
function run(refs, schema, container, seenSchemaCache) {
2484224841
try {
2484324842
var result = traverse(schema, [], function reduce(sub, maxReduceDepth) {
2484424843
if (typeof maxReduceDepth === 'undefined') {
@@ -24907,7 +24906,7 @@ function extend() {
2490724906
}
2490824907
}
2490924908
return container.wrap(sub);
24910-
});
24909+
}, null, seenSchemaCache);
2491124910
if (optionAPI('resolveJsonPath')) {
2491224911
return resolve(result);
2491324912
}
@@ -24949,7 +24948,7 @@ function extend() {
2494924948
}
2495024949
}
2495124950
}
24952-
var jsf = function (schema, refs) {
24951+
var jsf = function (schema, refs, seenSchemaCache) {
2495324952
var ignore = optionAPI('ignoreMissingRefs');
2495424953
var $ = deref(function (id, refs) {
2495524954
// FIXME: allow custom callback?
@@ -24958,7 +24957,7 @@ function extend() {
2495824957
}
2495924958
});
2496024959
var $refs = getRefs(refs);
24961-
return run($refs, $(schema, $refs, true), container);
24960+
return run($refs, $(schema, $refs, true), container, seenSchemaCache);
2496224961
};
2496324962
jsf.resolve = function (schema, refs, cwd) {
2496424963
if (typeof refs === 'string') {

lib/schemaUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function safeSchemaFaker(oldSchema, resolveTo, resolveFor, parameterSourceOption
197197
return fakedSchema;
198198
}
199199
// for JSON, the indentCharacter will be applied in the JSON.stringify step later on
200-
fakedSchema = schemaFaker(resolvedSchema);
200+
fakedSchema = schemaFaker(resolvedSchema, null, _.get(schemaCache, 'schemaValidationCache'));
201201
schemaFakerCache[key] = fakedSchema;
202202
return fakedSchema;
203203
}

lib/schemapack.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SchemaPack {
4646
this.computedOptions = null;
4747
this.schemaFakerCache = {};
4848
this.schemaResolutionCache = {};
49+
this.schemaValidationCache = new Map();
4950

5051
this.computedOptions = utils.mergeOptions(
5152
// predefined options
@@ -260,7 +261,8 @@ class SchemaPack {
260261
authHelper,
261262
schemaCache = {
262263
schemaResolutionCache: this.schemaResolutionCache,
263-
schemaFakerCache: this.schemaFakerCache
264+
schemaFakerCache: this.schemaFakerCache,
265+
schemaValidationCache: this.schemaValidationCache
264266
};
265267

266268
if (!this.validated) {

0 commit comments

Comments
 (0)