Skip to content

Commit 95698b6

Browse files
committed
improved example data generation, respecting the constraints
1 parent ed8a698 commit 95698b6

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/utils/schema-utils.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,24 @@ export function getSampleValueByType(schemaObj) {
111111
return schemaObj.$ref;
112112
}
113113
const typeValue = Array.isArray(schemaObj.type) ? schemaObj.type[0] : schemaObj.type;
114-
if (typeValue.match(/^integer/g)) { return 0; }
115-
if (typeValue.match(/^number/g)) { return 0.5; }
114+
115+
if (typeValue.match(/^integer|^number/g)) {
116+
const multipleOf = Number.isNaN(Number(schemaObj.multipleOf)) ? undefined : Number(schemaObj.multipleOf);
117+
const maximum = Number.isNaN(Number(schemaObj.maximum)) ? undefined : Number(schemaObj.maximum);
118+
const minimumPossibleVal = Number.isNaN(Number(schemaObj.minimum))
119+
? Number.isNaN(Number(schemaObj.exclusiveMinimum))
120+
? maximum || 0
121+
: Number(schemaObj.exclusiveMinimum) + (typeValue.startsWith('integer') ? 1 : 0.001)
122+
: Number(schemaObj.minimum);
123+
const finalVal = multipleOf
124+
? multipleOf >= minimumPossibleVal
125+
? multipleOf
126+
: minimumPossibleVal % multipleOf === 0
127+
? minimumPossibleVal
128+
: Math.ceil(minimumPossibleVal / multipleOf) * multipleOf
129+
: minimumPossibleVal;
130+
return finalVal;
131+
}
116132
if (typeValue.match(/^boolean/g)) { return false; }
117133
if (typeValue.match(/^null/g)) { return null; }
118134
if (typeValue.match(/^string/g)) {
@@ -145,8 +161,10 @@ export function getSampleValueByType(schemaObj) {
145161
return schemaObj.format;
146162
}
147163
} else {
148-
// TODO: check for min and max length
149-
return 'string';
164+
const minLength = Number.isNaN(schemaObj.minLength) ? undefined : Number(schemaObj.minLength);
165+
const maxLength = Number.isNaN(schemaObj.maxLength) ? undefined : Number(schemaObj.maxLength);
166+
const finalLength = minLength || (maxLength > 6 ? 6 : maxLength || undefined);
167+
return finalLength ? 'A'.repeat(finalLength) : 'string';
150168
}
151169
}
152170
// If type cannot be determined
@@ -227,17 +245,18 @@ function addPropertyExampleToObjectExamples(example, obj, propertyKey) {
227245
function mergePropertyExamples(obj, propertyName, propExamples) {
228246
// Create an example for each variant of the propertyExample, merging them with the current (parent) example
229247
let i = 0;
248+
const maxCombinations = 10;
230249
const mergedObj = {};
231250
for (const exampleKey in obj) {
232251
for (const propExampleKey in propExamples) {
233252
mergedObj[`example-${i}`] = { ...obj[exampleKey] };
234253
mergedObj[`example-${i}`][propertyName] = propExamples[propExampleKey];
235254
i++;
236-
if (i >= 10) {
255+
if (i >= maxCombinations) {
237256
break;
238257
}
239258
}
240-
if (i >= 10) {
259+
if (i >= maxCombinations) {
241260
break;
242261
}
243262
}

0 commit comments

Comments
 (0)