function generateExampleJson(schema) { let example; switch (schema.type) { case 'object': example = {}; if (schema.properties) { Object.keys(schema.properties).forEach(key => { if (schema.required && schema.required.includes(key)) { example[key] = generateExampleJson(schema.properties[key]); } else { // Optional if (Math.random() < 0.5) { example[key] = generateExampleJson(schema.properties[key]); } } }); } break; case 'array': example = []; const numItems = schema.minItems || Math.round(Math.random() * 10); for (let i = 0; i < numItems; i++) { let itemExample = generateExampleJson(schema.items); example.push(itemExample); } break; case 'string': if (schema.enum) { const index = Math.floor(Math.random() * schema.enum.length); example = schema.enum[index] } else { // Generate between 1 and 10 'words' const numwords = Math.floor(Math.random() * 10) + 1; let sentence = ''; for (i = 0; i < numwords; i++) { const numletters = Math.floor(Math.random() * 10) + 1; for (j = 0; j < numletters; j++) { sentence += String.fromCharCode(97 + Math.floor(Math.random() * 26)); } sentence += ' '; } example = sentence; // 'example string'; } break; case 'number': case 'integer': const minimum = schema.minimum !== undefined ? schema.minimum : 0; const maximum = schema.maximum !== undefined ? schema.maximum : 100; example = Math.floor(Math.random() * (maximum - minimum + 1) + minimum); break; case 'boolean': example = Math.random() < 0.5; break; default: example = null; } // Handle oneOf by selecting the first option as an example if (schema.oneOf && Array.isArray(schema.oneOf)) { const index = Math.floor(Math.random() * schema.oneOf.length) example = generateExampleJson(schema.oneOf[index]); } // Handle enum by selecting the first enum value if not already handled if (schema.enum && example === null) { const index = Math.floor(Math.random() * schema.enum.length); example = schema.enum[index]; } return example; }