schema-markdown 1.2.4 → 1.2.6

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 CHANGED
@@ -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
- ``` javascript
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
- ``` javascript
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
- ``` javascript
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
- ``` javascript
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>\/[^\s]*))?/;
49
+ const rURL = /^\s+(?<method>[A-Za-z]+|\*)(?:\s+(?<path>\/\S*))?/;
50
50
  const rLineSplit = /\r?\n/;
51
51
 
52
52
 
@@ -54,7 +54,8 @@ const rLineSplit = /\r?\n/;
54
54
  * Parse Schema Markdown from a string or an iterator of strings
55
55
  *
56
56
  * @param {string|string[]} text - The Schema Markdown text
57
- * @param {Object} [options.types=''] - The [type model]{@link https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='Types'}
57
+ * @param {Object} [options = {}] - The options object
58
+ * @param {Object} [options.types={}] - The [type model]{@link https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='Types'}
58
59
  * @param {string} [options.filename=''] - The name of file being parsed (for error messages)
59
60
  * @param {boolean} [options.validate=true] - If true, validate after parsing
60
61
  * @returns {Object} The [type model]{@link https://craigahobbs.github.io/schema-markdown-doc/doc/#var.vName='Types'}
@@ -375,7 +376,8 @@ export function parseSchemaMarkdown(text, {types = {}, filename = '', validate =
375
376
 
376
377
  // Duplicate URL?
377
378
  if (urls.some((url) => url.method === actionUrl.method && url.path === actionUrl.path)) {
378
- addError(`Duplicate URL: ${method} ${'path' in actionUrl ? actionUrl.path : ''}`, filename, linenum);
379
+ const paddedPath = 'path' in actionUrl ? ` ${actionUrl.path}` : '';
380
+ addError(`Duplicate URL: ${method}${paddedPath}`, filename, linenum);
379
381
  }
380
382
 
381
383
  // Add the URL
@@ -580,6 +582,7 @@ export class SchemaMarkdownParserError extends Error {
580
582
  */
581
583
  constructor(errors) {
582
584
  super(errors.join('\n'));
585
+ this.name = this.constructor.name;
583
586
  this.errors = errors;
584
587
  }
585
588
  }
package/lib/schema.js CHANGED
@@ -108,6 +108,7 @@ export class ValidationError extends Error {
108
108
  */
109
109
  constructor(message, memberFqn = null) {
110
110
  super(message);
111
+ this.name = this.constructor.name;
111
112
  this.memberFqn = memberFqn;
112
113
  }
113
114
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "schema-markdown",
4
- "version": "1.2.4",
4
+ "version": "1.2.6",
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": "~4.3",
28
- "c8": "~7.12",
29
- "eslint": "~8.24",
30
- "jsdoc": "~3.6",
31
- "jsdom": "~20.0"
27
+ "ava": "~5.2",
28
+ "c8": "~7.13",
29
+ "eslint": "~8.40",
30
+ "jsdoc": "~4.0",
31
+ "jsdom": "~22.0"
32
32
  }
33
33
  }