@ja-platform/parser-schema 0.0.0-dev-20261019214152 → 0.0.0-stage
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 +3 -0
- package/package.json +3 -38
- package/dist/@types/Version.d.ts +0 -19
- package/dist/@types/day/Day.d.ts +0 -4
- package/dist/@types/day/DayConstructorInterface.d.ts +0 -50
- package/dist/@types/day/DayInterface.d.ts +0 -34
- package/dist/@types/day/index.d.ts +0 -2
- package/dist/@types/day/utils.d.ts +0 -4
- package/dist/@types/expressions/SchemaObjectDisplayExpression.d.ts +0 -17
- package/dist/@types/index.d.ts +0 -16
- package/dist/@types/schema/ModelIndex.d.ts +0 -19
- package/dist/@types/schema/Schema.d.ts +0 -51
- package/dist/@types/schema/SchemaParserErrors.d.ts +0 -10
- package/dist/@types/schema/TypeFactory.d.ts +0 -26
- package/dist/@types/schema/reservedNames.d.ts +0 -7
- package/dist/@types/schema/schemaParser.d.ts +0 -43
- package/dist/@types/schema/schemaSerializer.d.ts +0 -11
- package/dist/@types/types/EnumOption.d.ts +0 -15
- package/dist/@types/types/FunctionType.d.ts +0 -15
- package/dist/@types/types/Param.d.ts +0 -19
- package/dist/@types/types/QueryType.d.ts +0 -19
- package/dist/@types/types/Relationship.d.ts +0 -20
- package/dist/@types/types/SchemaModelType.d.ts +0 -89
- package/dist/@types/types/Variable.d.ts +0 -32
- package/dist/@types/types/primitives/Boolean.d.ts +0 -21
- package/dist/@types/types/primitives/ChoiceType.d.ts +0 -47
- package/dist/@types/types/primitives/Date.d.ts +0 -13
- package/dist/@types/types/primitives/Datetime.d.ts +0 -9
- package/dist/@types/types/primitives/GeoTrack.d.ts +0 -5
- package/dist/@types/types/primitives/Location.d.ts +0 -6
- package/dist/@types/types/primitives/MultipleChoice.d.ts +0 -5
- package/dist/@types/types/primitives/MultipleChoiceInteger.d.ts +0 -8
- package/dist/@types/types/primitives/SingleChoice.d.ts +0 -5
- package/dist/@types/types/primitives/SingleChoiceInteger.d.ts +0 -8
- package/dist/@types/types/primitives/Text.d.ts +0 -8
- package/dist/@types/types/primitives/attachments/Attachment.d.ts +0 -9
- package/dist/@types/types/primitives/attachments/Photo.d.ts +0 -6
- package/dist/@types/types/primitives/attachments/Signature.d.ts +0 -6
- package/dist/@types/types/primitives/index.d.ts +0 -67
- package/dist/Version.js +0 -76
- package/dist/Version.js.map +0 -1
- package/dist/day/Day.js +0 -112
- package/dist/day/Day.js.map +0 -1
- package/dist/day/DayConstructorInterface.js +0 -3
- package/dist/day/DayConstructorInterface.js.map +0 -1
- package/dist/day/DayInterface.js +0 -3
- package/dist/day/DayInterface.js.map +0 -1
- package/dist/day/index.js +0 -19
- package/dist/day/index.js.map +0 -1
- package/dist/day/utils.js +0 -43
- package/dist/day/utils.js.map +0 -1
- package/dist/expressions/SchemaObjectDisplayExpression.js +0 -40
- package/dist/expressions/SchemaObjectDisplayExpression.js.map +0 -1
- package/dist/index.js +0 -35
- package/dist/index.js.map +0 -1
- package/dist/schema/ModelIndex.js +0 -14
- package/dist/schema/ModelIndex.js.map +0 -1
- package/dist/schema/Schema.js +0 -132
- package/dist/schema/Schema.js.map +0 -1
- package/dist/schema/SchemaParserErrors.js +0 -26
- package/dist/schema/SchemaParserErrors.js.map +0 -1
- package/dist/schema/TypeFactory.js +0 -21
- package/dist/schema/TypeFactory.js.map +0 -1
- package/dist/schema/reservedNames.js +0 -101
- package/dist/schema/reservedNames.js.map +0 -1
- package/dist/schema/schemaParser.js +0 -1135
- package/dist/schema/schemaParser.js.map +0 -1
- package/dist/schema/schemaSerializer.js +0 -181
- package/dist/schema/schemaSerializer.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/EnumOption.js +0 -22
- package/dist/types/EnumOption.js.map +0 -1
- package/dist/types/FunctionType.js +0 -30
- package/dist/types/FunctionType.js.map +0 -1
- package/dist/types/Param.js +0 -35
- package/dist/types/Param.js.map +0 -1
- package/dist/types/QueryType.js +0 -37
- package/dist/types/QueryType.js.map +0 -1
- package/dist/types/Relationship.js +0 -25
- package/dist/types/Relationship.js.map +0 -1
- package/dist/types/SchemaModelType.js +0 -147
- package/dist/types/SchemaModelType.js.map +0 -1
- package/dist/types/Variable.js +0 -49
- package/dist/types/Variable.js.map +0 -1
- package/dist/types/primitives/Boolean.js +0 -9
- package/dist/types/primitives/Boolean.js.map +0 -1
- package/dist/types/primitives/ChoiceType.js +0 -65
- package/dist/types/primitives/ChoiceType.js.map +0 -1
- package/dist/types/primitives/Date.js +0 -68
- package/dist/types/primitives/Date.js.map +0 -1
- package/dist/types/primitives/Datetime.js +0 -31
- package/dist/types/primitives/Datetime.js.map +0 -1
- package/dist/types/primitives/GeoTrack.js +0 -12
- package/dist/types/primitives/GeoTrack.js.map +0 -1
- package/dist/types/primitives/Location.js +0 -18
- package/dist/types/primitives/Location.js.map +0 -1
- package/dist/types/primitives/MultipleChoice.js +0 -12
- package/dist/types/primitives/MultipleChoice.js.map +0 -1
- package/dist/types/primitives/MultipleChoiceInteger.js +0 -34
- package/dist/types/primitives/MultipleChoiceInteger.js.map +0 -1
- package/dist/types/primitives/SingleChoice.js +0 -12
- package/dist/types/primitives/SingleChoice.js.map +0 -1
- package/dist/types/primitives/SingleChoiceInteger.js +0 -38
- package/dist/types/primitives/SingleChoiceInteger.js.map +0 -1
- package/dist/types/primitives/Text.js +0 -22
- package/dist/types/primitives/Text.js.map +0 -1
- package/dist/types/primitives/attachments/Attachment.js +0 -16
- package/dist/types/primitives/attachments/Attachment.js.map +0 -1
- package/dist/types/primitives/attachments/Photo.js +0 -16
- package/dist/types/primitives/attachments/Photo.js.map +0 -1
- package/dist/types/primitives/attachments/Signature.js +0 -16
- package/dist/types/primitives/attachments/Signature.js.map +0 -1
- package/dist/types/primitives/index.js +0 -85
- package/dist/types/primitives/index.js.map +0 -1
|
@@ -1,1135 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.TEXT_SUBTYPES = exports.ATTACHMENT_MEDIA_TYPES = void 0;
|
|
37
|
-
exports.parseParameterValueProvider = parseParameterValueProvider;
|
|
38
|
-
exports.parser = parser;
|
|
39
|
-
exports.applyVariableMetadata = applyVariableMetadata;
|
|
40
|
-
exports.parseJsonVariable = parseJsonVariable;
|
|
41
|
-
exports.parseJsonField = parseJsonField;
|
|
42
|
-
exports.jsonParser = jsonParser;
|
|
43
|
-
const SchemaObjectDisplayExpression_1 = require("../expressions/SchemaObjectDisplayExpression");
|
|
44
|
-
const parser_type_system_1 = require("@ja-platform/parser-type-system");
|
|
45
|
-
const QueryType_1 = require("../types/QueryType");
|
|
46
|
-
// # schema module
|
|
47
|
-
// Parser for v2 and v3 of the schema XML.
|
|
48
|
-
const parser_expressions_1 = require("@ja-platform/parser-expressions");
|
|
49
|
-
const xml = __importStar(require("@journeyapps/common-xml"));
|
|
50
|
-
const FunctionType_1 = require("../types/FunctionType");
|
|
51
|
-
const parser_type_system_2 = require("@ja-platform/parser-type-system");
|
|
52
|
-
const primitives_1 = require("../types/primitives");
|
|
53
|
-
const SchemaParserErrors_1 = require("./SchemaParserErrors");
|
|
54
|
-
const reservedNames_1 = require("./reservedNames");
|
|
55
|
-
const ModelIndex_1 = require("./ModelIndex");
|
|
56
|
-
/**
|
|
57
|
-
* Normalize serialized parameter providers once at the schema boundary.
|
|
58
|
-
*/
|
|
59
|
-
function parseParameterValueProvider(source) {
|
|
60
|
-
const code = source.trim().replace(/^\$:/, '');
|
|
61
|
-
const call = /^[\w$]+(?:\.[\w$]+)*$/.test(code) ? `${code}()` : code;
|
|
62
|
-
return parser_expressions_1.FunctionExpression.parse(`${parser_expressions_1.FUNCTION_PREFIX}${call}`);
|
|
63
|
-
}
|
|
64
|
-
function parseElement(element, definitions, errorHandler) {
|
|
65
|
-
const result = xml.parseElement(element, definitions);
|
|
66
|
-
if (result.errors.length > 0) {
|
|
67
|
-
errorHandler.pushErrors(result.errors);
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
const specs = xml.attribute.optionList([
|
|
72
|
-
'text',
|
|
73
|
-
'text.email',
|
|
74
|
-
'text.address',
|
|
75
|
-
'text.name',
|
|
76
|
-
'text.url',
|
|
77
|
-
'text.paragraph',
|
|
78
|
-
'number',
|
|
79
|
-
'number.za_id',
|
|
80
|
-
'phone',
|
|
81
|
-
'phone.za_cell',
|
|
82
|
-
'decimal',
|
|
83
|
-
'password'
|
|
84
|
-
], 'Invalid spec');
|
|
85
|
-
exports.ATTACHMENT_MEDIA_TYPES = [
|
|
86
|
-
'image/png',
|
|
87
|
-
'image/jpeg',
|
|
88
|
-
'image/svg+xml',
|
|
89
|
-
'application/pdf',
|
|
90
|
-
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
91
|
-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
92
|
-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
93
|
-
'application/vnd.ms-excel',
|
|
94
|
-
'application/msword',
|
|
95
|
-
'application/vnd.anritsu',
|
|
96
|
-
'application/vnd.valvelink',
|
|
97
|
-
'application/xml',
|
|
98
|
-
'text/plain',
|
|
99
|
-
'text/csv',
|
|
100
|
-
'any'
|
|
101
|
-
];
|
|
102
|
-
const v2AttributeDef = {
|
|
103
|
-
name: xml.attribute.name,
|
|
104
|
-
type: xml.attribute.notBlank,
|
|
105
|
-
spec: specs,
|
|
106
|
-
media: xml.attribute.optionList(exports.ATTACHMENT_MEDIA_TYPES, "media must be 'any', or one of the specific allowed mime types."),
|
|
107
|
-
minValue: xml.attribute.notBlank,
|
|
108
|
-
maxValue: xml.attribute.notBlank,
|
|
109
|
-
label: xml.attribute.label,
|
|
110
|
-
_required: ['name', 'type', 'label']
|
|
111
|
-
};
|
|
112
|
-
const deprecationWarning = {
|
|
113
|
-
warning: 'Attribute is deprecated. Please remove or replace it.'
|
|
114
|
-
};
|
|
115
|
-
const v3AttributeDef = {
|
|
116
|
-
name: xml.attribute.name,
|
|
117
|
-
type: xml.attribute.notBlank,
|
|
118
|
-
media: xml.attribute.optionList(exports.ATTACHMENT_MEDIA_TYPES, "media must be 'any', or one of the specific allowed mime types."),
|
|
119
|
-
'auto-download': xml.attribute.optionList('true', 'false'),
|
|
120
|
-
label: xml.attribute.label,
|
|
121
|
-
_required: ['name', 'type', 'label'],
|
|
122
|
-
'deprecated-spec': deprecationWarning,
|
|
123
|
-
minValue: deprecationWarning,
|
|
124
|
-
maxValue: deprecationWarning,
|
|
125
|
-
spec: { warning: "'spec' is deprecated. Please remove or replace it." }
|
|
126
|
-
};
|
|
127
|
-
const v2VariableDef = {
|
|
128
|
-
name: xml.attribute.name,
|
|
129
|
-
type: xml.attribute.notBlank,
|
|
130
|
-
spec: specs,
|
|
131
|
-
media: xml.attribute.optionList(exports.ATTACHMENT_MEDIA_TYPES, "media must be 'any', or one of the specific allowed mime types."),
|
|
132
|
-
minValue: xml.attribute.notBlank,
|
|
133
|
-
maxValue: xml.attribute.notBlank,
|
|
134
|
-
array: xml.attribute.optionList(['true', 'false']),
|
|
135
|
-
_required: ['name', 'type']
|
|
136
|
-
};
|
|
137
|
-
const v3VariableDef = {
|
|
138
|
-
name: xml.attribute.name,
|
|
139
|
-
type: xml.attribute.notBlank,
|
|
140
|
-
media: xml.attribute.optionList(exports.ATTACHMENT_MEDIA_TYPES, "media must be 'any', or one of the specific allowed mime types."),
|
|
141
|
-
minValue: deprecationWarning,
|
|
142
|
-
maxValue: deprecationWarning,
|
|
143
|
-
_required: ['name', 'type']
|
|
144
|
-
};
|
|
145
|
-
const v5ParamArgDef = {
|
|
146
|
-
name: xml.attribute.name,
|
|
147
|
-
type: xml.attribute.notBlank
|
|
148
|
-
};
|
|
149
|
-
const v3ParameterDef = {
|
|
150
|
-
name: xml.attribute.name,
|
|
151
|
-
type: xml.attribute.notBlank,
|
|
152
|
-
media: xml.attribute.optionList(exports.ATTACHMENT_MEDIA_TYPES, "media must be 'any', or one of the specific allowed mime types."),
|
|
153
|
-
required: xml.attribute.optionList(['true', 'false']),
|
|
154
|
-
'transform-value': xml.attribute.optionListWithFunctions([], parser_expressions_1.FUNCTION_PREFIX),
|
|
155
|
-
_required: ['name', 'type']
|
|
156
|
-
};
|
|
157
|
-
const v2PrimitiveMapping = {
|
|
158
|
-
enum_set: primitives_1.PrimitiveType.MULTIPLE_CHOICE_INTEGER,
|
|
159
|
-
enum: primitives_1.PrimitiveType.SINGLE_CHOICE_INTEGER,
|
|
160
|
-
int: primitives_1.PrimitiveType.INTEGER,
|
|
161
|
-
string: primitives_1.PrimitiveType.TEXT,
|
|
162
|
-
attachment: primitives_1.PrimitiveType.ATTACHMENT,
|
|
163
|
-
location: primitives_1.PrimitiveType.LOCATION,
|
|
164
|
-
decimal: primitives_1.PrimitiveType.NUMBER,
|
|
165
|
-
date: primitives_1.PrimitiveType.DATE,
|
|
166
|
-
datetime: primitives_1.PrimitiveType.DATETIME
|
|
167
|
-
};
|
|
168
|
-
const v3SpecMapping = {
|
|
169
|
-
email: 'text.email',
|
|
170
|
-
address: 'text.address',
|
|
171
|
-
name: 'text.name',
|
|
172
|
-
url: 'text.url',
|
|
173
|
-
paragraph: 'text.paragraph',
|
|
174
|
-
'signed-number': 'number',
|
|
175
|
-
number: 'number', // TODO: fix
|
|
176
|
-
'south-african-id': 'number.za_id',
|
|
177
|
-
'phone-number': 'phone',
|
|
178
|
-
'decimal-number': 'decimal',
|
|
179
|
-
password: 'password'
|
|
180
|
-
};
|
|
181
|
-
exports.TEXT_SUBTYPES = Object.keys(v3SpecMapping);
|
|
182
|
-
const getAttribute = xml.getAttribute;
|
|
183
|
-
function parser(schema, options) {
|
|
184
|
-
const errorHandler = new SchemaParserErrors_1.SchemaParserErrors();
|
|
185
|
-
options = options || {};
|
|
186
|
-
let tag;
|
|
187
|
-
let description;
|
|
188
|
-
const version = options.version || { v3: false, v3_1: false };
|
|
189
|
-
const v3 = version.v3 === true;
|
|
190
|
-
const v3p1 = version.v3_1 === true;
|
|
191
|
-
const variableDef = v3 ? v3VariableDef : v2VariableDef;
|
|
192
|
-
const parameterDef = v3ParameterDef;
|
|
193
|
-
const attributeDef = v3 ? v3AttributeDef : v2AttributeDef;
|
|
194
|
-
let optionDef;
|
|
195
|
-
if (!v3) {
|
|
196
|
-
tag = {
|
|
197
|
-
root: 'schema',
|
|
198
|
-
model: 'object',
|
|
199
|
-
relatedModel: 'type',
|
|
200
|
-
inverseOf: 'inverse_of',
|
|
201
|
-
belongsTo: 'belongs_to',
|
|
202
|
-
hasMany: 'has_many',
|
|
203
|
-
field: 'attribute',
|
|
204
|
-
optionKey: 'value'
|
|
205
|
-
};
|
|
206
|
-
description = {
|
|
207
|
-
field: 'Attribute'
|
|
208
|
-
};
|
|
209
|
-
optionDef = { option: {} };
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
tag = {
|
|
213
|
-
root: 'data-model',
|
|
214
|
-
model: 'model',
|
|
215
|
-
relatedModel: 'model',
|
|
216
|
-
inverseOf: 'inverse-of',
|
|
217
|
-
belongsTo: 'belongs-to',
|
|
218
|
-
hasMany: 'has-many',
|
|
219
|
-
field: 'field',
|
|
220
|
-
optionKey: 'key'
|
|
221
|
-
};
|
|
222
|
-
description = {
|
|
223
|
-
field: 'Field'
|
|
224
|
-
};
|
|
225
|
-
optionDef = { option: { key: xml.attribute.notBlank } };
|
|
226
|
-
if (!v3p1) {
|
|
227
|
-
tag.model = 'object';
|
|
228
|
-
tag.relatedModel = 'type';
|
|
229
|
-
optionDef = { option: { value: xml.attribute.notBlank } };
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
function recordSource(on, source) {
|
|
233
|
-
if (options.recordSource && on != null) {
|
|
234
|
-
on.sourceElement = source;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function parse(rawxml) {
|
|
238
|
-
const xmlDoc = xml.parse(rawxml);
|
|
239
|
-
schema.errors = xmlDoc.errors || [];
|
|
240
|
-
errorHandler.pushErrors(schema.errors);
|
|
241
|
-
if (schema.errors.length > 0) {
|
|
242
|
-
// Stop parsing right here, and keep the previous version we parsed (if any).
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
const root = xmlDoc.documentElement;
|
|
246
|
-
recordSource(schema, root);
|
|
247
|
-
if (root == null) {
|
|
248
|
-
errorHandler.pushError({ element: null, message: 'Not a valid XML document' });
|
|
249
|
-
return; // fatal error
|
|
250
|
-
}
|
|
251
|
-
if (root.tagName != tag.root) {
|
|
252
|
-
errorHandler.pushError({ element: root, message: `<${tag.root}> root tag expected` });
|
|
253
|
-
return; // fatal error
|
|
254
|
-
}
|
|
255
|
-
// Validate that the only children are <object>
|
|
256
|
-
let rootChildren = {
|
|
257
|
-
[tag.model]: 1
|
|
258
|
-
};
|
|
259
|
-
errorHandler.pushErrors(xml.validateChildren(root, rootChildren));
|
|
260
|
-
const objects = xml.children(root, tag.model);
|
|
261
|
-
if (objects.length === 0) {
|
|
262
|
-
errorHandler.pushError({ element: root, message: 'At least one model is required' });
|
|
263
|
-
}
|
|
264
|
-
schema.objects = {};
|
|
265
|
-
// First pass - load the objects with attributes and display names
|
|
266
|
-
objects.forEach(function (element) {
|
|
267
|
-
const object = parseObjectType(element);
|
|
268
|
-
if (object.name != null) {
|
|
269
|
-
if (object.name in schema.objects) {
|
|
270
|
-
const errorElement = xml.attributeNode(element, 'name') || element;
|
|
271
|
-
errorHandler.pushError({ element: errorElement, message: "Model '" + object.name + "' is already defined" });
|
|
272
|
-
errorHandler.pushErrors(object.errors);
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
schema.objects[object.name] = object;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
errorHandler.pushErrors(object.errors);
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
// Second pass - load the belongs_to relationships
|
|
283
|
-
objects.forEach(function (element) {
|
|
284
|
-
const name = getAttribute(element, 'name');
|
|
285
|
-
const object = schema.objects[name];
|
|
286
|
-
if (object) {
|
|
287
|
-
parseBelongsTo(object, element);
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
// Third pass - load the has_many relationships and indexes
|
|
291
|
-
objects.forEach(function (element) {
|
|
292
|
-
const name = getAttribute(element, 'name');
|
|
293
|
-
const object = schema.objects[name];
|
|
294
|
-
if (object) {
|
|
295
|
-
parseHasMany(object, element);
|
|
296
|
-
parseIndexes(object, element);
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
// Fourth pass - display and webhooks
|
|
300
|
-
objects.forEach(function (element) {
|
|
301
|
-
const name = getAttribute(element, 'name');
|
|
302
|
-
const object = schema.objects[name];
|
|
303
|
-
if (object) {
|
|
304
|
-
parseDisplay(object, element);
|
|
305
|
-
parseWebhooks(object, element);
|
|
306
|
-
parseNotifications(object, element);
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
function parseObjectType(element) {
|
|
311
|
-
const object = schema.newObjectType();
|
|
312
|
-
recordSource(object, element);
|
|
313
|
-
const modelDef = {
|
|
314
|
-
[tag.model]: {
|
|
315
|
-
name: xml.attribute.name,
|
|
316
|
-
label: xml.attribute.label,
|
|
317
|
-
classification: xml.attribute.optionList(['standard', 'sensitive']),
|
|
318
|
-
_required: ['name', 'label']
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
parseElement(element, modelDef, errorHandler);
|
|
322
|
-
object.name = getAttribute(element, 'name');
|
|
323
|
-
object.label = getAttribute(element, 'label');
|
|
324
|
-
const validation = (0, reservedNames_1.validateModelName)(object.name);
|
|
325
|
-
// Special case here: if name === null, we don't want to tell the user it's
|
|
326
|
-
// a reserved name, since it's not specified at all.
|
|
327
|
-
if (validation && object.name !== null) {
|
|
328
|
-
errorHandler.pushError({
|
|
329
|
-
element: xml.attributeNode(element, 'name'),
|
|
330
|
-
message: validation == 'error'
|
|
331
|
-
? `Reserved model name '${object.name}'`
|
|
332
|
-
: `'${object.name}' is reserved in some contexts and may cause issues.`,
|
|
333
|
-
type: validation
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
// the ordering of the elements (elements can share a number and be interchangeable)
|
|
337
|
-
const allowedChildren = {
|
|
338
|
-
[tag.field]: 1,
|
|
339
|
-
[tag.belongsTo]: 2,
|
|
340
|
-
[tag.hasMany]: 2,
|
|
341
|
-
index: 3,
|
|
342
|
-
display: 3,
|
|
343
|
-
'notify-user': 3,
|
|
344
|
-
webhook: 3
|
|
345
|
-
};
|
|
346
|
-
const orderMessage = 'Elements must be in this order: ' + tag.field + 's, relationships, display, notify-user, indices, webhooks';
|
|
347
|
-
const errors = xml.validateChildren(element, allowedChildren, orderMessage);
|
|
348
|
-
errorHandler.pushErrors(errors);
|
|
349
|
-
// Processes model attributes
|
|
350
|
-
const attributes = xml.children(element, tag.field);
|
|
351
|
-
attributes.forEach(function (attributeElement) {
|
|
352
|
-
const variable = parseField(attributeElement, false);
|
|
353
|
-
if (object.attributes.hasOwnProperty(variable.name)) {
|
|
354
|
-
const errorElement = xml.attributeNode(attributeElement, 'name') || attributeElement;
|
|
355
|
-
errorHandler.pushError({
|
|
356
|
-
element: errorElement,
|
|
357
|
-
message: description.field + " '" + variable.name + "' is already defined"
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
else {
|
|
361
|
-
object.addAttribute(variable);
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
return object;
|
|
365
|
-
}
|
|
366
|
-
function parseDisplay(object, element) {
|
|
367
|
-
const displayElements = xml.children(element, 'display');
|
|
368
|
-
if (displayElements.length == 1) {
|
|
369
|
-
const displayElement = displayElements[0];
|
|
370
|
-
parseElement(displayElement, {
|
|
371
|
-
display: {
|
|
372
|
-
format: xml.attribute.label
|
|
373
|
-
}
|
|
374
|
-
}, errorHandler);
|
|
375
|
-
let attribute = true;
|
|
376
|
-
let fmtString = getAttribute(displayElement, 'format');
|
|
377
|
-
if (fmtString == null) {
|
|
378
|
-
fmtString = displayElement.textContent;
|
|
379
|
-
attribute = false;
|
|
380
|
-
}
|
|
381
|
-
object.displayFormat = SchemaObjectDisplayExpression_1.SchemaObjectDisplayExpression.parse(fmtString);
|
|
382
|
-
if (options.recordSource) {
|
|
383
|
-
object.displaySource = displayElement;
|
|
384
|
-
}
|
|
385
|
-
const displayErrors = object.displayFormat.validate({
|
|
386
|
-
getReference(path) {
|
|
387
|
-
let type = object;
|
|
388
|
-
for (const field of path) {
|
|
389
|
-
type = parser_type_system_2.ObjectType.isInstanceOf(type) ? type.getField(String(field)) : null;
|
|
390
|
-
}
|
|
391
|
-
return type;
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
displayErrors.forEach(function (error) {
|
|
395
|
-
if (attribute) {
|
|
396
|
-
errorHandler.pushError({
|
|
397
|
-
element: xml.attributeValuePosition(displayElement, 'format', error.token.start, error.token.end),
|
|
398
|
-
message: error.message
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
errorHandler.pushError({
|
|
403
|
-
element: xml.elementTextPosition(displayElement, error.token.start, error.token.end),
|
|
404
|
-
message: error.message
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
else if (displayElements.length === 0) {
|
|
410
|
-
errorHandler.pushError({ element, message: '<display> is required' });
|
|
411
|
-
object.displayFormat = SchemaObjectDisplayExpression_1.SchemaObjectDisplayExpression.parse('');
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
errorHandler.pushError({
|
|
415
|
-
element: displayElements[1],
|
|
416
|
-
message: 'Only one <display> element is allowed',
|
|
417
|
-
type: 'warning'
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
return object;
|
|
421
|
-
}
|
|
422
|
-
function parseBelongsTo(object, element) {
|
|
423
|
-
const syntax = {
|
|
424
|
-
[tag.belongsTo]: {
|
|
425
|
-
name: xml.attribute.name,
|
|
426
|
-
[tag.relatedModel]: xml.attribute.notBlank,
|
|
427
|
-
_required: [tag.relatedModel]
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
xml.children(element, tag.belongsTo).forEach(function (e) {
|
|
431
|
-
parseElement(e, syntax, errorHandler);
|
|
432
|
-
try {
|
|
433
|
-
const typeName = getAttribute(e, tag.relatedModel);
|
|
434
|
-
let name = getAttribute(e, 'name');
|
|
435
|
-
if (name == null || name === '') {
|
|
436
|
-
name = typeName;
|
|
437
|
-
}
|
|
438
|
-
object.addBelongsTo({ name, type: typeName, schema });
|
|
439
|
-
recordSource(object.belongsToVars[name], e);
|
|
440
|
-
}
|
|
441
|
-
catch (err) {
|
|
442
|
-
const node = xml.attributeNode(e, err.attribute) || e;
|
|
443
|
-
errorHandler.pushError({ element: node, message: err.message });
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
function parseHasMany(object, element) {
|
|
448
|
-
const syntax = {
|
|
449
|
-
[tag.hasMany]: {
|
|
450
|
-
name: xml.attribute.name,
|
|
451
|
-
[tag.inverseOf]: xml.attribute.notBlank,
|
|
452
|
-
[tag.relatedModel]: xml.attribute.notBlank,
|
|
453
|
-
_required: ['name', tag.relatedModel]
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
xml.children(element, tag.hasMany).forEach(function (e) {
|
|
457
|
-
parseElement(e, syntax, errorHandler);
|
|
458
|
-
const typeName = getAttribute(e, tag.relatedModel);
|
|
459
|
-
const name = getAttribute(e, 'name');
|
|
460
|
-
const inverseOf = getAttribute(e, tag.inverseOf);
|
|
461
|
-
if (typeName != null) {
|
|
462
|
-
const objectType = schema.objects[typeName];
|
|
463
|
-
if (objectType == null) {
|
|
464
|
-
errorHandler.pushError({
|
|
465
|
-
element: xml.attributeNode(e, tag.relatedModel) || e,
|
|
466
|
-
message: "Object '" + typeName + "' is not defined"
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
let rel = null;
|
|
471
|
-
if (inverseOf != null) {
|
|
472
|
-
rel = objectType.belongsTo[inverseOf];
|
|
473
|
-
if (rel == null) {
|
|
474
|
-
errorHandler.pushError({
|
|
475
|
-
element: xml.attributeNode(e, tag.inverseOf),
|
|
476
|
-
message: "'" + inverseOf + "' is not defined on '" + objectType.name + "'"
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
else {
|
|
481
|
-
const candidates = [];
|
|
482
|
-
for (let key in objectType.belongsTo) {
|
|
483
|
-
if (objectType.belongsTo.hasOwnProperty(key)) {
|
|
484
|
-
const r = objectType.belongsTo[key];
|
|
485
|
-
if (r.foreignType === object) {
|
|
486
|
-
candidates.push(r);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
if (candidates.length == 1) {
|
|
491
|
-
rel = candidates[0];
|
|
492
|
-
}
|
|
493
|
-
else if (candidates.length === 0) {
|
|
494
|
-
errorHandler.pushError({
|
|
495
|
-
element: e,
|
|
496
|
-
message: 'Need <' +
|
|
497
|
-
tag.belongsTo +
|
|
498
|
-
' ' +
|
|
499
|
-
tag.relatedModel +
|
|
500
|
-
'="' +
|
|
501
|
-
object.name +
|
|
502
|
-
'"> in object \'' +
|
|
503
|
-
objectType.name +
|
|
504
|
-
"'"
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
else {
|
|
508
|
-
const names = [];
|
|
509
|
-
for (let i = 0; i < candidates.length; i++) {
|
|
510
|
-
names.push(candidates[i].name);
|
|
511
|
-
}
|
|
512
|
-
errorHandler.pushError({
|
|
513
|
-
element: e,
|
|
514
|
-
message: 'Ambiguous ' + e.tagName + ' - set ' + tag.inverseOf + ' to one of ' + names.join(', ')
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
if (rel != null) {
|
|
519
|
-
if (object.hasMany.hasOwnProperty(name)) {
|
|
520
|
-
errorHandler.pushError({
|
|
521
|
-
element: xml.attributeNode(e, 'name') || e,
|
|
522
|
-
message: "Relationship '" + name + "' is already defined"
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
else if (rel.foreignName != null) {
|
|
526
|
-
errorHandler.pushError({
|
|
527
|
-
element: e,
|
|
528
|
-
message: "Relationship '" + rel.name + "' already has a " + e.tagName + " '" + rel.foreignName + "'"
|
|
529
|
-
});
|
|
530
|
-
}
|
|
531
|
-
else {
|
|
532
|
-
rel.foreignName = name;
|
|
533
|
-
object.hasMany[name] = rel;
|
|
534
|
-
object.hasManyVars[name] = schema.variable(name, new QueryType_1.QueryType(objectType));
|
|
535
|
-
recordSource(object.hasManyVars[name], e);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
function parseWebhooks(object, element) {
|
|
543
|
-
const syntax = {
|
|
544
|
-
webhook: {
|
|
545
|
-
name: xml.attribute.name,
|
|
546
|
-
type: xml.attribute.notBlank,
|
|
547
|
-
receiver: xml.attribute.any, // pdfmailer|cloudcode|<none>
|
|
548
|
-
action: xml.attribute.any, // template |task |<n/a>
|
|
549
|
-
// Fixme: this looks wrong.
|
|
550
|
-
field: {
|
|
551
|
-
name: xml.attribute.name,
|
|
552
|
-
required: xml.attribute.notBlank,
|
|
553
|
-
embed: xml.attribute.notBlank,
|
|
554
|
-
state: xml.attribute.notBlank,
|
|
555
|
-
_required: ['name']
|
|
556
|
-
},
|
|
557
|
-
_required: ['type']
|
|
558
|
-
}
|
|
559
|
-
};
|
|
560
|
-
xml.children(element, 'webhook').forEach(function (webhookElement) {
|
|
561
|
-
parseElement(webhookElement, syntax, errorHandler);
|
|
562
|
-
let webhook = {
|
|
563
|
-
name: getAttribute(webhookElement, 'name'),
|
|
564
|
-
type: getAttribute(webhookElement, 'type'),
|
|
565
|
-
receiver: getAttribute(webhookElement, 'receiver'),
|
|
566
|
-
action: getAttribute(webhookElement, 'action'),
|
|
567
|
-
fields: [],
|
|
568
|
-
sourceElement: null
|
|
569
|
-
};
|
|
570
|
-
recordSource(webhook, webhookElement);
|
|
571
|
-
if (webhook.type != 'ready' && webhook.type != 'update') {
|
|
572
|
-
errorHandler.pushError({
|
|
573
|
-
element: xml.attributeNode(webhookElement, 'type') || webhookElement,
|
|
574
|
-
message: 'webhook type must be "ready" or "update"'
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
if (webhook.receiver == null) {
|
|
578
|
-
if (webhook.name == null) {
|
|
579
|
-
webhook.name = object.name + '_' + webhook.type;
|
|
580
|
-
}
|
|
581
|
-
if (webhook.action != null) {
|
|
582
|
-
errorHandler.pushError({
|
|
583
|
-
element: xml.attributeNode(webhookElement, 'action') || webhookElement,
|
|
584
|
-
message: 'webhook action is not allowed unless receiver is specified'
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
if (webhook.receiver != 'pdfmailer' && webhook.receiver != 'cloudcode') {
|
|
590
|
-
errorHandler.pushError({
|
|
591
|
-
element: xml.attributeNode(webhookElement, 'receiver') || webhookElement,
|
|
592
|
-
message: 'webhook receiver must be "cloudcode" or "pdfmailer" when specified'
|
|
593
|
-
});
|
|
594
|
-
}
|
|
595
|
-
if (webhook.action == null || webhook.action == '') {
|
|
596
|
-
errorHandler.pushError({
|
|
597
|
-
element: webhookElement,
|
|
598
|
-
message: 'webhook action required when receiver is specified'
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
|
-
if (webhook.name == null) {
|
|
602
|
-
// pdfmailer: pdfmailer_notification_ready_delivery_note
|
|
603
|
-
webhook.name = webhook.receiver + '_' + object.name + '_' + webhook.type + '_' + webhook.action;
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
const booleanMapping = {
|
|
607
|
-
true: true,
|
|
608
|
-
false: false,
|
|
609
|
-
null: true,
|
|
610
|
-
'': true
|
|
611
|
-
};
|
|
612
|
-
// iterate through all fields in webhook
|
|
613
|
-
xml.children(webhookElement, 'field').forEach(function (fieldElement) {
|
|
614
|
-
const fieldName = getAttribute(fieldElement, 'name');
|
|
615
|
-
// TODO: validate 'name'
|
|
616
|
-
const fieldRequiredText = getAttribute(fieldElement, 'required');
|
|
617
|
-
// 'required' defaults to true if not provided
|
|
618
|
-
const fieldRequired = booleanMapping[fieldRequiredText];
|
|
619
|
-
if (fieldRequired == null) {
|
|
620
|
-
errorHandler.pushError({
|
|
621
|
-
element: xml.attributeNode(fieldElement, 'required') || fieldElement,
|
|
622
|
-
message: 'required must be "true" or "false"'
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
const fieldEmbedText = getAttribute(fieldElement, 'embed');
|
|
626
|
-
// 'embed' defaults to true if not provided
|
|
627
|
-
const fieldEmbed = booleanMapping[fieldEmbedText];
|
|
628
|
-
if (fieldEmbed == null) {
|
|
629
|
-
errorHandler.pushError({
|
|
630
|
-
element: xml.attributeNode(fieldElement, 'embed') || fieldElement,
|
|
631
|
-
message: 'embed must be "true" or "false"'
|
|
632
|
-
});
|
|
633
|
-
}
|
|
634
|
-
let fieldState = getAttribute(fieldElement, 'state');
|
|
635
|
-
// 'state' defaults to 'present' if not provided
|
|
636
|
-
if (fieldState == null) {
|
|
637
|
-
fieldState = 'present';
|
|
638
|
-
}
|
|
639
|
-
if (fieldState != 'present' && fieldState != 'uploaded') {
|
|
640
|
-
errorHandler.pushError({
|
|
641
|
-
element: xml.attributeNode(fieldElement, 'state') || fieldElement,
|
|
642
|
-
message: 'state must be "present" or "uploaded"'
|
|
643
|
-
});
|
|
644
|
-
}
|
|
645
|
-
const field = {
|
|
646
|
-
name: fieldName,
|
|
647
|
-
required: fieldRequired,
|
|
648
|
-
embed: fieldEmbed,
|
|
649
|
-
state: fieldState
|
|
650
|
-
};
|
|
651
|
-
webhook.fields.push(field);
|
|
652
|
-
});
|
|
653
|
-
object.webhooks.push(webhook);
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
function parseNotifications(object, element) {
|
|
657
|
-
const syntax = {
|
|
658
|
-
'notify-user': {
|
|
659
|
-
message: xml.attribute.notBlank,
|
|
660
|
-
'received-field': xml.attribute.notBlank,
|
|
661
|
-
'recipient-field': xml.attribute.notBlank,
|
|
662
|
-
'badge-count-field': xml.attribute.notBlank
|
|
663
|
-
},
|
|
664
|
-
_required: ['message', 'received-field', 'recipient-field']
|
|
665
|
-
};
|
|
666
|
-
xml.children(element, 'notify-user').forEach(function (notificationElement) {
|
|
667
|
-
parseElement(notificationElement, syntax, errorHandler);
|
|
668
|
-
const notifyUserObject = {
|
|
669
|
-
message: SchemaObjectDisplayExpression_1.SchemaObjectDisplayExpression.parse(getAttribute(notificationElement, 'message')),
|
|
670
|
-
recipient: getAttribute(notificationElement, 'recipient-field'),
|
|
671
|
-
received: getAttribute(notificationElement, 'received-field'),
|
|
672
|
-
badgeCount: getAttribute(notificationElement, 'badge-count-field')
|
|
673
|
-
};
|
|
674
|
-
object.notifyUsers.push(notifyUserObject);
|
|
675
|
-
});
|
|
676
|
-
}
|
|
677
|
-
function parseIndexes(object, element) {
|
|
678
|
-
const indexDef = {
|
|
679
|
-
index: {
|
|
680
|
-
name: xml.attribute.name,
|
|
681
|
-
on: xml.attribute.notBlank,
|
|
682
|
-
database: xml.attribute.notBlank,
|
|
683
|
-
_required: ['on']
|
|
684
|
-
}
|
|
685
|
-
};
|
|
686
|
-
xml.children(element, 'index').forEach(function (e) {
|
|
687
|
-
parseElement(e, indexDef, errorHandler);
|
|
688
|
-
const onAttribute = getAttribute(e, 'on');
|
|
689
|
-
let name = getAttribute(e, 'name');
|
|
690
|
-
const on = onAttribute.split(',');
|
|
691
|
-
let keys = [];
|
|
692
|
-
for (let key of on) {
|
|
693
|
-
let dir;
|
|
694
|
-
if (key.startsWith('-')) {
|
|
695
|
-
key = key.substring(1);
|
|
696
|
-
dir = ModelIndex_1.IndexDirection.DESCENDING;
|
|
697
|
-
}
|
|
698
|
-
else {
|
|
699
|
-
dir = ModelIndex_1.IndexDirection.ASCENDING;
|
|
700
|
-
}
|
|
701
|
-
const fieldType = object.attributes[key] || object.belongsToVars[key];
|
|
702
|
-
if (!fieldType) {
|
|
703
|
-
errorHandler.pushError({
|
|
704
|
-
element: xml.attributeNode(e, 'on') || e,
|
|
705
|
-
message: "Undefined field or model in index: '" + key + "'"
|
|
706
|
-
});
|
|
707
|
-
}
|
|
708
|
-
else if (['attachment', 'multiple-choice', 'multiple-choice-integer'].indexOf(fieldType.type.name) >= 0) {
|
|
709
|
-
errorHandler.pushError({
|
|
710
|
-
element: xml.attributeNode(e, 'on') || e,
|
|
711
|
-
message: "Field is not indexable: '" + key + "'"
|
|
712
|
-
});
|
|
713
|
-
}
|
|
714
|
-
keys.push({
|
|
715
|
-
dir,
|
|
716
|
-
field: key
|
|
717
|
-
});
|
|
718
|
-
}
|
|
719
|
-
if (name == null) {
|
|
720
|
-
name = keys.map((key) => key.field).join('_');
|
|
721
|
-
}
|
|
722
|
-
const databaseAttribute = getAttribute(e, 'database');
|
|
723
|
-
let databases = [ModelIndex_1.IndexDatabase.APP, ModelIndex_1.IndexDatabase.CLOUD];
|
|
724
|
-
if (databaseAttribute) {
|
|
725
|
-
databases = databaseAttribute.split(',');
|
|
726
|
-
for (let db of databases) {
|
|
727
|
-
if ([ModelIndex_1.IndexDatabase.APP, ModelIndex_1.IndexDatabase.CLOUD].indexOf(db) == -1) {
|
|
728
|
-
errorHandler.pushError({ element: xml.attributeNode(e, 'database'), message: `Invalid database: ${db}` });
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
const index = {
|
|
733
|
-
on, // Deprecated, since this doesn't support IndexDirection.
|
|
734
|
-
keys,
|
|
735
|
-
name,
|
|
736
|
-
databases
|
|
737
|
-
};
|
|
738
|
-
object.allIndexes.push(index);
|
|
739
|
-
if (index.databases.indexOf(ModelIndex_1.IndexDatabase.APP) >= 0) {
|
|
740
|
-
object.indexes.push(index);
|
|
741
|
-
}
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
function parseIntegerOptions(type, element) {
|
|
745
|
-
if (!primitives_1.ChoiceType.isInstanceOf(type)) {
|
|
746
|
-
throw new Error('Integer options can only be used with choice types');
|
|
747
|
-
}
|
|
748
|
-
let index = 0;
|
|
749
|
-
let largest = -1;
|
|
750
|
-
xml.children(element, 'option').forEach(function (e) {
|
|
751
|
-
parseElement(e, optionDef, errorHandler);
|
|
752
|
-
const valueText = getAttribute(e, tag.optionKey);
|
|
753
|
-
const value = valueText == null ? largest + 1 : parseInt(valueText, 10);
|
|
754
|
-
const label = e.textContent;
|
|
755
|
-
try {
|
|
756
|
-
let enumOption = type.addOption(value, label, index);
|
|
757
|
-
recordSource(enumOption, e);
|
|
758
|
-
}
|
|
759
|
-
catch (err) {
|
|
760
|
-
errorHandler.pushError({ element: xml.attributeNode(e, tag.optionKey) || e, message: err.message });
|
|
761
|
-
}
|
|
762
|
-
index++;
|
|
763
|
-
largest = Math.max(largest, value);
|
|
764
|
-
});
|
|
765
|
-
}
|
|
766
|
-
function parseStringOptions(type, element) {
|
|
767
|
-
if (!primitives_1.ChoiceType.isInstanceOf(type)) {
|
|
768
|
-
throw new Error('String options can only be used with choice types');
|
|
769
|
-
}
|
|
770
|
-
let index = 0;
|
|
771
|
-
xml.children(element, 'option').forEach(function (e) {
|
|
772
|
-
parseElement(e, optionDef, errorHandler);
|
|
773
|
-
const value = getAttribute(e, tag.optionKey);
|
|
774
|
-
const label = e.textContent;
|
|
775
|
-
try {
|
|
776
|
-
let enumOption = type.addOption(value, label, index);
|
|
777
|
-
recordSource(enumOption, e);
|
|
778
|
-
index++;
|
|
779
|
-
}
|
|
780
|
-
catch (err) {
|
|
781
|
-
errorHandler.pushError({ element: xml.attributeNode(e, tag.optionKey) || e, message: err.message });
|
|
782
|
-
}
|
|
783
|
-
});
|
|
784
|
-
}
|
|
785
|
-
function parseBooleanOptions(type, element) {
|
|
786
|
-
if (!primitives_1.ChoiceType.isInstanceOf(type)) {
|
|
787
|
-
throw new Error('Boolean options can only be used with choice types');
|
|
788
|
-
}
|
|
789
|
-
let index = 0;
|
|
790
|
-
xml.children(element, 'option').forEach(function (e) {
|
|
791
|
-
parseElement(e, optionDef, errorHandler);
|
|
792
|
-
const valueText = getAttribute(e, tag.optionKey);
|
|
793
|
-
const value = { true: true, false: false }[valueText];
|
|
794
|
-
const label = e.textContent;
|
|
795
|
-
if (value != null) {
|
|
796
|
-
try {
|
|
797
|
-
let enumOption = type.addOption(value, label, index);
|
|
798
|
-
recordSource(enumOption, e);
|
|
799
|
-
index++;
|
|
800
|
-
}
|
|
801
|
-
catch (err) {
|
|
802
|
-
errorHandler.pushError({ element: xml.attributeNode(e, tag.optionKey) || e, message: err.message });
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
else {
|
|
806
|
-
errorHandler.pushError({
|
|
807
|
-
element: xml.attributeNode(e, tag.optionKey) || e,
|
|
808
|
-
message: 'key must be "true" or "false"'
|
|
809
|
-
});
|
|
810
|
-
}
|
|
811
|
-
});
|
|
812
|
-
if (Object.keys(type.options).length === 0) {
|
|
813
|
-
type.addOption(false, 'No', 0);
|
|
814
|
-
type.addOption(true, 'Yes', 1);
|
|
815
|
-
}
|
|
816
|
-
if (Object.keys(type.options).length != 2) {
|
|
817
|
-
errorHandler.pushError({ element, message: 'A boolean must contain exactly two options.' });
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Validate declaration attributes independently of resolving their type.
|
|
822
|
-
* A caller validating type expressions separately can omit the type attribute checks.
|
|
823
|
-
*/
|
|
824
|
-
function validateVariableAttributes(element, options = {}) {
|
|
825
|
-
const definition = element.tagName === 'param' ? parameterDef : variableDef;
|
|
826
|
-
parseElement(element, {
|
|
827
|
-
[element.tagName]: options.validateType === false
|
|
828
|
-
? Object.assign(Object.assign({}, definition), { type: xml.attribute.any, _required: definition._required.filter((name) => name !== 'type') }) : definition
|
|
829
|
-
}, errorHandler);
|
|
830
|
-
}
|
|
831
|
-
function parseField(element, isVariable) {
|
|
832
|
-
const isParam = element.tagName == 'param';
|
|
833
|
-
const variable = isParam ? schema.param() : schema.variable();
|
|
834
|
-
recordSource(variable, element);
|
|
835
|
-
if (isVariable) {
|
|
836
|
-
validateVariableAttributes(element);
|
|
837
|
-
}
|
|
838
|
-
else {
|
|
839
|
-
parseElement(element, {
|
|
840
|
-
attribute: attributeDef,
|
|
841
|
-
field: attributeDef
|
|
842
|
-
}, errorHandler);
|
|
843
|
-
}
|
|
844
|
-
variable.name = getAttribute(element, 'name');
|
|
845
|
-
variable.label = getAttribute(element, 'label');
|
|
846
|
-
if (isParam) {
|
|
847
|
-
const requiredAttr = getAttribute(element, 'required');
|
|
848
|
-
variable.required = requiredAttr != null ? requiredAttr !== 'false' : true;
|
|
849
|
-
const provider = getAttribute(element, 'transform-value');
|
|
850
|
-
if (provider != null) {
|
|
851
|
-
variable.provideValue = parseParameterValueProvider(provider);
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
const originalTypeName = getAttribute(element, 'type');
|
|
855
|
-
variable.sourceTypeName = originalTypeName; // Useful for debugging info
|
|
856
|
-
let typeName = originalTypeName;
|
|
857
|
-
let subType = null;
|
|
858
|
-
if (originalTypeName != null) {
|
|
859
|
-
const colon = originalTypeName.indexOf(':');
|
|
860
|
-
if (colon >= 0 && v3) {
|
|
861
|
-
typeName = originalTypeName.substring(0, colon);
|
|
862
|
-
subType = originalTypeName.substring(colon + 1);
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
if (!isVariable) {
|
|
866
|
-
const validation = (0, reservedNames_1.validateFieldName)(variable.name);
|
|
867
|
-
if (validation) {
|
|
868
|
-
errorHandler.pushError({
|
|
869
|
-
element: xml.attributeNode(element, 'name'),
|
|
870
|
-
message: `Reserved field name '${variable.name}'`,
|
|
871
|
-
type: validation
|
|
872
|
-
});
|
|
873
|
-
}
|
|
874
|
-
else if (variable.name != null && variable.name.slice(-3) === '_id') {
|
|
875
|
-
errorHandler.pushError({
|
|
876
|
-
element: xml.attributeNode(element, 'name'),
|
|
877
|
-
message: "Field name should not end with '_id'",
|
|
878
|
-
type: 'warning'
|
|
879
|
-
});
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
let query = false;
|
|
883
|
-
let array = false;
|
|
884
|
-
if (isVariable) {
|
|
885
|
-
if (v3 && typeName == 'query') {
|
|
886
|
-
query = true;
|
|
887
|
-
typeName = subType;
|
|
888
|
-
}
|
|
889
|
-
else if (v3 && typeName == 'array') {
|
|
890
|
-
array = true;
|
|
891
|
-
typeName = subType;
|
|
892
|
-
}
|
|
893
|
-
else if (!v3 && getAttribute(element, 'array') == 'true') {
|
|
894
|
-
query = true;
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
let primitiveType = null;
|
|
898
|
-
if (v3) {
|
|
899
|
-
if (!v3p1 && typeName == 'decimal') {
|
|
900
|
-
typeName = primitives_1.PrimitiveType.NUMBER;
|
|
901
|
-
}
|
|
902
|
-
primitiveType = schema.primitive(typeName);
|
|
903
|
-
}
|
|
904
|
-
else {
|
|
905
|
-
if (typeName in v2PrimitiveMapping) {
|
|
906
|
-
typeName = v2PrimitiveMapping[typeName];
|
|
907
|
-
primitiveType = schema.primitive(typeName);
|
|
908
|
-
}
|
|
909
|
-
else {
|
|
910
|
-
primitiveType = null;
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
if (typeName == primitives_1.PrimitiveType.SINGLE_CHOICE_INTEGER ||
|
|
914
|
-
typeName == primitives_1.PrimitiveType.MULTIPLE_CHOICE_INTEGER ||
|
|
915
|
-
typeName == primitives_1.PrimitiveType.SINGLE_CHOICE ||
|
|
916
|
-
typeName == primitives_1.PrimitiveType.MULTIPLE_CHOICE ||
|
|
917
|
-
typeName == primitives_1.PrimitiveType.BOOLEAN) {
|
|
918
|
-
errorHandler.pushErrors(xml.validateChildren(element, { option: 1 }));
|
|
919
|
-
}
|
|
920
|
-
else {
|
|
921
|
-
errorHandler.pushErrors(xml.validateChildren(element, { arg: 1 }));
|
|
922
|
-
}
|
|
923
|
-
if (primitiveType == null && isVariable) {
|
|
924
|
-
const objectType = schema.getType(typeName);
|
|
925
|
-
if (objectType != null) {
|
|
926
|
-
if (query) {
|
|
927
|
-
variable.type = new QueryType_1.QueryType(objectType);
|
|
928
|
-
}
|
|
929
|
-
else if (array) {
|
|
930
|
-
variable.type = new parser_type_system_1.ArrayType(objectType);
|
|
931
|
-
}
|
|
932
|
-
else {
|
|
933
|
-
variable.type = objectType;
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
else if (isParam && typeName == FunctionType_1.FunctionType.TYPE) {
|
|
937
|
-
const functionType = schema.functionType();
|
|
938
|
-
xml.children(element, FunctionType_1.FunctionType.ARG_TAG).forEach((child) => {
|
|
939
|
-
const result = parseElement(child, { arg: v5ParamArgDef }, errorHandler);
|
|
940
|
-
if (result.errors.length == 0) {
|
|
941
|
-
functionType.addArgument(schema.variable(getAttribute(child, 'name'), schema.getType(getAttribute(child, 'type'))));
|
|
942
|
-
}
|
|
943
|
-
});
|
|
944
|
-
variable.type = functionType;
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
else if (primitiveType != null) {
|
|
948
|
-
variable.type = primitiveType;
|
|
949
|
-
if (primitives_1.TextType.isInstanceOf(variable.type)) {
|
|
950
|
-
variable.type.spec = getAttribute(element, 'spec');
|
|
951
|
-
if (v3 && subType) {
|
|
952
|
-
variable.type.subType = subType;
|
|
953
|
-
// For backwards compatibility
|
|
954
|
-
variable.type.spec = v3SpecMapping[subType];
|
|
955
|
-
if (variable.type.spec == null) {
|
|
956
|
-
errorHandler.pushError({
|
|
957
|
-
element: xml.attributeNode(element, 'type'),
|
|
958
|
-
message: "'" + subType + "' is not a valid string format"
|
|
959
|
-
});
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
else if (primitives_1.DateType.isInstanceOf(variable.type)) {
|
|
964
|
-
variable.type.isDay = v3p1 ? true : false;
|
|
965
|
-
}
|
|
966
|
-
else if (typeName == 'single-choice-integer' || typeName == 'multiple-choice-integer') {
|
|
967
|
-
parseIntegerOptions(variable.type, element);
|
|
968
|
-
}
|
|
969
|
-
else if (typeName == 'single-choice' || typeName == 'multiple-choice') {
|
|
970
|
-
parseStringOptions(variable.type, element);
|
|
971
|
-
}
|
|
972
|
-
else if (typeName == 'boolean') {
|
|
973
|
-
parseBooleanOptions(variable.type, element);
|
|
974
|
-
}
|
|
975
|
-
else if (originalTypeName == 'attachment' && primitives_1.AttachmentType.isInstanceOf(variable.type)) {
|
|
976
|
-
variable.type.media = getAttribute(element, 'media');
|
|
977
|
-
if (variable.type.media == null) {
|
|
978
|
-
if (v3) {
|
|
979
|
-
errorHandler.pushError({
|
|
980
|
-
element,
|
|
981
|
-
message: 'Include media="" with a specific mime type, or media="any" for any of the allowed types.'
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
else {
|
|
985
|
-
errorHandler.pushError({
|
|
986
|
-
element,
|
|
987
|
-
message: 'Include media="image/svg+xml" (signature) or "image/jpeg" (picture)'
|
|
988
|
-
});
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
// parseElement validates the allowed values when the element is present
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
if (variable.type != null && primitives_1.AttachmentType.isInstanceOf(variable.type)) {
|
|
995
|
-
variable.type.autoDownload = getAttribute(element, 'auto-download') == 'true';
|
|
996
|
-
}
|
|
997
|
-
if (variable.type == null) {
|
|
998
|
-
if (xml.attributeNode(element, 'type') != null && typeName !== '') {
|
|
999
|
-
errorHandler.pushError({
|
|
1000
|
-
element: xml.attributeNode(element, 'type'),
|
|
1001
|
-
message: "Invalid type '" + originalTypeName + "'"
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
else {
|
|
1005
|
-
// This case is covered by parseElement().
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
return variable;
|
|
1009
|
-
}
|
|
1010
|
-
function reset() {
|
|
1011
|
-
errorHandler.reset();
|
|
1012
|
-
}
|
|
1013
|
-
function getErrors() {
|
|
1014
|
-
return errorHandler.getErrors();
|
|
1015
|
-
}
|
|
1016
|
-
return {
|
|
1017
|
-
parse,
|
|
1018
|
-
parseField,
|
|
1019
|
-
validateVariableAttributes,
|
|
1020
|
-
parseObjectType,
|
|
1021
|
-
parseBelongsTo,
|
|
1022
|
-
parseDisplay,
|
|
1023
|
-
reset,
|
|
1024
|
-
getErrors
|
|
1025
|
-
};
|
|
1026
|
-
}
|
|
1027
|
-
/**
|
|
1028
|
-
* Apply serialized field metadata to a variable whose type is already resolved.
|
|
1029
|
-
*/
|
|
1030
|
-
function applyVariableMetadata(variable, attributeData) {
|
|
1031
|
-
variable.label = attributeData.label;
|
|
1032
|
-
if (attributeData.spec && primitives_1.TextType.isInstanceOf(variable.type)) {
|
|
1033
|
-
variable.type.spec = attributeData.spec;
|
|
1034
|
-
}
|
|
1035
|
-
if (attributeData.subType && primitives_1.TextType.isInstanceOf(variable.type)) {
|
|
1036
|
-
variable.type.subType = attributeData.subType;
|
|
1037
|
-
}
|
|
1038
|
-
if (primitives_1.DateType.isInstanceOf(variable.type)) {
|
|
1039
|
-
variable.type.isDay = !!attributeData.isDay;
|
|
1040
|
-
}
|
|
1041
|
-
if (attributeData.options && primitives_1.ChoiceType.isInstanceOf(variable.type)) {
|
|
1042
|
-
for (let optionData of attributeData.options) {
|
|
1043
|
-
variable.type.addOption(optionData.value, optionData.label, optionData.index);
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
return variable;
|
|
1047
|
-
}
|
|
1048
|
-
function parseJsonVariable(schema, attributeName, attributeData) {
|
|
1049
|
-
if (attributeData.type == 'query') {
|
|
1050
|
-
return schema.variable(attributeName, new QueryType_1.QueryType(schema.getType(attributeData.object)));
|
|
1051
|
-
}
|
|
1052
|
-
else if (attributeData.type == 'array') {
|
|
1053
|
-
return schema.variable(attributeName, new parser_type_system_1.ArrayType(schema.getType(attributeData.object)));
|
|
1054
|
-
}
|
|
1055
|
-
else {
|
|
1056
|
-
const variable = schema.variable(attributeName, attributeData.type);
|
|
1057
|
-
if (attributeData.required != null) {
|
|
1058
|
-
variable.required = attributeData.required;
|
|
1059
|
-
}
|
|
1060
|
-
if (attributeData.provideValue != null) {
|
|
1061
|
-
variable.provideValue = parseParameterValueProvider(attributeData.provideValue);
|
|
1062
|
-
}
|
|
1063
|
-
return applyVariableMetadata(variable, attributeData);
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
// Schema-free variable parsing
|
|
1067
|
-
// Used for indexes
|
|
1068
|
-
function parseJsonField(schema, attributeData) {
|
|
1069
|
-
const type = schema.primitive(attributeData.type);
|
|
1070
|
-
let variable = schema.variable(attributeData.name, type);
|
|
1071
|
-
if (attributeData.relationship) {
|
|
1072
|
-
variable.relationship = attributeData.relationship;
|
|
1073
|
-
variable.isRelationshipId = attributeData.isRelationshipId;
|
|
1074
|
-
}
|
|
1075
|
-
return applyVariableMetadata(variable, attributeData);
|
|
1076
|
-
}
|
|
1077
|
-
function jsonParser(schema) {
|
|
1078
|
-
// Structure:
|
|
1079
|
-
// {
|
|
1080
|
-
// objects: {
|
|
1081
|
-
// "asset": {
|
|
1082
|
-
// attributes: {
|
|
1083
|
-
// "serial_number": {
|
|
1084
|
-
// type: "string",
|
|
1085
|
-
// spec: "number",
|
|
1086
|
-
// },
|
|
1087
|
-
// ...,
|
|
1088
|
-
// },
|
|
1089
|
-
// belongsTo: {
|
|
1090
|
-
// "owner": {
|
|
1091
|
-
// type: "person",
|
|
1092
|
-
// foreignName: "assets"
|
|
1093
|
-
// },
|
|
1094
|
-
// ...
|
|
1095
|
-
// },
|
|
1096
|
-
// display: "A {serial_number}",
|
|
1097
|
-
// },
|
|
1098
|
-
// ...
|
|
1099
|
-
// }
|
|
1100
|
-
// }
|
|
1101
|
-
function parse(data) {
|
|
1102
|
-
schema.objects = {};
|
|
1103
|
-
// First pass - load the objects with attributes and display names
|
|
1104
|
-
Object.keys(data.objects).forEach(function (name) {
|
|
1105
|
-
const objectData = data.objects[name];
|
|
1106
|
-
const object = schema.newObjectType();
|
|
1107
|
-
object.name = name;
|
|
1108
|
-
object.label = objectData.label;
|
|
1109
|
-
object.displayFormat = SchemaObjectDisplayExpression_1.SchemaObjectDisplayExpression.parse(objectData.display);
|
|
1110
|
-
Object.keys(objectData.attributes).forEach(function (attributeName) {
|
|
1111
|
-
const attributeData = objectData.attributes[attributeName];
|
|
1112
|
-
const variable = parseJsonVariable(schema, attributeName, attributeData);
|
|
1113
|
-
object.addAttribute(variable);
|
|
1114
|
-
});
|
|
1115
|
-
schema.objects[object.name] = object;
|
|
1116
|
-
});
|
|
1117
|
-
Object.keys(data.objects).forEach(function (name) {
|
|
1118
|
-
const objectData = data.objects[name];
|
|
1119
|
-
const object = schema.objects[name];
|
|
1120
|
-
Object.keys(objectData.belongsTo).forEach(function (relName) {
|
|
1121
|
-
const relData = objectData.belongsTo[relName];
|
|
1122
|
-
const rel = object.addBelongsTo({
|
|
1123
|
-
name: relName,
|
|
1124
|
-
type: relData.type,
|
|
1125
|
-
schema,
|
|
1126
|
-
foreignName: relData.foreignName
|
|
1127
|
-
});
|
|
1128
|
-
});
|
|
1129
|
-
});
|
|
1130
|
-
}
|
|
1131
|
-
return {
|
|
1132
|
-
parse
|
|
1133
|
-
};
|
|
1134
|
-
}
|
|
1135
|
-
//# sourceMappingURL=schemaParser.js.map
|