Skip to content

API and CLI ​

CLI ​

sh
genever <input> [options]
OptionDefaultDescription
-o, --outputinput path with .tsPath of the TypeScript file to write. .json, .yaml and .yml are replaced.
--nameSchemaName of the root schema's type. Not used for an OpenAPI document.
--stricttrueFail on likely mistakes. Disable with --no-strict.
--parameter-defaultsfalseWith an OpenAPI document, also write OperationRequest, with parameters' defaults filled in.
--typed-min-items3Largest minItems typed as required items. 0 turns it off.
--open-api-30falseRead the schema as an OpenAPI 3.0 schema. An OpenAPI document's own version is used instead.
--citrue in CIFail if the output file is not up to date. Implies --dry-run.
--dry-runfalseDo not write the output file.
--signedfalseSign the output file, so edits to it are detected, and refuse to overwrite it if it was edited or is not signed.
--forcefalseWith --signed, overwrite the output file anyway.
--manifestRecord the output, and hashes of its inputs, in this manifest. With --ci, an output that still matches it passes without fetching URLs.
--fetchtrueLoad URLs the schema references. --no-fetch loads only files.
--fetch-hostLoad only URLs of this host, e.g. example.com, localhost:8080, or *.example.com for its subdomains. Repeat for more hosts.
--cwd.Directory relative paths resolve from.

The path of a written file is logged. Nothing is logged when it was already up to date.

genever(schema, options?) ​

Generate TypeScript types from a JSON Schema.

ParameterTypeDefaultDescription
schemaunknown—Required. Parsed JSON Schema, or OpenAPI document.
options.namestring'Schema'Name of the root schema's type. Not used for an OpenAPI document.
options.strictbooleantrueFail on likely mistakes. See Strict mode.
options.baseUristringa placeholderURI the schema was read from, which relative $ids and $refs resolve against.
options.parameterDefaultsbooleanfalseWith an OpenAPI document, also generate OperationRequest<Id>: what a handler gets once a validator that fills in defaults has run.
options.typedMinItemsnumber3Largest minItems typed as required items, e.g. [T, ...T[]]. 0 turns it off. The default matches Juniper.
options.openApi30booleanfalseRead the schema as an OpenAPI 3.0 schema, which allows nullable and example. An OpenAPI document's own version is used instead.

Returns string — TypeScript source, not formatted.

Throws GeneverError — see Errors.

ts
import { 
genever
} from 'genever';
genever
({
type
: 'array',
items
: {
type
: 'string' } }, {
name
: 'Tags' });
// export type Tags = string[];

geneverFile(input, options?) ​

Read a JSON Schema file, or OpenAPI document, in JSON or YAML, and write its types to a TypeScript file.

ParameterTypeDefaultDescription
inputstring—Required. Path to the JSON Schema file, or OpenAPI document.
options.outputstringinput path with .ts instead of .json, .yaml or .ymlPath of the TypeScript file to write.
options.cwdstring | URLprocess.cwd()Directory relative paths resolve from.
options.checkbooleanfalseFail, rather than write, if the output file is not up to date.
options.dryRunbooleanfalseDo not write the output file.
options.signedbooleanfalseSign the output file with generated-signature, so edits to it are detected. An output file that was edited, or is not signed yet, is not overwritten (a PopulateFilesOwnershipError from populate-files).
options.forcebooleanfalseWith signed, overwrite the output file anyway. Use it once to start signing an existing file.
options.manifeststring—Path of a manifest (relative to cwd) to record the output and hashes of its inputs in. With check, an output matching its entry is up to date without loading anything. See References.
options.fetchboolean | string[]trueLoad http: and https: URLs the schema references, or only those of the hosts listed. Otherwise only files are loaded. See References to other files.
options.name, options.strict, options.openApi30, options.parameterDefaults, options.typedMinItemsSame as genever.

Returns Promise<{ output: string; updated: boolean }> — absolute path of the output file, and whether its content changed.

Throws GeneverError — see Errors. With check, also throws if the file is out of date. Throws a TypeError if fetch lists something that is not a host.


YAML ​

A .yaml or .yml file is read as YAML 1.2, whether it is the input or a file it references, and so is a URL served with a YAML media type. YAML 1.2 reads on, off, yes and no as strings, not booleans.

Anchors and aliases are expanded, and merge keys (<<: *base) are merged. An alias inside the schema it names, a recursive schema, becomes a $ref to that schema:

yaml
# node.yaml: `genever node.yaml --name Node` writes `type Node = { children?: Node[] }`
&node
type: object
properties:
  children: { type: array, items: *node }

The $ref follows the usual rules: one that applies to the same value again, such as &a { anyOf: [*a, …] }, breaks strict mode. A recursive schema with another $id between the alias and its anchor throws a GeneverUnsupportedError.

Anything else JSON can't hold is an error. Tags beyond YAML's core schema (such as !!binary), keys that aren't strings, duplicate keys and more than one document throw the yaml package's YAMLError, as invalid JSON throws a SyntaxError. .inf, .nan, and an alias inside a value that isn't a schema (such as a const) throw a GeneverInvalidSchemaError. In a referenced file, any of these is a GeneverLoadError.

Errors ​

Every error extends GeneverError, with a pointer property: the JSON Pointer to the schema or keyword at fault, e.g. #/properties/a/type. The pointer is also at the end of the message.

ErrorThrown when
GeneverInvalidSchemaErrorThe input is not a valid JSON Schema, e.g. properties is not an object, or a $ref does not resolve.
GeneverStrictModeErrorA strict mode rule is broken.
GeneverUnsupportedErrorThe schema uses a feature genever can't generate types for. See Not supported.
GeneverLoadErrorA file or URL the schema references could not be loaded. Its cause is the underlying error, such as the file system's or the parser's, if there was one.