--- description: The genever CLI and its flags, the genever(), geneverFile(), parseYaml(), parseYamlDocument(), yamlToJson() and geneverSource() functions, and the errors they throw. --- # API and CLI ## CLI ```sh genever [options] ``` | Option | Default | Description | | ---------------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | `-o`, `--output` | input path with `.ts` | Path of the TypeScript file to write. `.json`, `.yaml` and `.yml` are replaced. | | `--name` | `Schema` | Name of the root schema's type. Not used for an OpenAPI document. | | `--strict` | `true` | Fail on likely mistakes. Disable with `--no-strict`. | | `--parameter-defaults` | `false` | With an OpenAPI document, also write `OperationRequest`, with parameters' defaults filled in. | | `--typed-min-items` | `3` | Largest `minItems` typed as required items. `0` turns it off. | | `--open-api-30` | `false` | Read the schema as an OpenAPI 3.0 schema. An OpenAPI document's own version is used instead. | | `--ci` | `true` in CI | Fail if the output file is not up to date. Implies `--dry-run`. | | `--dry-run` | `false` | Do not write the output file. | | `--signed` | `false` | Sign the output file, so edits to it are detected, and refuse to overwrite it if it was edited or is not signed. | | `--force` | `false` | With `--signed`, overwrite the output file anyway. | | `--manifest` | | Record the output, and hashes of its inputs, in this manifest. With `--ci`, an output that still matches it passes without fetching URLs. | | `--fetch` | `true` | Load URLs the schema references. `--no-fetch` loads only files. | | `--fetch-host` | | Load 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. | Parameter | Type | Default | Description | | --------------------------- | --------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | | `schema` | `unknown` | — | Required. Parsed JSON Schema, or OpenAPI document. | | `options.name` | `string` | `'Schema'` | Name of the root schema's type. Not used for an OpenAPI document. | | `options.strict` | `boolean` | `true` | Fail on likely mistakes. See [Strict mode](/packages/genever/guides/strict-mode). | | `options.baseUri` | `string` | a placeholder | URI the schema was read from, which relative `$id`s and `$ref`s resolve against. | | `options.parameterDefaults` | `boolean` | `false` | With an OpenAPI document, also generate `OperationRequest`: what a handler gets once a validator that fills in defaults has run. | | `options.typedMinItems` | `number` | `3` | Largest `minItems` typed as required items, e.g. `[T, ...T[]]`. `0` turns it off. The default matches Juniper. Must be a non-negative integer. | | `options.openApi30` | `boolean` | `false` | Read 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](#errors). Throws a `TypeError` if `typedMinItems` is not a non-negative integer. ```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. | Parameter | Type | Default | Description | | ----------------------------------------------------------------------------------------------------------- | --------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `input` | `string` | — | Required. Path to the JSON Schema file, or OpenAPI document. | | `options.output` | `string` | input path with `.ts` instead of `.json`, `.yaml` or `.yml` | Path of the TypeScript file to write. | | `options.cwd` | `string \| URL` | `process.cwd()` | Directory relative paths resolve from. | | `options.check` | `boolean` | `false` | Fail, rather than write, if the output file is not up to date. | | `options.dryRun` | `boolean` | `false` | Do not write the output file. | | `options.signed` | `boolean` | `false` | Sign the output file with [`generated-signature`](/packages/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`](/packages/populate-files/reference/api#errors)). | | `options.force` | `boolean` | `false` | With `signed`, overwrite the output file anyway. Use it once to start signing an existing file. | | `options.manifest` | `string` | — | 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](/packages/genever/guides/references#checking-without-fetching). | | `options.fetch` | `boolean \| string[]` | `true` | Load `http:` and `https:` URLs the schema references, or only those of the hosts listed. Otherwise only files are loaded. See [References to other files](/packages/genever/guides/references). | | `options.name`, `options.strict`, `options.openApi30`, `options.parameterDefaults`, `options.typedMinItems` | | | Same as [`genever`](#genever-schema-options). | **Returns** `Promise<{ output: string; updated: boolean }>` — absolute path of the output file, and whether its content changed. **Throws** `GeneverError` — see [Errors](#errors). With `check`, also throws if the file is out of date. Throws a `TypeError` if `fetch` lists something that is not a host, or `typedMinItems` is not a non-negative integer. --- ## `parseYaml(text)` Parse a YAML (or JSON) document the way genever reads a `.yaml` file, for `genever` to generate types from. Returns the parsed document, with only the values JSON has. What is read, and what is an error, is described [below](#yaml). ```ts import { genever, parseYaml } from 'genever'; genever(parseYaml('type: array\nitems: { type: string }\n'), { name: 'Tags' }); // export type Tags = string[]; ``` ## `parseYamlDocument(text)` and `yamlToJson(document)` The two steps of `parseYaml`, for a tool that also needs to know where things are in the text, without parsing twice. `parseYamlDocument` returns the [`yaml`](https://www.npmjs.com/package/yaml) package's `Document`, whose nodes each have a `range`, and throws the `YAMLError`s `parseYaml` would. `yamlToJson` returns the value `parseYaml` would, and throws its `GeneverError`s. ```ts import { geneverSource, parseYamlDocument, yamlToJson } from 'genever'; const document = parseYamlDocument('type: array\nitems: { type: string }\n'); const { names } = geneverSource(yamlToJson(document), { name: 'Tags' }); ``` ## `geneverSource(schema, options?)` Generate types as `genever` does, and say where each type's name is in the source and which part of the document it comes from, so a tool showing the types can link each name back to its schema. Takes the same options as `genever`. Returns `{ text, names }`. `text` is the same source `genever` returns. `names` lists each type generated from a schema, or a document's `paths`, and each property generated from a schema's `properties` (or a `patternProperties` pattern matching one key), in the order they are in `text`, as `{ name, pointer, start }`: `start` is the index of the name in `text`, and `pointer` the JSON Pointer to its schema (`#/$defs/Note`, `#/components/schemas/Note/properties/body`) or `#/paths` for `Paths`, in the same form as an [error](#errors)'s. A property's `name` is as printed, so quoted if it isn't an identifier (`"created at"`). A property declared by both a schema and its `allOf` members points to the schema's own `properties`, or else the first member that declares it. An OpenAPI document's helper types (`Operations`, `OperationInput` and the rest), and their own properties, aren't listed. ```ts import { geneverSource } from 'genever'; const { text, names } = geneverSource({ type: 'object', properties: { body: { type: 'string' } } }); // names: [{ name: 'Schema', pointer: '#', start: … }, { name: 'body', pointer: '#/properties/body', start: … }] ``` ## 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](/packages/genever/guides/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, an alias with no anchor before it, a merge key that merges a map into itself (`<<: *a` inside `&a`) or anything but maps (`<<: 1`) and more than one document throw the [`yaml`](https://www.npmjs.com/package/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`. In another OpenAPI document, or file of OpenAPI objects, it starts with that file's URI, e.g. `file:///api/common.yaml#/components/schemas/Error/type`. The pointer is also at the end of the message. A pointer is in its URI fragment form ([RFC 6901 §6](https://www.rfc-editor.org/rfc/rfc6901#section-6)), so it is also a `$ref` to the same place: in each key, `~` is `~0` and `/` is `~1`, then each UTF-8 byte of a character a URI fragment can't hold ([RFC 3986 §3.5](https://www.rfc-editor.org/rfc/rfc3986#section-3.5)) is percent-encoded. Letters, digits and `-._~!$&'()*+,;=:@/?` are kept as they are, so the property `a/b~c` is `#/properties/a~1b~0c`, `""` is `#/properties/`, and `50% off` is `#/properties/50%25%20off`. The `pointer` of each of [`geneverSource`](#geneversource-schema-options)'s `names` has the same form. When a schema has several mistakes, the one reported is the first genever finds, and it is the same in every implementation. A schema's keywords are checked in a fixed order, and the members of every object of them (`properties`, `$defs`, `patternProperties`, `dependentSchemas`, an OpenAPI document's `paths`, `components`, `responses`, `content` and the rest) in the order of their keys by UTF-16 code unit, as [RFC 8785 §3.2.3](https://www.rfc-editor.org/rfc/rfc8785#section-3.2.3) sorts them: not the order they are written in, or the order a JavaScript object keeps them in. So of the invalid properties `b`, `9` and `10`, `10` is reported. Generated types list properties, paths and operations in the order a JavaScript object keeps them: keys that are array indices first, ascending, then the rest as written. | Error | Thrown when | | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `GeneverInvalidSchemaError` | The input is not a valid JSON Schema, e.g. `properties` is not an object, or a `$ref` does not resolve. | | `GeneverStrictModeError` | A [strict mode](/packages/genever/guides/strict-mode) rule is broken. | | `GeneverUnsupportedError` | The schema uses a feature genever can't generate types for. See [Not supported](/packages/genever/reference/types#not-supported). | | `GeneverLoadError` | A file or URL the schema references could not be loaded. Its `pointer` is to the `$ref` that needed it, in the file or URL its `referencedFrom` names: the input, or a file the input loaded. Its `cause` is the underlying error, such as the file system's or the parser's, if there was one. |