schema-markdown 1.1.5 → 1.2.1
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 +55 -41
- package/lib/parser.js +481 -531
- package/lib/schema.js +21 -41
- package/lib/schemaUtil.js +1 -1
- package/lib/typeModel.js +6 -23
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -3,91 +3,105 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/schema-markdown)
|
|
4
4
|
[](https://github.com/craigahobbs/schema-markdown-js/blob/main/LICENSE)
|
|
5
5
|
|
|
6
|
-
**
|
|
7
|
-
its features at a glance:
|
|
8
|
-
|
|
9
|
-
- Schema-validate JSON objects
|
|
10
|
-
- Human-friendly schema definition
|
|
11
|
-
- Validates member value and length contraints
|
|
12
|
-
- Validation *type-massages* string member values
|
|
13
|
-
- Pure JavaScript
|
|
6
|
+
**schema-markdown** is a schema definition and validation library.
|
|
14
7
|
|
|
15
8
|
|
|
16
9
|
## Links
|
|
17
10
|
|
|
18
|
-
- [Schema Markdown Language
|
|
19
|
-
- [Documentation
|
|
20
|
-
- [
|
|
21
|
-
- [Source code on GitHub](https://github.com/craigahobbs/schema-markdown-js)
|
|
11
|
+
- [The Schema Markdown Language](https://craigahobbs.github.io/schema-markdown-js/language/)
|
|
12
|
+
- [API Documentation](https://craigahobbs.github.io/schema-markdown-js/)
|
|
13
|
+
- [Source code](https://github.com/craigahobbs/schema-markdown-js)
|
|
22
14
|
|
|
23
15
|
|
|
24
|
-
##
|
|
16
|
+
## Define a Schema
|
|
25
17
|
|
|
26
|
-
|
|
27
|
-
[
|
|
28
|
-
|
|
18
|
+
Schemas are defined using the
|
|
19
|
+
[Schema Markdown](https://craigahobbs.github.io/schema-markdown-js/language/)
|
|
20
|
+
language which is parsed by the
|
|
21
|
+
[parseSchemaMarkdown](https://craigahobbs.github.io/schema-markdown-js/module-lib_parser.html#.parseSchemaMarkdown)
|
|
22
|
+
function. For example:
|
|
29
23
|
|
|
30
24
|
``` javascript
|
|
31
|
-
import {
|
|
32
|
-
import {validateType} from 'schema-markdown/schema.js';
|
|
33
|
-
|
|
34
|
-
const parser = new SchemaMarkdownParser(`\
|
|
35
|
-
# An aggregation function
|
|
36
|
-
enum Aggregation
|
|
37
|
-
Average
|
|
38
|
-
Sum
|
|
25
|
+
import {parseSchemaMarkdown} from 'schema-markdown/parser.js';
|
|
39
26
|
|
|
27
|
+
export const modelTypes = parseSchemaMarkdown(`\
|
|
40
28
|
# An aggregate numerical operation
|
|
41
|
-
struct
|
|
29
|
+
struct Aggregation
|
|
30
|
+
|
|
42
31
|
# The aggregation function - default is "Sum"
|
|
43
|
-
optional
|
|
32
|
+
optional AggregationFunction aggregation
|
|
44
33
|
|
|
45
|
-
# The numbers to
|
|
34
|
+
# The numbers to aggregate on
|
|
46
35
|
int[len > 0] numbers
|
|
36
|
+
|
|
37
|
+
# An aggregation function
|
|
38
|
+
enum AggregationFunction
|
|
39
|
+
Average
|
|
40
|
+
Sum
|
|
47
41
|
`);
|
|
48
42
|
```
|
|
49
43
|
|
|
50
|
-
|
|
44
|
+
|
|
45
|
+
## Validate using a Schema
|
|
46
|
+
|
|
47
|
+
To validate an object using the schema, use the
|
|
51
48
|
[validateType](https://craigahobbs.github.io/schema-markdown-js/module-lib_schema.html#.validateType)
|
|
52
|
-
function:
|
|
49
|
+
function. For example:
|
|
53
50
|
|
|
54
51
|
``` javascript
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
52
|
+
import {validateType} from 'schema-markdown/schema.js';
|
|
53
|
+
|
|
54
|
+
const obj = validateType(modelTypes, 'Aggregation', {'numbers': [1, 2, '3', 4]});
|
|
58
55
|
console.assert(obj.numbers[2] === 3);
|
|
59
56
|
```
|
|
60
57
|
|
|
61
58
|
Notice that the numerical input '3' above is *type-massaged* to the integer 3 by validation.
|
|
59
|
+
|
|
62
60
|
Validation fails if the object does not match the schema:
|
|
63
61
|
|
|
64
62
|
``` javascript
|
|
65
63
|
try {
|
|
66
|
-
validateType(
|
|
67
|
-
'numbers': [1, 2, 'asdf', 4]
|
|
68
|
-
});
|
|
64
|
+
validateType(modelTypes, 'Aggregation', {'numbers': [1, 2, 'asdf', 4]});
|
|
69
65
|
} catch ({message}) {
|
|
70
66
|
console.assert(message === "Invalid value \"asdf\" (type 'string') for member 'numbers.2', expected type 'int'", message);
|
|
71
67
|
}
|
|
72
68
|
```
|
|
73
69
|
|
|
74
|
-
Validation also fails if a member
|
|
70
|
+
Validation also fails if a member constraint is violated:
|
|
75
71
|
|
|
76
72
|
``` javascript
|
|
77
73
|
try {
|
|
78
|
-
validateType(
|
|
79
|
-
'numbers': []
|
|
80
|
-
});
|
|
74
|
+
validateType(modelTypes, 'Aggregation', {'numbers': []});
|
|
81
75
|
} catch ({message}) {
|
|
82
76
|
console.assert(message === "Invalid value [] (type 'object') for member 'numbers', expected type 'array' [len > 0]", message);
|
|
83
77
|
}
|
|
84
78
|
```
|
|
85
79
|
|
|
86
80
|
|
|
81
|
+
## Document a Schema
|
|
82
|
+
|
|
83
|
+
To document the schema, download the
|
|
84
|
+
[documentation application](https://github.com/craigahobbs/schema-markdown-doc#the-schema-markdown-documentation-viewer)
|
|
85
|
+
stub and save the type model as JSON:
|
|
86
|
+
|
|
87
|
+
~~~
|
|
88
|
+
curl -O https://craigahobbs.github.io/schema-markdown-doc/extra/index.html
|
|
89
|
+
node --input-type=module \
|
|
90
|
+
-e 'import {modelTypes} from "model.js"; console.log(JSON.stringify(modelTypes))' \
|
|
91
|
+
> model.json
|
|
92
|
+
~~~
|
|
93
|
+
|
|
94
|
+
To host locally, start a local static web server:
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
python3 -m http.server
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
|
|
87
101
|
## Development
|
|
88
102
|
|
|
89
|
-
|
|
90
|
-
|
|
103
|
+
This package is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme).
|
|
104
|
+
It was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme) as follows:
|
|
91
105
|
|
|
92
106
|
```
|
|
93
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
|