@trojs/openapi-model 0.5.4 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +26 -15
- package/src/model.js +61 -61
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trojs/openapi-model",
|
|
3
3
|
"description": "OpenAPI Model",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pieter Wigboldus",
|
|
7
7
|
"url": "https://trojs.org/"
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"scripts": {
|
|
11
|
-
"lint": "eslint
|
|
12
|
-
"lint:report": "eslint src/*.js
|
|
13
|
-
"lint:fix": "eslint
|
|
11
|
+
"lint": "eslint",
|
|
12
|
+
"lint:report": "eslint src/*.js -f json -o report.json",
|
|
13
|
+
"lint:fix": "eslint --fix",
|
|
14
14
|
"lint:rules": "eslint --print-config file.js > eslintconfig.json",
|
|
15
15
|
"test": "node --test src/*.test.js",
|
|
16
16
|
"test:watch": "node --watch --test src/*.test.js",
|
|
@@ -26,14 +26,18 @@
|
|
|
26
26
|
"main": "src/model.js",
|
|
27
27
|
"types": "src/schema.d.ts",
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@
|
|
30
|
-
"eslint": "^
|
|
31
|
-
"eslint-
|
|
32
|
-
"eslint
|
|
33
|
-
"eslint-
|
|
34
|
-
"eslint-plugin-
|
|
35
|
-
"eslint-plugin-
|
|
36
|
-
"eslint-plugin-
|
|
29
|
+
"@eslint/js": "^9.15.0",
|
|
30
|
+
"@stylistic/eslint-plugin": "^2.11.0",
|
|
31
|
+
"@stylistic/eslint-plugin-js": "^2.11.0",
|
|
32
|
+
"eslint": "^9.15.0",
|
|
33
|
+
"eslint-config-prettier": "^9.1.0",
|
|
34
|
+
"eslint-plugin-import": "^2.31.0",
|
|
35
|
+
"eslint-plugin-jsdoc": "^50.5.0",
|
|
36
|
+
"eslint-plugin-n": "^17.14.0",
|
|
37
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
38
|
+
"eslint-plugin-promise": "^7.1.0",
|
|
39
|
+
"eslint-plugin-sonarjs": "^2.0.4",
|
|
40
|
+
"globals": "^15.12.0",
|
|
37
41
|
"jscpd": "^4.0.0"
|
|
38
42
|
},
|
|
39
43
|
"repository": {
|
|
@@ -41,7 +45,7 @@
|
|
|
41
45
|
"url": "https://github.com/hckrnews/openapi-model"
|
|
42
46
|
},
|
|
43
47
|
"engines": {
|
|
44
|
-
"node": ">=
|
|
48
|
+
"node": ">= 20"
|
|
45
49
|
},
|
|
46
50
|
"keywords": [
|
|
47
51
|
"openapi",
|
|
@@ -53,7 +57,7 @@
|
|
|
53
57
|
"ajv"
|
|
54
58
|
],
|
|
55
59
|
"dependencies": {
|
|
56
|
-
"@trojs/deep-merge": "^0.
|
|
60
|
+
"@trojs/deep-merge": "^1.0.0",
|
|
57
61
|
"ajv": "^8.12.0",
|
|
58
62
|
"ajv-formats": "^3.0.0"
|
|
59
63
|
},
|
|
@@ -63,6 +67,13 @@
|
|
|
63
67
|
},
|
|
64
68
|
"overrides": {
|
|
65
69
|
"semver": "^7.5.3",
|
|
66
|
-
"xml2js": "^0.6.0"
|
|
70
|
+
"xml2js": "^0.6.0",
|
|
71
|
+
"eslint-plugin-sonarjs": {
|
|
72
|
+
"eslint": "^9.15.0",
|
|
73
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
74
|
+
"@typescript-eslint/utils": "^8.0.0",
|
|
75
|
+
"eslint-plugin-import": "^2.31.0",
|
|
76
|
+
"eslint-plugin-react-hooks": "^5.0.0"
|
|
77
|
+
}
|
|
67
78
|
}
|
|
68
79
|
}
|
package/src/model.js
CHANGED
|
@@ -15,14 +15,14 @@ const defaultExtraAjvFormats = ['date', 'time', 'uri', 'uuid', 'email', 'hostnam
|
|
|
15
15
|
* @returns {[key: string, defaultValue: any]}
|
|
16
16
|
*/
|
|
17
17
|
const createPropertyWithDefaultValue = ([key, schema]) => {
|
|
18
|
-
|
|
18
|
+
const { type, default: defaultValue } = schema
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
if (type === 'object') {
|
|
21
|
+
const subParser = createBaseObjectFromSchema(schema)
|
|
22
|
+
return [key, subParser]
|
|
23
|
+
}
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
return [key, defaultValue]
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -30,75 +30,75 @@ const createPropertyWithDefaultValue = ([key, schema]) => {
|
|
|
30
30
|
* @returns {object}
|
|
31
31
|
*/
|
|
32
32
|
const createBaseObjectFromSchema = (schema) => Object.fromEntries(
|
|
33
|
-
|
|
33
|
+
Object.entries(schema.properties).map(createPropertyWithDefaultValue)
|
|
34
34
|
)
|
|
35
35
|
|
|
36
36
|
const openapiToModel = (schema, options = {}) => {
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
const { validate = true } = options
|
|
38
|
+
const emptyObject = createBaseObjectFromSchema(schema)
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
const ajv = new Ajv({
|
|
41
|
+
strict: options.strict || false
|
|
42
|
+
})
|
|
43
|
+
const extraAjvFormats = options.extraAjvFormats || []
|
|
44
|
+
addFormats(ajv, [...defaultExtraAjvFormats, ...extraAjvFormats])
|
|
45
|
+
const compile = ajv.compile(schema)
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
class Model {
|
|
48
48
|
/**
|
|
49
49
|
* @param {object=} data
|
|
50
50
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
constructor (data) {
|
|
52
|
+
this.errors = undefined
|
|
53
|
+
this.data = data || {}
|
|
54
|
+
this.schema = schema
|
|
55
|
+
this.emptyObject = emptyObject
|
|
56
|
+
}
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Set the data for the model, including defaults, and validate it against the schema
|
|
60
|
+
* @param {object=} data
|
|
61
|
+
*/
|
|
62
|
+
set data (data) {
|
|
63
|
+
const newData = deepMerge(emptyObject, data)
|
|
64
|
+
const valid = compile(newData)
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
if (validate && !valid) {
|
|
67
|
+
const error = new Error('Invalid data')
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
error.errors = compile.errors
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
error.schema = schema
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
error.data = data
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
error.newData = newData
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
error.emptyObject = emptyObject
|
|
78
|
+
throw error
|
|
79
|
+
}
|
|
80
|
+
this.value = newData
|
|
81
|
+
}
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
/**
|
|
84
|
+
* @returns {object}
|
|
85
|
+
*/
|
|
86
|
+
get data () {
|
|
87
|
+
return this.value
|
|
88
|
+
}
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
/**
|
|
91
|
+
* @returns {object}
|
|
92
|
+
*/
|
|
93
|
+
valueOf () {
|
|
94
|
+
return this.value
|
|
95
|
+
}
|
|
95
96
|
}
|
|
96
|
-
|
|
97
|
-
return Model
|
|
97
|
+
return Model
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
export {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
openapiToModel,
|
|
102
|
+
createBaseObjectFromSchema,
|
|
103
|
+
createPropertyWithDefaultValue
|
|
104
104
|
}
|