@stxt-lang/core 0.5.2 → 0.6.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/README.md +116 -1
- package/out/all.d.ts +5 -0
- package/out/all.js +10 -4
- package/out/core/Constants.d.ts +8 -0
- package/out/core/Constants.js +8 -0
- package/out/core/Line.d.ts +20 -0
- package/out/core/Line.js +15 -0
- package/out/core/LineParser.d.ts +17 -0
- package/out/core/LineParser.js +27 -10
- package/out/core/NameNamespace.d.ts +9 -0
- package/out/core/NameNamespace.js +9 -0
- package/out/core/NameNamespaceParser.d.ts +11 -0
- package/out/core/NameNamespaceParser.js +16 -5
- package/out/core/NamespaceValidator.d.ts +18 -0
- package/out/core/NamespaceValidator.js +18 -0
- package/out/core/Node.d.ts +55 -0
- package/out/core/Node.js +56 -1
- package/out/core/NodeCreator.d.ts +11 -0
- package/out/core/NodeCreator.js +14 -3
- package/out/core/ParseResult.d.ts +26 -0
- package/out/core/ParseResult.js +26 -0
- package/out/core/Parser.d.ts +31 -0
- package/out/core/Parser.js +50 -19
- package/out/core/StringUtils.d.ts +31 -0
- package/out/core/StringUtils.js +40 -9
- package/out/discovery/DiscoveryEnvironment.d.ts +35 -0
- package/out/discovery/DiscoveryEnvironment.js +3 -0
- package/out/discovery/DiscoveryError.d.ts +30 -0
- package/out/discovery/DiscoveryError.js +36 -0
- package/out/discovery/DiscoveryFileSystem.d.ts +57 -0
- package/out/discovery/DiscoveryFileSystem.js +3 -0
- package/out/discovery/DiscoveryResolver.d.ts +74 -0
- package/out/discovery/DiscoveryResolver.js +232 -0
- package/out/discovery/DiscoveryResult.d.ts +94 -0
- package/out/discovery/DiscoveryResult.js +111 -0
- package/out/exceptions/ParseException.d.ts +14 -0
- package/out/exceptions/ParseException.js +12 -0
- package/out/exceptions/RuntimeException.d.ts +14 -0
- package/out/exceptions/RuntimeException.js +13 -0
- package/out/exceptions/ValidationException.d.ts +8 -0
- package/out/exceptions/ValidationException.js +8 -0
- package/out/processors/Observer.d.ts +29 -0
- package/out/processors/Validator.d.ts +9 -0
- package/out/runtime/ConditionalValidator.d.ts +15 -0
- package/out/runtime/ConditionalValidator.js +16 -2
- package/out/runtime/NodeWriter.d.ts +18 -0
- package/out/runtime/NodeWriter.js +18 -0
- package/out/runtime/UnifiedSchemaProvider.d.ts +23 -5
- package/out/runtime/UnifiedSchemaProvider.js +28 -10
- package/out/schema/ChildDefinition.d.ts +18 -0
- package/out/schema/ChildDefinition.js +18 -0
- package/out/schema/NodeDefinition.d.ts +44 -0
- package/out/schema/NodeDefinition.js +47 -3
- package/out/schema/Schema.d.ts +26 -0
- package/out/schema/Schema.js +26 -1
- package/out/schema/SchemaParser.d.ts +7 -0
- package/out/schema/SchemaParser.js +26 -16
- package/out/schema/SchemaProvider.d.ts +7 -0
- package/out/schema/SchemaProviderMemory.d.ts +27 -0
- package/out/schema/SchemaProviderMemory.js +27 -0
- package/out/schema/SchemaProviderMeta.d.ts +16 -0
- package/out/schema/SchemaProviderMeta.js +16 -0
- package/out/schema/SchemaValidator.d.ts +20 -0
- package/out/schema/SchemaValidator.js +27 -7
- package/out/schema/Type.d.ts +9 -0
- package/out/schema/TypeRegistry.d.ts +13 -0
- package/out/schema/TypeRegistry.js +18 -5
- package/out/schema/type/BASE64.d.ts +1 -0
- package/out/schema/type/BASE64.js +5 -4
- package/out/schema/type/BINARY.d.ts +1 -0
- package/out/schema/type/BINARY.js +2 -1
- package/out/schema/type/BLOCK.d.ts +1 -0
- package/out/schema/type/BLOCK.js +2 -1
- package/out/schema/type/BOOLEAN.d.ts +1 -0
- package/out/schema/type/BOOLEAN.js +1 -0
- package/out/schema/type/DATE.d.ts +1 -0
- package/out/schema/type/DATE.js +1 -0
- package/out/schema/type/EMAIL.d.ts +1 -0
- package/out/schema/type/EMAIL.js +1 -0
- package/out/schema/type/ENUM.d.ts +1 -0
- package/out/schema/type/ENUM.js +2 -1
- package/out/schema/type/GROUP.d.ts +1 -0
- package/out/schema/type/GROUP.js +2 -1
- package/out/schema/type/HEXADECIMAL.d.ts +1 -0
- package/out/schema/type/HEXADECIMAL.js +2 -1
- package/out/schema/type/INLINE.d.ts +1 -0
- package/out/schema/type/INLINE.js +2 -1
- package/out/schema/type/INTEGER.d.ts +1 -0
- package/out/schema/type/INTEGER.js +1 -0
- package/out/schema/type/MARKDOWN.d.ts +4 -0
- package/out/schema/type/MARKDOWN.js +4 -2
- package/out/schema/type/NATURAL.d.ts +1 -0
- package/out/schema/type/NATURAL.js +1 -0
- package/out/schema/type/NUMBER.d.ts +1 -0
- package/out/schema/type/NUMBER.js +1 -0
- package/out/schema/type/TEXT.d.ts +1 -0
- package/out/schema/type/TEXT.js +1 -0
- package/out/schema/type/TIME.d.ts +1 -0
- package/out/schema/type/TIME.js +1 -0
- package/out/schema/type/TIMESTAMP.d.ts +1 -0
- package/out/schema/type/TIMESTAMP.js +1 -0
- package/out/schema/type/URL.d.ts +1 -0
- package/out/schema/type/URL.js +2 -1
- package/out/schema/type/UUID.d.ts +1 -0
- package/out/schema/type/UUID.js +1 -0
- package/out/schema/type/binaryValue.d.ts +9 -0
- package/out/schema/type/binaryValue.js +9 -3
- package/out/schema/type/regexType.d.ts +8 -0
- package/out/schema/type/regexType.js +9 -1
- package/out/template/ChildLine.d.ts +14 -0
- package/out/template/ChildLine.js +14 -0
- package/out/template/ChildLineParser.d.ts +10 -0
- package/out/template/ChildLineParser.js +17 -8
- package/out/template/MetaTemplateSchemaProvider.d.ts +16 -0
- package/out/template/MetaTemplateSchemaProvider.js +17 -1
- package/out/template/TemplateParser.d.ts +8 -0
- package/out/template/TemplateParser.js +49 -39
- package/out/template/TemplateSchemaProviderMemory.d.ts +18 -0
- package/out/template/TemplateSchemaProviderMemory.js +21 -3
- package/package.json +2 -1
|
@@ -5,7 +5,16 @@ const Parser_1 = require("../core/Parser");
|
|
|
5
5
|
const ValidationException_1 = require("../exceptions/ValidationException");
|
|
6
6
|
const RuntimeException_1 = require("../exceptions/RuntimeException");
|
|
7
7
|
const TemplateParser_1 = require("./TemplateParser");
|
|
8
|
+
/**
|
|
9
|
+
* {@link SchemaProvider} that defines in code the meta-schema of the template language itself
|
|
10
|
+
* (`@stxt.template`), so that a loaded template can validate itself.
|
|
11
|
+
*/
|
|
8
12
|
class MetaTemplateSchemaProvider {
|
|
13
|
+
/**
|
|
14
|
+
* Parses the meta-template and keeps the schema it produces ready to be served.
|
|
15
|
+
*
|
|
16
|
+
* @throws ValidationException with code `META_SCHEMA_INVALID` if the meta-template does not produce exactly one document.
|
|
17
|
+
*/
|
|
9
18
|
constructor() {
|
|
10
19
|
const parser = new Parser_1.Parser();
|
|
11
20
|
const nodes = parser.parse(MetaTemplateSchemaProvider.META_TEXT);
|
|
@@ -14,11 +23,18 @@ class MetaTemplateSchemaProvider {
|
|
|
14
23
|
}
|
|
15
24
|
this.meta = (0, TemplateParser_1.transformTemplateNodeToSchema)(nodes[0]);
|
|
16
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Serves the meta-schema of the template language.
|
|
28
|
+
*
|
|
29
|
+
* @param namespace namespace whose schema is wanted; only `@stxt.template` is served.
|
|
30
|
+
* @returns the meta-schema of the template language.
|
|
31
|
+
* @throws RuntimeException with code `RESOURCE_NOT_FOUND` if any other namespace is asked for.
|
|
32
|
+
*/
|
|
17
33
|
getSchema(namespace) {
|
|
18
34
|
if (namespace !== "@stxt.template") {
|
|
19
35
|
throw new RuntimeException_1.RuntimeException("RESOURCE_NOT_FOUND", `Not found '${namespace}' in namespace: @stxt.template`);
|
|
20
36
|
}
|
|
21
|
-
// meta
|
|
37
|
+
// meta always exists once the constructor finished, but this mirrors the Java version
|
|
22
38
|
if (!this.meta) {
|
|
23
39
|
throw new ValidationException_1.ValidationException(0, "META_SCHEMA_NOT_AVAILABLE", "Meta schema not available");
|
|
24
40
|
}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
import { Node } from "../core/Node";
|
|
2
2
|
import { Schema } from "../schema/Schema";
|
|
3
|
+
/**
|
|
4
|
+
* Turns the tree of an already parsed `@stxt.template` document into an equivalent {@link Schema}.
|
|
5
|
+
*
|
|
6
|
+
* @param node root of the already parsed `@stxt.template` document.
|
|
7
|
+
* @returns the resulting {@link Schema}.
|
|
8
|
+
* @throws ValidationException if the template is not valid, with the line already shifted to the
|
|
9
|
+
* one of the original document.
|
|
10
|
+
*/
|
|
3
11
|
export declare function transformTemplateNodeToSchema(node: Node): Schema;
|
|
@@ -10,29 +10,37 @@ const StringUtils_1 = require("../core/StringUtils");
|
|
|
10
10
|
const ChildLineParser_1 = require("./ChildLineParser");
|
|
11
11
|
const ParseException_1 = require("../exceptions/ParseException");
|
|
12
12
|
const TypeRegistry_1 = require("../schema/TypeRegistry");
|
|
13
|
+
/**
|
|
14
|
+
* Turns the tree of an already parsed `@stxt.template` document into an equivalent {@link Schema}.
|
|
15
|
+
*
|
|
16
|
+
* @param node root of the already parsed `@stxt.template` document.
|
|
17
|
+
* @returns the resulting {@link Schema}.
|
|
18
|
+
* @throws ValidationException if the template is not valid, with the line already shifted to the
|
|
19
|
+
* one of the original document.
|
|
20
|
+
*/
|
|
13
21
|
function transformTemplateNodeToSchema(node) {
|
|
14
|
-
//
|
|
22
|
+
// Set the namespace
|
|
15
23
|
const result = new Schema_1.Schema(node.getValue(), node.getLine(), undefined);
|
|
16
|
-
//
|
|
24
|
+
// Look for the structure node
|
|
17
25
|
const structure = node.getChild("structure");
|
|
18
26
|
if (!structure) {
|
|
19
27
|
throw new ValidationException_1.ValidationException(node.getLine(), "TEMPLATE_STRUCTURE_REQUIRED", "Template must define 'Structure >>'");
|
|
20
28
|
}
|
|
21
29
|
const text = structure.getText();
|
|
22
30
|
const offset = structure.getLine();
|
|
23
|
-
//
|
|
31
|
+
// Build a plain parser
|
|
24
32
|
const parser = new Parser_1.Parser();
|
|
25
|
-
//
|
|
33
|
+
// Parse to get the nodes
|
|
26
34
|
try {
|
|
27
35
|
const nodes = parser.parse(text);
|
|
28
|
-
//
|
|
36
|
+
// Walk every node adding it to the schema
|
|
29
37
|
for (const n of nodes) {
|
|
30
38
|
addToSchema(result, n);
|
|
31
39
|
}
|
|
32
40
|
}
|
|
33
41
|
catch (e) {
|
|
34
|
-
// ValidationException
|
|
35
|
-
//
|
|
42
|
+
// ValidationException extends ParseException: check it first so the severity is not
|
|
43
|
+
// downgraded (the extension paints ValidationException as a Warning)
|
|
36
44
|
if (e instanceof ValidationException_1.ValidationException) {
|
|
37
45
|
throw new ValidationException_1.ValidationException(e.line + offset, e.code, e.message);
|
|
38
46
|
}
|
|
@@ -41,7 +49,7 @@ function transformTemplateNodeToSchema(node) {
|
|
|
41
49
|
}
|
|
42
50
|
throw e;
|
|
43
51
|
}
|
|
44
|
-
//
|
|
52
|
+
// Look for the descriptions
|
|
45
53
|
const description = node.getChild("description");
|
|
46
54
|
if (description) {
|
|
47
55
|
const text = description.getText();
|
|
@@ -59,21 +67,22 @@ function transformTemplateNodeToSchema(node) {
|
|
|
59
67
|
throw e;
|
|
60
68
|
}
|
|
61
69
|
}
|
|
62
|
-
//
|
|
70
|
+
// Return the result
|
|
63
71
|
return result;
|
|
64
72
|
}
|
|
73
|
+
/** Adds to the schema the definition a node of the structure declares, along with its children. */
|
|
65
74
|
function addToSchema(schema, node) {
|
|
66
|
-
//
|
|
75
|
+
// Get the qualified name
|
|
67
76
|
let namespace = node.getNamespace();
|
|
68
77
|
const name = node.getName();
|
|
69
|
-
//
|
|
78
|
+
// Look at the data
|
|
70
79
|
let cl = ChildLineParser_1.ChildLineParser.parse(node.getValue(), node.getLine());
|
|
71
80
|
if (!namespace || namespace === "") {
|
|
72
81
|
namespace = schema.getNamespace();
|
|
73
82
|
}
|
|
74
83
|
if (namespace !== schema.getNamespace()) {
|
|
75
|
-
//
|
|
76
|
-
//
|
|
84
|
+
// An external node may only declare cardinality: no type, no ENUM values
|
|
85
|
+
// and no children (STXT-TEMPLATE-SPEC 6.4, 10 and 14.15)
|
|
77
86
|
const type = cl.getType();
|
|
78
87
|
if (type && type.trim().length > 0) {
|
|
79
88
|
throw new ValidationException_1.ValidationException(node.getLine(), "TYPE_DEFINITION_NOT_ALLOWED", "Not allowed type definition in external namespaces");
|
|
@@ -85,17 +94,17 @@ function addToSchema(schema, node) {
|
|
|
85
94
|
if (node.getChildren().length > 0) {
|
|
86
95
|
throw new ValidationException_1.ValidationException(node.getLine(), "CHILDREN_NOT_ALLOWED_IN_EXTERNAL_NAMESPACE", `Not allowed children in external namespaces (node ${node.getName()})`);
|
|
87
96
|
}
|
|
88
|
-
//
|
|
97
|
+
// Nodes that do not belong to @stxt.template create nothing here!!
|
|
89
98
|
return;
|
|
90
99
|
}
|
|
91
|
-
//
|
|
100
|
+
// Check whether it is new and add it to the list
|
|
92
101
|
let schemaNode = schema.getNodeDefinition(name);
|
|
93
102
|
if (!schemaNode) {
|
|
94
|
-
//
|
|
103
|
+
// New one
|
|
95
104
|
const type = cl.getType() ?? "INLINE";
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
105
|
+
// At this point the schema already holds both the previous definitions, already closed,
|
|
106
|
+
// and the open ancestors, so a reference that does not resolve here resolves to
|
|
107
|
+
// nothing (STXT-TEMPLATE-SPEC 6.4 and 14.11)
|
|
99
108
|
if (type.startsWith("@")) {
|
|
100
109
|
throw new ValidationException_1.ValidationException(node.getLine(), "REFERENCE_NOT_FOUND", `Reference '${type}' does not point to a previous definition or an open ancestor`);
|
|
101
110
|
}
|
|
@@ -107,16 +116,16 @@ function addToSchema(schema, node) {
|
|
|
107
116
|
const values = cl.getValues();
|
|
108
117
|
if (values) {
|
|
109
118
|
if (type !== "ENUM") {
|
|
110
|
-
//
|
|
111
|
-
// (STXT-TEMPLATE-SPEC 13),
|
|
112
|
-
//
|
|
119
|
+
// Same code as SchemaParser: a template is sugar equivalent to a schema
|
|
120
|
+
// (STXT-TEMPLATE-SPEC 13), so the same condition must not change its code
|
|
121
|
+
// depending on the entry point
|
|
113
122
|
throw new ValidationException_1.ValidationException(node.getLine(), "VALUES_ONLY_SUPPORTED_BY_ENUM", `Values only supported for type ENUM, not for type ${type}`);
|
|
114
123
|
}
|
|
115
124
|
for (const v of values) {
|
|
116
125
|
schemaNode.addValue(v, node.getLine());
|
|
117
126
|
}
|
|
118
127
|
}
|
|
119
|
-
//
|
|
128
|
+
// An ENUM with no list of values is an invalid template (STXT-TEMPLATE-SPEC 9 and 13.7)
|
|
120
129
|
if (type === "ENUM" && (!values || values.length === 0)) {
|
|
121
130
|
throw new ValidationException_1.ValidationException(node.getLine(), "VALUES_EMPTY_FOR_ENUM", "ENUM Type must include values");
|
|
122
131
|
}
|
|
@@ -127,7 +136,7 @@ function addToSchema(schema, node) {
|
|
|
127
136
|
throw new ValidationException_1.ValidationException(node.getLine(), "NODE_DEFINED_MULTIPLE_TIMES", `Multiple node reference must start with @: ${node.getName()}`);
|
|
128
137
|
}
|
|
129
138
|
const reference = type.substring(1).trim();
|
|
130
|
-
//
|
|
139
|
+
// Reference and explicit type on the same line (STXT-TEMPLATE-SPEC 14.13)
|
|
131
140
|
const explicitType = referenceType(reference, node.getNormalizedName());
|
|
132
141
|
if (explicitType) {
|
|
133
142
|
throw new ValidationException_1.ValidationException(node.getLine(), "REFERENCE_WITH_TYPE_NOT_ALLOWED", `Reference '@${node.getName()}' can not declare a type: ${explicitType}`);
|
|
@@ -135,8 +144,8 @@ function addToSchema(schema, node) {
|
|
|
135
144
|
if (StringUtils_1.StringUtils.normalize(reference) !== node.getNormalizedName()) {
|
|
136
145
|
throw new ValidationException_1.ValidationException(node.getLine(), "NODE_REFERENCE_NOT_VALID", `Reference must be '@${node.getName()}', not '${reference}'`);
|
|
137
146
|
}
|
|
138
|
-
//
|
|
139
|
-
//
|
|
147
|
+
// A reference may override the cardinality, but it may redefine neither the ENUM
|
|
148
|
+
// values nor the children (STXT-TEMPLATE-SPEC 6.4)
|
|
140
149
|
const values = cl.getValues();
|
|
141
150
|
if (values) {
|
|
142
151
|
throw new ValidationException_1.ValidationException(node.getLine(), "VALUES_NOT_ALLOWED_IN_REFERENCE", `Reference '@${node.getName()}' can not redefine ENUM values`);
|
|
@@ -146,13 +155,13 @@ function addToSchema(schema, node) {
|
|
|
146
155
|
}
|
|
147
156
|
return; // OK Definition
|
|
148
157
|
}
|
|
149
|
-
//
|
|
158
|
+
// Once it exists, try to create its children if it has any.
|
|
150
159
|
const childrenNode = node.getChildren();
|
|
151
|
-
//
|
|
160
|
+
// Template error 14.9: node with children and an effective type that does not admit children
|
|
152
161
|
if (childrenNode.length > 0 && !TypeRegistry_1.TypeRegistry.admitsChildren(schemaNode.getType())) {
|
|
153
162
|
throw new ValidationException_1.ValidationException(node.getLine(), "CHILDREN_NOT_ALLOWED_FOR_TYPE", `Type ${schemaNode.getType()} does not allow children (node ${node.getName()})`);
|
|
154
163
|
}
|
|
155
|
-
//
|
|
164
|
+
// Add the children
|
|
156
165
|
for (const child of childrenNode) {
|
|
157
166
|
cl = ChildLineParser_1.ChildLineParser.parse(child.getValue(), child.getLine());
|
|
158
167
|
const childName = child.getName();
|
|
@@ -166,11 +175,11 @@ function addToSchema(schema, node) {
|
|
|
166
175
|
}
|
|
167
176
|
}
|
|
168
177
|
/**
|
|
169
|
-
*
|
|
170
|
-
* (
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
178
|
+
* Tells `@Node Name TYPE` (reference + type, error 14.13) apart from `@Another Name`
|
|
179
|
+
* (reference with a different name, error 14.12). Since node names may contain spaces,
|
|
180
|
+
* the only reliable reading is: if the last token is a known type and what comes before
|
|
181
|
+
* it is the name of the node itself, the line is declaring both things.
|
|
182
|
+
* Returns the declared type, or null when the reference carries no type.
|
|
174
183
|
*/
|
|
175
184
|
function referenceType(reference, normalizedName) {
|
|
176
185
|
const cut = reference.lastIndexOf(" ");
|
|
@@ -184,27 +193,28 @@ function referenceType(reference, normalizedName) {
|
|
|
184
193
|
}
|
|
185
194
|
return null;
|
|
186
195
|
}
|
|
196
|
+
/** Attaches to the node definitions the descriptions declared in the `Description >>` block. */
|
|
187
197
|
function addDescriptions(schema, nodes) {
|
|
188
198
|
nodes.forEach((node) => {
|
|
189
|
-
//
|
|
199
|
+
// Get the namespace
|
|
190
200
|
let namespace = node.getNamespace();
|
|
191
201
|
if (!namespace || namespace === "") {
|
|
192
202
|
namespace = schema.getNamespace();
|
|
193
203
|
}
|
|
194
|
-
//
|
|
204
|
+
// No external descriptions
|
|
195
205
|
if (namespace !== schema.getNamespace()) {
|
|
196
206
|
throw new ValidationException_1.ValidationException(node.getLine(), "EXTERNAL_DESCRIPTION_NOT_ALLOWED", "Not allowed description in external namespaces");
|
|
197
207
|
}
|
|
198
|
-
//
|
|
208
|
+
// No children either
|
|
199
209
|
if (node.getChildren().length > 0) {
|
|
200
210
|
throw new ValidationException_1.ValidationException(node.getLine(), "CHILDREN_DESCRIPTION_NOT_ALLOWED", "Not allowed children in description");
|
|
201
211
|
}
|
|
202
|
-
//
|
|
212
|
+
// Look for the node in the schema
|
|
203
213
|
const nodeDef = schema.getNodeDefinition(node.getName());
|
|
204
214
|
if (!nodeDef) {
|
|
205
215
|
throw new ValidationException_1.ValidationException(node.getLine(), "NODE_NOT_FOUND", `Not found node with name: ${node.getName()}`);
|
|
206
216
|
}
|
|
207
|
-
// No
|
|
217
|
+
// No more than one entry per node is allowed (STXT-TEMPLATE-SPEC 12)
|
|
208
218
|
if (nodeDef.getDescription() !== undefined) {
|
|
209
219
|
throw new ValidationException_1.ValidationException(node.getLine(), "DESCRIPTION_ALREADY_DEFINED", `Exists a previous description for node: ${node.getName()}`);
|
|
210
220
|
}
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { SchemaProviderMemory } from "../schema/SchemaProviderMemory";
|
|
2
2
|
import { SchemaProvider } from "../schema/SchemaProvider";
|
|
3
|
+
/**
|
|
4
|
+
* In-memory {@link SchemaProvider} fed with `@stxt.template` documents: each template is turned
|
|
5
|
+
* into its equivalent {@link Schema} and registered under its own namespace.
|
|
6
|
+
*/
|
|
3
7
|
export declare class TemplateSchemaProviderMemory extends SchemaProviderMemory {
|
|
8
|
+
/**
|
|
9
|
+
* Creates an empty provider.
|
|
10
|
+
*
|
|
11
|
+
* @param parent provider to fall back to when a namespace is not registered here; the
|
|
12
|
+
* template meta-schema provider when omitted.
|
|
13
|
+
*/
|
|
4
14
|
constructor(parent?: SchemaProvider | null | undefined);
|
|
15
|
+
/**
|
|
16
|
+
* Parses a template document, validates it against the template meta-schema and registers the
|
|
17
|
+
* schema it produces.
|
|
18
|
+
*
|
|
19
|
+
* @param template text of the `@stxt.template` document.
|
|
20
|
+
* @throws ValidationException with code `INVALID_SCHEMA` if the document does not hold exactly
|
|
21
|
+
* one template, or if the resulting schema has no namespace.
|
|
22
|
+
*/
|
|
5
23
|
addTemplate(template: string): void;
|
|
6
24
|
}
|
|
@@ -8,25 +8,43 @@ const ValidationException_1 = require("../exceptions/ValidationException");
|
|
|
8
8
|
const MetaTemplateSchemaProvider_1 = require("./MetaTemplateSchemaProvider");
|
|
9
9
|
const SchemaProviderMemory_1 = require("../schema/SchemaProviderMemory");
|
|
10
10
|
const TemplateParser_1 = require("./TemplateParser");
|
|
11
|
+
/**
|
|
12
|
+
* In-memory {@link SchemaProvider} fed with `@stxt.template` documents: each template is turned
|
|
13
|
+
* into its equivalent {@link Schema} and registered under its own namespace.
|
|
14
|
+
*/
|
|
11
15
|
class TemplateSchemaProviderMemory extends SchemaProviderMemory_1.SchemaProviderMemory {
|
|
16
|
+
/**
|
|
17
|
+
* Creates an empty provider.
|
|
18
|
+
*
|
|
19
|
+
* @param parent provider to fall back to when a namespace is not registered here; the
|
|
20
|
+
* template meta-schema provider when omitted.
|
|
21
|
+
*/
|
|
12
22
|
constructor(parent) {
|
|
13
23
|
if (!parent) {
|
|
14
24
|
parent = new MetaTemplateSchemaProvider_1.MetaTemplateSchemaProvider();
|
|
15
25
|
}
|
|
16
26
|
super(parent);
|
|
17
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Parses a template document, validates it against the template meta-schema and registers the
|
|
30
|
+
* schema it produces.
|
|
31
|
+
*
|
|
32
|
+
* @param template text of the `@stxt.template` document.
|
|
33
|
+
* @throws ValidationException with code `INVALID_SCHEMA` if the document does not hold exactly
|
|
34
|
+
* one template, or if the resulting schema has no namespace.
|
|
35
|
+
*/
|
|
18
36
|
addTemplate(template) {
|
|
19
37
|
const parser = new Parser_1.Parser();
|
|
20
38
|
const nodes = parser.parse(template);
|
|
21
39
|
if (nodes.length !== 1) {
|
|
22
40
|
throw new ValidationException_1.ValidationException(0, "INVALID_SCHEMA", `There are ${nodes.length}, and expected is 1`);
|
|
23
41
|
}
|
|
24
|
-
//
|
|
42
|
+
// Validate the template against the template meta-schema
|
|
25
43
|
const schemaValidator = new SchemaValidator_1.SchemaValidator(new MetaTemplateSchemaProvider_1.MetaTemplateSchemaProvider(), true);
|
|
26
44
|
schemaValidator.validate(nodes[0]);
|
|
27
|
-
//
|
|
45
|
+
// Build the schema out of the template
|
|
28
46
|
const sch = (0, TemplateParser_1.transformTemplateNodeToSchema)(nodes[0]);
|
|
29
|
-
//
|
|
47
|
+
// Minimum safety check (Java checked the expected namespace here too)
|
|
30
48
|
if (!sch.getNamespace() || sch.getNamespace().trim().length === 0) {
|
|
31
49
|
throw new ValidationException_1.ValidationException(0, "INVALID_SCHEMA", "Schema namespace is empty");
|
|
32
50
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stxt-lang/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Parser and schema validator for STXT, an indentation-based structured-text format.",
|
|
5
5
|
"main": "out/all.js",
|
|
6
6
|
"types": "out/all.d.ts",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"out/all.js",
|
|
12
12
|
"out/all.d.ts",
|
|
13
13
|
"out/core",
|
|
14
|
+
"out/discovery",
|
|
14
15
|
"out/exceptions",
|
|
15
16
|
"out/processors",
|
|
16
17
|
"out/runtime",
|