diff --git a/index.js b/index.js index e3ff7ae..75ed892 100644 --- a/index.js +++ b/index.js @@ -127,7 +127,20 @@ function processProperties (schema, rootSchema, base, config) { result.push(...writeProperty('object', prefixedProperty, prop.description, optional, defaultValue, schema, config)) result.push(...processProperties(prop, rootSchema, prefixedProperty, config)) } else if (prop.type === 'array' && prop.items) { - result.push(...writeProperty('array', prefixedProperty, prop.description, optional, defaultValue, schema, config)) + let type = 'array' + if (prop.items.type === 'object') { + const objectPropDefs = [] + Object.entries(prop.items.properties).forEach(([k, p]) => { + const jsType = p.type || '*' + if (prop.items.required && prop.items.required.includes(k)) { + objectPropDefs.push(`${k}:${jsType}`) + } else { + objectPropDefs.push(`${k}?:${jsType}`) + } + }) + type = `{${objectPropDefs.join(',')}}[]` + } + result.push(...writeProperty(type, prefixedProperty, prop.description, optional, defaultValue, schema, config)) result.push(...processItems(prop, rootSchema, prefixedProperty, config)) } else { const type = getSchemaType(prop, rootSchema) || getDefaultPropertyType(config, property) diff --git a/test.js b/test.js index 3334122..bf28910 100644 --- a/test.js +++ b/test.js @@ -31,7 +31,37 @@ describe('Simple schemas', () => { ` expect(generate(schema)).toEqual(expected) }) - + it('Array of objects', function () { + const schema = { + type: 'object', + properties: { + arrayTest: { + type: 'array', + items: { + type: 'object', + required: ['a', 'b'], + properties: { + a: { + type: 'string' + }, + b: { + type: 'number' + }, + c: { + type: 'number' + } + } + } + } + } + } + const expected = `/** + * @typedef {object} + * @property {{a:string,b:number}[]} [arrayTest] + */ +` + expect(generate(schema)).toEqual(expected) + }) it('Simple string with description', function () { const schema = { type: 'string', description: 'String description' } const expected = `/**