# @hckrnews/openapi-model

> OpenAPI Model

Latest version **0.4.1** (published 2024-02-02) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @hckrnews/openapi-model
pnpm add @hckrnews/openapi-model
yarn add @hckrnews/openapi-model
bun add @hckrnews/openapi-model
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2024-02-02 |
| First published | 2023-11-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18.13 |
| Dependencies | 3 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Pieter Wigboldus |
| Maintainers | epeus, w3news |
| Keywords | openapi, model, entity, schema, specification, api, ajv |

## Links

- npm: https://www.npmjs.com/package/@hckrnews/openapi-model
- Repository: https://github.com/hckrnews/openapi-model
- Homepage: https://github.com/hckrnews/openapi-model#readme
- Issues: https://github.com/hckrnews/openapi-model/issues
- Funding: https://github.com/sponsors/w3nl
- npm.io page: https://npm.io/package/@hckrnews/openapi-model

## Dependencies (3)

- [ajv](https://npm.io/package/ajv.md) ^8.12.0
- [ajv-formats](https://npm.io/package/ajv-formats.md) ^2.1.1
- [@hckrnews/deep-merge](https://npm.io/package/@hckrnews/deep-merge.md) ^0.3.0

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 0.4.1 (latest) — 2024-02-02
- 0.4.0 — 2024-01-12
- 0.3.2 — 2024-01-04
- 0.3.1 — 2023-12-06
- 0.3.0 — 2023-11-29
- 0.2.8 — 2023-11-29
- 0.2.6 — 2023-11-24
- 0.2.5 — 2023-11-22
- 0.2.4 — 2023-11-22
- 0.2.3 — 2023-11-22
- 0.2.2 — 2023-11-21
- 0.2.1 — 2023-11-21
- 0.2.0 — 2023-11-21
- 0.1.2 — 2023-11-21
- 0.1.1 — 2023-11-21
- … 1 more at https://npm.io/package/@hckrnews/openapi-model/versions

## README

# OpenAPI Model

[![NPM version][npm-image]][npm-url] [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=coverage)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) 
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=bugs)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=hckrnews_openapi-model&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-model)

Create easy a Model from a OpenAPI spec.

## Installation

`npm install @hckrnews/openapi-model`
or
`yarn add @hckrnews/openapi-model`

## Test the package

`npm run test`
or
`yarn test`

## How to use

```javascript
const schema = {
    type: 'object',
    properties: {
        foo: {
            type: 'string',
            default: 'bar'
        }
    },
    required: ['foo'],
    additionalProperties: false
}

const options = {
    validate: true,
    strict: false,
    extraAjvFormats: ['date-time']
}

const ExampleModel = openapiToModel(schema, options)

// Create an empty model, with the default values
const example = new ExampleModel()
/*
{
    foo: 'bar'
}
*/

// Create a model with the default values, but overwrite the given properties
const example2 = new ExampleModel({
    foo: 'It works!'
})
/*
{
    foo: 'It works!'
}
*/

// Create a model with the default values, but overwrite the given properties
const example2 = new ExampleModel({
    foo: 3.14
})
/*
    Throws an Error because the type is wrong

    error.message
    "Invalid data"

    error.errors
    [
      {
        instancePath: '/foo',
        keyword: 'type',
        message: 'must be string',
        params: {
          type: 'string'
        },
        schemaPath: '#/properties/foo/type'
      }
    ]

    error.schema
    {
        type: 'object',
        properties: {
            foo: {
                type: 'string',
                default: 'bar'
            }
        },
        required: ['foo'],
        additionalProperties: false
    }

    // The object that is send to the model
    error.data
    {
        foo: 3.14
    }

    // The object that is send to the model, but also with the default values
    error.newData
    {
        foo: 3.14
    }
*/
```


[npm-url]: https://www.npmjs.com/package/@hckrnews/openapi-model
[npm-image]: https://img.shields.io/npm/v/@hckrnews/openapi-model.svg

---
_Source: https://npm.io/package/@hckrnews/openapi-model · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
