Create easy a Model from a OpenAPI spec.
npm install @trojs/openapi-model
or
yarn add @trojs/openapi-model
npm run test
or
yarn test
constschema={type: 'object',properties: {foo: {type: 'string',default: 'bar'}},required: ['foo'],additionalProperties: false}constoptions={validate: true,strict: false,extraAjvFormats: ['date-time']}constExampleModel=openapiToModel(schema,options)// Create an empty model, with the default valuesconstexample=newExampleModel()/*{ foo: 'bar'}*/// Create a model with the default values, but overwrite the given propertiesconstexample2=newExampleModel({foo: 'It works!'})/*{ foo: 'It works!'}*/// Create a model with the default values, but overwrite the given propertiesconstexample2=newExampleModel({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 }*/