0.4.1 • Published 3 months ago

@hckrnews/openapi-model v0.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

OpenAPI Model

NPM version Coverage Quality Gate Status Bugs Code Smells Technical Debt Vulnerabilities Maintainability Rating Reliability Rating Security Rating

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

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
    }
*/
0.4.1

3 months ago

0.4.0

4 months ago

0.3.2

4 months ago

0.3.1

5 months ago

0.3.0

6 months ago

0.2.8

6 months ago

0.2.6

6 months ago

0.2.5

6 months ago

0.2.4

6 months ago

0.2.3

6 months ago

0.2.2

6 months ago

0.2.1

6 months ago

0.2.0

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago