[![npm version](https://badge.fury.io/js/@buildwithlayer%2Fopenapi-zod-spec.svg)](https://badge.fury.io/js/@buildwithlayer%2Fopenapi-zod-spec)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

# OpenAPI Zod Spec

> This is a simple Zod Schema for Open API specs version 3.X.X.

## Prerequisites

This project requires NodeJS (version 18 or later) and NPM.
[Node](http://nodejs.org/) and [NPM](https://npmjs.org/) are really easy to install.
To make sure you have them available on your machine,
try running the following command.

```sh
$ npm -v && node -v
10.2.4
v20.11.0
```

## Table of contents

- [OpenAPI Zod Spec](#openapi-zod-spec)
    - [Prerequisites](#prerequisites)
    - [Table of contents](#table-of-contents)
    - [Installation](#installation)
    - [Usage](#usage)
        - [Loading an OpenAPI Spec](#loading-an-openapi-spec)
        - [Upgrading an OpenAPI Spec](#upgrading-an-openapi-spec)
    - [Contributing](#contributing)
    - [Versioning](#versioning)
    - [License](#license)

## Installation

**BEFORE YOU INSTALL:** please read the [prerequisites](#prerequisites)

To install and set up the library, run:

```sh
$ npm install @buildwithlayer/openapi-zod-spec
```

Or if you prefer using Yarn:

```sh
$ yarn add @buildwithlayer/openapi-zod-spec
```

## Usage

### Loading an OpenAPI Spec

From JSON:

```ts
import {OpenAPISpec} from '@buildwithlayer/openapi-zod-spec/index.js';
import {rawSpec} from './openapi.json';

const spec = OpenAPISpec.parse(rawSpec);
```

From YAML:

```ts
import {OpenAPISpec} from '@buildwithlayer/openapi-zod-spec/index.js';
import fs from 'fs';
import yaml from 'js-yaml';

const pathToYamlFile = './path/to/openapi.yml';
const fileContents = fs.readFileSync(pathToYamlFile, 'utf-8');
const rawSpec = yaml.load(fileContents);
const spec = OpenAPISpec.parse(rawSpec);
```

### Upgrading an OpenAPI Spec

```ts
import {OpenAPISpec, upgrade} from '@buildwithlayer/openapi-zod-spec/index.js';
import {oldSpec} from './openapi.json';

const upgradedSpec = upgrade(OpenAPISpec.parse(oldSpec));
```

## Contributing

> These instructions will be updated *soon*.

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests to us.

1.  Fork it!
2.  Create your feature branch: `git checkout -b my-new-feature`
3.  Add your changes: `git add .`
4.  Commit your changes: `git commit -am 'Add some feature'`
5.  Push to the branch: `git push origin my-new-feature`
6.  Submit a pull request :sunglasses:


## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags).

## License

[MIT License](https://andreasonny.mit-license.org/2019) © Andrea SonnY