Useful when you already have a JSON Schema and want to document the types you want to validate. Works with subschema definitions.
const jsdoc = require('json-schema-to-jsdoc');
const schema = {
"title": "Person",
"type": "object",
"properties": {
"name": {"type": "string", "description": "A person's name"},
"age": {"type": "integer", "description": "A person's age"}
},
"required" : ["name"]
}
jsdoc(schema /* , optionsObject */);/**
* @typedef {object}
* @property {string} name A person's name
* @property {integer} [age] A person's age
*/jsdoc(schema, {
hyphenatedDescriptions: true
});/**
* @typedef {object}
* @property {string} name - A person's name
* @property {integer} [age] - A person's age
*/jsdoc(schema, {
autoDescribe: true
});/**
* Represents a Person object
* @typedef {object}
* @property {string} name A person's name
* @property {integer} [age] A person's age
*/jsdoc(schema, {
types: {
object: 'PlainObject'
}
});/**
* Represents a Person object
* @typedef {PlainObject}
* @property {string} name A person's name
* @property {integer} [age] A person's age
*/addDescriptionLineBreak: boolean- Inserts an empty line whenautoDescribeisfalseand the schemadescriptionis empty. Defaults tofalse.autoDescribe: boolean- Adds a description ("Represents a/n [<title> ]<type>") when the schema has nodescription. Defaults tofalse.capitalizeProperty: boolean- WhenpropertyNameAsTypeistrue, capitalizes the property-as-type, i.e.,MyTitlein@property {MyTitle} myTitle. Defaults tofalse.capitalizeTitle: boolean- If a schematitleis present, capitalizes the schema'stitlein the output of@typedef {myType} title. Defaults tofalse.defaultPropertyType: null|string- Used when no schema type is present. If set to a string, that string will be used (e.g., "any", "JSON", "external:JSON"). Note that jsdoc recommends*for any, while TypeScript uses "any". If one defines one's own "JSON" type, one could use that to clarify that only JSON types are used. IfdefaultPropertyTypeis set tonull, will avoid any type brackets or type being added. Defaults to*.descriptionPlaceholder: boolean- Iffalseand there is nodescriptionfor the object@property, this will avoid a hyphen or even a space for{description}within@property {name}{description}. Defaults tofalse.hyphenatedDescriptions: boolean- Inserts a hyphen + space in the{description}portion of@property {name}{description}(will add a space, however, unlessdescriptionPlaceholderisfalse). Defaults tofalse.ignore: string[]- Property names to ignore adding to output. Defaults to empty array.indent: number- How many ofindentCharto precede each line. Defaults to0(no indent). Note that a single space will be added in addition to the indent for every line of the document block after the first.indentChar: string- Character to use whenindentis set (e.g., a tab or space). Defaults to a space.maxLength: number|boolean- Enforce a maximum length in@typedefand@propertydescriptions (taking into accountindent/indentChar). Set tofalseto prevent wrapping entirely. Defaults tofalse.objectTagName: string- Tag name to use for objects. Defaults totypedef.propertyNameAsType: boolean- Indicates that the property name (for objects) should be used as the type name (optionally capitalized withcapitalizeProperty). Defaults tofalse.types: null|{[schemaType: string]: string}- Used to determine output of curly-bracketed type content within@typedef {...}. Iftypesisnull, no curly brackets or type content will be shown with the@typedefat all. If the schematypematches a property in the object map, and it maps to the empty string, an empty{}will result. Otherwise, if there is atypematch, that string will be used as the curly bracketed type, or if there is no match, the schema'stypewill be used for the bracketed content. Defaults to an empty object map (will always just use the schema'stype). This property may be used to change the likes of@typedef {object}to@typedef {PlainObject}.