# core-types-json-schema

> core-types ⬌ JSON Schema conversion

Latest version **2.2.0** (published 2023-06-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install core-types-json-schema
pnpm add core-types-json-schema
yarn add core-types-json-schema
bun add core-types-json-schema
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2023-06-04 |
| First published | 2020-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.13.1 \|\| >=16.0.0 |
| Dependencies | 4 |
| Unpacked size | 30.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Gustaf Räntilä |
| Maintainers | grantila |
| Keywords | core-types, type, types, json, schema, json-schema, typescript |

## Links

- npm: https://www.npmjs.com/package/core-types-json-schema
- Repository: https://github.com/grantila/core-types-json-schema
- Homepage: https://github.com/grantila/core-types-json-schema#readme
- Issues: https://github.com/grantila/core-types-json-schema/issues
- npm.io page: https://npm.io/package/core-types-json-schema

## Dependencies (4)

- [jsonpos](https://npm.io/package/jsonpos.md) ^4.1.2
- [core-types](https://npm.io/package/core-types.md) ^3.1.0
- [@types/json-schema](https://npm.io/package/@types/json-schema.md) ^7.0.12
- [openapi-json-schema](https://npm.io/package/openapi-json-schema.md) ^2.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.2.0 (latest) — 2023-06-04
- 2.1.0 — 2023-03-19
- 2.0.0 — 2023-02-02
- 1.7.0 — 2022-06-19
- 1.6.3 — 2022-06-18
- 1.6.2 — 2021-10-20
- 1.6.1 — 2021-10-20
- 1.6.0 — 2021-10-17
- 1.5.1 — 2021-09-11
- 1.5.0 — 2021-04-02
- 1.4.1 — 2021-02-22
- 1.4.0 — 2021-02-22
- 1.3.0 — 2021-02-19
- 1.2.1 — 2021-02-18
- 1.2.0 — 2021-02-18
- … 2 more at https://npm.io/package/core-types-json-schema/versions

## README

[![npm version][npm-image]][npm-url]
[![downloads][downloads-image]][npm-url]
[![build status][build-image]][build-url]
[![coverage status][coverage-image]][coverage-url]
[![Node.JS version][node-version]][node-url]


# core-types-json-schema

This package provides conversion functions between [`core-types`][core-types-github-url] and JSON Schema (and Open API through the [`openapi-json-schema`](https://github.com/grantila/openapi-json-schema) package).

*You probably don't want to use this package directly, but rather [`typeconv`][typeconv-github-url] which uses this package to convert between TypeScript, JSON Schema, GraphQL and Open API.*


# See

Other conversion packages:
 * [`core-types-ts`][core-types-ts-github-url]
 * [`core-types-graphql`][core-types-graphql-github-url]


# Usage

There are four main conversion functions, `convertCoreTypesToJsonSchema`, `convertJsonSchemaToCoreTypes`, `convertCoreTypesToOpenApi` and `convertOpenApiTpCoreTypes`, all returning a wrapped value, of the type [`ConversionResult`](https://github.com/grantila/core-types#conversion).

This package also re-exports `jsonSchemaTypeToOpenApi` and `openApiToJsonSchemaType` from [`openapi-json-schema`](https://github.com/grantila/openapi-json-schema).


## core-types to JSON Schema

```ts
import { convertCoreTypesToJsonSchema } from 'core-types-json-schema'

let doc; // This core-types document comes from somewhere

const { data: jsonSchema } = convertCoreTypesToJsonSchema( doc );
```

You can provide options as a second argument on the type:

```ts
interface ConvertCoreTypesToJsonSchemaOptions
{
    sourceFilename?: string;
    filename?: string;
    userPackage?: string;
    userPackageUrl?: string;
}
```

These fields will be used when constructing a comment (`$comment`) at the root of the JSON Schema, describing the context of where the schema comes from.


## JSON Schema to core-types

```ts
import { convertJsonSchemaToCoreTypes } from 'core-types-json-schema'

let jsonSchema; // This JSON Schema comes from somewhere

const { data: doc } = convertJsonSchemaToCoreTypes( jsonSchema );
```


## core-types to Open API

```ts
import { convertCoreTypesToOpenApi } from 'core-types-json-schema'

let doc; // This core-types document comes from somewhere

const { data: jsonSchema } = convertCoreTypesToOpenApi( doc );
```

You can provide options as a second argument on the type:

```ts
interface CoreTypesToOpenApiOptions extends ConvertCoreTypesToJsonSchemaOptions
{
    title: string;
    version: string;
    schemaVersion?: string;
}
```

The `title` and `version` are required for Open API. The `schemaVersion` defaults to `3.0.0`.


## Open API to core-types

```ts
import { convertOpenApiTpCoreTypes } from 'core-types-json-schema'

let openApiSchema; // This Open API schema comes from somewhere

const { data: doc } = convertOpenApiTpCoreTypes( openApiSchema );
```


[npm-image]: https://img.shields.io/npm/v/core-types-json-schema.svg
[npm-url]: https://npmjs.org/package/core-types-json-schema
[downloads-image]: https://img.shields.io/npm/dm/core-types-json-schema.svg
[build-image]: https://img.shields.io/github/actions/workflow/status/grantila/core-types-json-schema/master.yml?branch=master
[build-url]: https://github.com/grantila/core-types-json-schema/actions?query=workflow%3AMaster
[coverage-image]: https://coveralls.io/repos/github/grantila/core-types-json-schema/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/grantila/core-types-json-schema?branch=master
[node-version]: https://img.shields.io/node/v/core-types-json-schema
[node-url]: https://nodejs.org/en/

[typeconv-github-url]: https://github.com/grantila/typeconv
[core-types-github-url]: https://github.com/grantila/core-types
[core-types-ts-github-url]: https://github.com/grantila/core-types-ts
[core-types-graphql-github-url]: https://github.com/grantila/core-types-graphql

---
_Source: https://npm.io/package/core-types-json-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
