Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 31 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `/**
Expand Down
Loading