Skip to content

Commit

Permalink
Merge pull request #12 from hckrnews/feature/optimize
Browse files Browse the repository at this point in the history
Finetune data merge
  • Loading branch information
w3nl authored Nov 29, 2023
2 parents 7278b48 + fab6003 commit 4f5896e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.9.0
20.10.0
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/openapi-model",
"description": "OpenAPI Model",
"version": "0.2.8",
"version": "0.3.0",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down Expand Up @@ -53,6 +53,7 @@
"ajv"
],
"dependencies": {
"@hckrnews/deep-merge": "^0.1.2",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1"
},
Expand All @@ -64,4 +65,4 @@
"semver": "^7.5.3",
"xml2js": "^0.5.0"
}
}
}
8 changes: 3 additions & 5 deletions src/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ajv from 'ajv'
import addFormats from 'ajv-formats'
import { deepMerge } from '@hckrnews/deep-merge'

const defaultExtraAjvFormats = ['date', 'time', 'uri', 'uuid', 'email', 'hostname', 'regex']
/**
Expand Down Expand Up @@ -49,7 +50,7 @@ const openapiToModel = (schema, options = {}) => {
*/
constructor (data) {
this.errors = undefined
this.data = data
this.data = data || {}
this.schema = schema
this.emptyObject = emptyObject
}
Expand All @@ -59,10 +60,7 @@ const openapiToModel = (schema, options = {}) => {
* @param {object=} data
*/
set data (data) {
const newData = {
...emptyObject,
...data
}
const newData = deepMerge(emptyObject, data)
const valid = compile(newData)

if (validate && !valid) {
Expand Down
13 changes: 7 additions & 6 deletions src/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test('Test the openapiToModel helpers', async (t) => {
// eslint-disable-next-line no-new
new ExampleModel({
baz: {
qux: '42'
qux: 42
}
})
} catch (error) {
Expand All @@ -103,13 +103,13 @@ test('Test the openapiToModel helpers', async (t) => {
error.errors,
[
{
instancePath: '/baz',
keyword: 'required',
message: "must have required property 'quux'",
instancePath: '/baz/qux',
keyword: 'type',
message: 'must be string',
params: {
missingProperty: 'quux'
type: 'string'
},
schemaPath: '#/properties/baz/required'
schemaPath: '#/properties/baz/properties/qux/type'
}
]
)
Expand All @@ -130,6 +130,7 @@ test('Test the openapiToModel helpers', async (t) => {
{
bar: 42,
baz: {
quux: 3.14,
qux: '42'
},
foo: 'bar'
Expand Down

0 comments on commit 4f5896e

Please sign in to comment.