schema-markdown 1.2.3 → 1.2.5
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/README.md +15 -15
- package/lib/encode.js +37 -0
- package/lib/parser.js +1 -1
- package/lib/schema.js +2 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/schema-markdown)
|
|
4
4
|
[](https://github.com/craigahobbs/schema-markdown-js/blob/main/LICENSE)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
schema-markdown is a schema definition and validation library.
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
## Links
|
|
@@ -21,8 +21,8 @@ which is parsed by the
|
|
|
21
21
|
[parseSchemaMarkdown](https://craigahobbs.github.io/schema-markdown-js/module-lib_parser.html#.parseSchemaMarkdown)
|
|
22
22
|
function. For example:
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
import {parseSchemaMarkdown} from 'schema-markdown/parser.js';
|
|
24
|
+
~~~ javascript
|
|
25
|
+
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
26
26
|
|
|
27
27
|
export const modelTypes = parseSchemaMarkdown(`\
|
|
28
28
|
# An aggregate numerical operation
|
|
@@ -39,7 +39,7 @@ enum AggregationFunction
|
|
|
39
39
|
Average
|
|
40
40
|
Sum
|
|
41
41
|
`);
|
|
42
|
-
|
|
42
|
+
~~~
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
## Validate using a Schema
|
|
@@ -48,34 +48,34 @@ To validate an object using the schema, use the
|
|
|
48
48
|
[validateType](https://craigahobbs.github.io/schema-markdown-js/module-lib_schema.html#.validateType)
|
|
49
49
|
function. For example:
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
import {validateType} from 'schema-markdown/schema.js';
|
|
51
|
+
~~~ javascript
|
|
52
|
+
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
53
53
|
|
|
54
54
|
const obj = validateType(modelTypes, 'Aggregation', {'numbers': [1, 2, '3', 4]});
|
|
55
55
|
console.assert(obj.numbers[2] === 3);
|
|
56
|
-
|
|
56
|
+
~~~
|
|
57
57
|
|
|
58
58
|
Notice that the numerical input '3' above is *type-massaged* to the integer 3 by validation.
|
|
59
59
|
|
|
60
60
|
Validation fails if the object does not match the schema:
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
~~~ javascript
|
|
63
63
|
try {
|
|
64
64
|
validateType(modelTypes, 'Aggregation', {'numbers': [1, 2, 'asdf', 4]});
|
|
65
65
|
} catch ({message}) {
|
|
66
66
|
console.assert(message === "Invalid value \"asdf\" (type 'string') for member 'numbers.2', expected type 'int'", message);
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
~~~
|
|
69
69
|
|
|
70
70
|
Validation also fails if a member constraint is violated:
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
~~~ javascript
|
|
73
73
|
try {
|
|
74
74
|
validateType(modelTypes, 'Aggregation', {'numbers': []});
|
|
75
75
|
} catch ({message}) {
|
|
76
76
|
console.assert(message === "Invalid value [] (type 'object') for member 'numbers', expected type 'array' [len > 0]", message);
|
|
77
77
|
}
|
|
78
|
-
|
|
78
|
+
~~~
|
|
79
79
|
|
|
80
80
|
|
|
81
81
|
## Document a Schema
|
|
@@ -93,9 +93,9 @@ node --input-type=module \
|
|
|
93
93
|
|
|
94
94
|
To host locally, start a local static web server:
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
~~~
|
|
97
97
|
python3 -m http.server
|
|
98
|
-
|
|
98
|
+
~~~
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
## Development
|
|
@@ -103,6 +103,6 @@ python3 -m http.server
|
|
|
103
103
|
This package is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme).
|
|
104
104
|
It was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme) as follows:
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
~~~
|
|
107
107
|
template-specialize javascript-template/template/ schema-markdown-js/ -k package schema-markdown -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs' -k noapp 1
|
|
108
|
-
|
|
108
|
+
~~~
|
package/lib/encode.js
CHANGED
|
@@ -128,3 +128,40 @@ export function decodeQueryString(paramStr) {
|
|
|
128
128
|
|
|
129
129
|
return result[0] !== null ? result[0] : {};
|
|
130
130
|
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* JSON-stringify an object with sorted keys
|
|
135
|
+
*
|
|
136
|
+
* @param {Object} obj - The object to stringify
|
|
137
|
+
* @returns {string}
|
|
138
|
+
*/
|
|
139
|
+
export function jsonStringifySortKeys(value, space) {
|
|
140
|
+
// JSON-stringify non-objects without creating a key set
|
|
141
|
+
if (value === null || typeof value !== 'object') {
|
|
142
|
+
return JSON.stringify(value, null, space);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// JSON-stringify with sorted keys
|
|
146
|
+
const keySet = new Set();
|
|
147
|
+
getObjectKeys(value, keySet);
|
|
148
|
+
const sortedKeys = Array.from(keySet.values()).sort();
|
|
149
|
+
return JSON.stringify(value, sortedKeys, space);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
// Helper function to get an object keys (deep)
|
|
154
|
+
function getObjectKeys(value, keySet) {
|
|
155
|
+
if (value !== null && typeof value === 'object') {
|
|
156
|
+
if (Array.isArray(value)) {
|
|
157
|
+
for (const subValue of value) {
|
|
158
|
+
getObjectKeys(subValue, keySet);
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
for (const [subKey, subValue] of Object.entries(value)) {
|
|
162
|
+
keySet.add(subKey);
|
|
163
|
+
getObjectKeys(subValue, keySet);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
package/lib/parser.js
CHANGED
|
@@ -46,7 +46,7 @@ const rTypedef = new RegExp(`^typedef\\s+${rPartTypedef}\\s*$`);
|
|
|
46
46
|
const rMember = new RegExp(`^\\s+(?<optional>optional\\s+)?${rPartTypedef}\\s*$`);
|
|
47
47
|
const rValue = new RegExp(`^\\s+(?<id>${rPartId})\\s*$`);
|
|
48
48
|
const rValueQuoted = /^\s+"(?<id>.*?)"\s*$/;
|
|
49
|
-
const rURL = /^\s+(?<method>[A-Za-z]+|\*)(?:\s+(?<path
|
|
49
|
+
const rURL = /^\s+(?<method>[A-Za-z]+|\*)(?:\s+(?<path>\/\S*))?/;
|
|
50
50
|
const rLineSplit = /\r?\n/;
|
|
51
51
|
|
|
52
52
|
|
package/lib/schema.js
CHANGED
|
@@ -168,9 +168,10 @@ function validateTypeHelper(types, type, value, memberFqn) {
|
|
|
168
168
|
// Not a number?
|
|
169
169
|
} else if (typeof value !== 'number') {
|
|
170
170
|
throwMemberError(type, value, memberFqn);
|
|
171
|
+
}
|
|
171
172
|
|
|
172
173
|
// Non-int number?
|
|
173
|
-
|
|
174
|
+
if (builtin === 'int' && Math.trunc(valueNew) !== valueNew) {
|
|
174
175
|
throwMemberError(type, value, memberFqn);
|
|
175
176
|
}
|
|
176
177
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "schema-markdown",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.5",
|
|
5
5
|
"description": "A schema definition and validation library",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"schema",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"test": "ava -v test/"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"ava": "~
|
|
27
|
+
"ava": "~5.1",
|
|
28
28
|
"c8": "~7.12",
|
|
29
|
-
"eslint": "~8.
|
|
30
|
-
"jsdoc": "~
|
|
31
|
-
"jsdom": "~
|
|
29
|
+
"eslint": "~8.32",
|
|
30
|
+
"jsdoc": "~4.0",
|
|
31
|
+
"jsdom": "~21.1"
|
|
32
32
|
}
|
|
33
33
|
}
|