Giter VIP home page Giter VIP logo

json-schema-to-jsdoc's Introduction

npm version Build Status

Known Vulnerabilities Total Alerts Code Quality: Javascript

JSON Schema to JSDoc

Useful when you already have a JSON Schema and want to document the types you want to validate. Works with subschema definitions.

Usage

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 */)

Output

/**
 * @typedef {object} Person
 * @property {string} name A person's name
 * @property {integer} [age] A person's age
 */

Examples

hyphenatedDescriptions

jsdoc(schema, {
    hyphenatedDescriptions: true
})
/**
 * @typedef {object} Person
 * @property {string} name - A person's name
 * @property {integer} [age] - A person's age
 */

autoDescribe

jsdoc(schema, {
    autoDescribe: true
})
/**
 * Represents a Person object
 * @typedef {object} Person
 * @property {string} name A person's name
 * @property {integer} [age] A person's age
 */

types

jsdoc(schema, {
    types: {
      object: 'PlainObject'
    }
})
/**
 * @typedef {PlainObject} Person
 * @property {string} name A person's name
 * @property {integer} [age] A person's age
 */

formats

const schema = {
  title: 'Info',
  type: 'object',
  properties: {
    code: {
      type: 'string', format: 'html', description: 'The HTML source'
    }
  },
  required: ['code']
}

jsdoc(schema, {
    formats: {
      html: {
        string: 'HTML'
      }
    }
})
/**
 * @typedef {object} Info
 * @property {HTML} code The HTML source
 */

Options

addDescriptionLineBreak: boolean
Inserts an empty line when autoDescribe is false and the schema description is empty. Defaults to false.

autoDescribe: boolean
Adds a description ("Represents a/n [<title> ]<type>") when the schema has no description. Defaults to false.

capitalizeProperty: boolean
When propertyNameAsType is true, capitalizes the property-as-type, i.e., MyTitle in @property {MyTitle} myTitle. Defaults to false.

capitalizeTitle: boolean
If a schema title is present, capitalizes the schema's title in the output of @typedef {myType} title. Defaults to false.

defaultPropertyType: null | string
Used when no schema type is present. Defaults to "*".

  • string: 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.
  • null: Will avoid any type brackets or type being added.

descriptionPlaceholder: boolean
If false and there is no description for the object @property, this will avoid a hyphen or even a space for {description} within @property {name}{description}. Defaults to false.

hyphenatedDescriptions: boolean
Inserts a hyphen + space in the {description} portion of @property {name}{description} (will add a space, however, unless descriptionPlaceholder is false). Defaults to false.

ignore: string[]
Property names to ignore adding to output. Defaults to empty array.

indent: number
How many of indentChar to precede each line. Defaults to 0 (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 when indent is set (e.g., a tab or space). Defaults to a space.

maxLength: number | boolean

  • number: Enforce a maximum length in @typedef and @property descriptions (taking into account indent/indentChar).
  • false: Prevent wrapping entirely. Defaults to false.

objectTagName: string
Tag name to use for objects. Defaults to typedef.

propertyNameAsType: boolean
Indicates that the property name (for objects) should be used as the type name (optionally capitalized with capitalizeProperty). Defaults to false.

types: null | {[schemaType: string]: string}
Used to determine output of curly-bracketed type content within @typedef {...}. If null no curly brackets or type content will be shown with the @typedef at all. If the schema type matches a property in the object map, and it maps to the empty string, an empty {} will result. Otherwise, if there is a type match, that string will be used as the curly bracketed type, or if there is no match, the schema's type will be used for the bracketed content. Defaults to an empty object map (will always just use the schema's type). This property may be used to change the likes of @typedef {object} to @typedef {PlainObject}.

json-schema-to-jsdoc's People

Contributors

brettz9 avatar dependabot[bot] avatar n3ps avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.