node-opcua-factory 2.73.1 → 2.76.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.
Files changed (69) hide show
  1. package/LICENSE +3 -1
  2. package/dist/base_ua_object.d.ts +53 -53
  3. package/dist/base_ua_object.js +535 -535
  4. package/dist/basic_type.d.ts +40 -40
  5. package/dist/basic_type.js +118 -118
  6. package/dist/builtin_types.d.ts +46 -46
  7. package/dist/builtin_types.js +283 -283
  8. package/dist/builtin_types_special.d.ts +5 -5
  9. package/dist/builtin_types_special.js +45 -45
  10. package/dist/datatype_factory.d.ts +36 -36
  11. package/dist/datatype_factory.js +307 -305
  12. package/dist/datatype_factory.js.map +1 -1
  13. package/dist/encode_decode.d.ts +9 -9
  14. package/dist/encode_decode.js +39 -39
  15. package/dist/enumerations.d.ts +32 -32
  16. package/dist/enumerations.js +77 -77
  17. package/dist/get_built_in_type.js +1 -1
  18. package/dist/get_standard_data_type_factory.d.ts +12 -12
  19. package/dist/get_standard_data_type_factory.js +34 -34
  20. package/dist/get_structured_type_schema.d.ts +4 -4
  21. package/dist/get_structured_type_schema.js +12 -12
  22. package/dist/id_generator.d.ts +3 -3
  23. package/dist/id_generator.js +21 -21
  24. package/dist/index.d.ts +18 -18
  25. package/dist/index.js +34 -34
  26. package/dist/nodeid_type.d.ts +13 -13
  27. package/dist/nodeid_type.js +18 -18
  28. package/dist/parameters.d.ts +3 -3
  29. package/dist/parameters.js +6 -6
  30. package/dist/register_class_definition.d.ts +3 -3
  31. package/dist/register_class_definition.js +8 -8
  32. package/dist/schema_helpers.d.ts +24 -24
  33. package/dist/schema_helpers.js +99 -99
  34. package/dist/structured_type_schema.d.ts +44 -44
  35. package/dist/structured_type_schema.js +278 -278
  36. package/dist/types.d.ts +151 -151
  37. package/dist/types.js +9 -9
  38. package/package.json +15 -12
  39. package/source/datatype_factory.ts +8 -5
  40. package/dist/constructor_type.d.ts +0 -15
  41. package/dist/constructor_type.js +0 -3
  42. package/dist/constructor_type.js.map +0 -1
  43. package/dist/factories_baseobject.d.ts +0 -56
  44. package/dist/factories_baseobject.js +0 -535
  45. package/dist/factories_baseobject.js.map +0 -1
  46. package/dist/factories_basic_type.d.ts +0 -40
  47. package/dist/factories_basic_type.js +0 -136
  48. package/dist/factories_basic_type.js.map +0 -1
  49. package/dist/factories_builtin_types.d.ts +0 -32
  50. package/dist/factories_builtin_types.js +0 -262
  51. package/dist/factories_builtin_types.js.map +0 -1
  52. package/dist/factories_builtin_types_special.d.ts +0 -5
  53. package/dist/factories_builtin_types_special.js +0 -46
  54. package/dist/factories_builtin_types_special.js.map +0 -1
  55. package/dist/factories_enumerations.d.ts +0 -31
  56. package/dist/factories_enumerations.js +0 -78
  57. package/dist/factories_enumerations.js.map +0 -1
  58. package/dist/factories_factories.d.ts +0 -17
  59. package/dist/factories_factories.js +0 -54
  60. package/dist/factories_factories.js.map +0 -1
  61. package/dist/factories_id_generator.d.ts +0 -3
  62. package/dist/factories_id_generator.js +0 -22
  63. package/dist/factories_id_generator.js.map +0 -1
  64. package/dist/factories_schema_helpers.d.ts +0 -27
  65. package/dist/factories_schema_helpers.js +0 -122
  66. package/dist/factories_schema_helpers.js.map +0 -1
  67. package/dist/factories_structuredTypeSchema.d.ts +0 -45
  68. package/dist/factories_structuredTypeSchema.js +0 -277
  69. package/dist/factories_structuredTypeSchema.js.map +0 -1
@@ -1,279 +1,279 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildStructuredType = exports.check_options_correctness_against_schema = exports.extract_all_fields = exports.get_base_schema = exports.StructuredTypeSchema = void 0;
4
- /**
5
- * @module node-opcua-factory
6
- */
7
- const chalk = require("chalk");
8
- const node_opcua_assert_1 = require("node-opcua-assert");
9
- const node_opcua_nodeid_1 = require("node-opcua-nodeid");
10
- const node_opcua_utils_1 = require("node-opcua-utils");
11
- const builtin_types_1 = require("./builtin_types");
12
- const enumerations_1 = require("./enumerations");
13
- const parameters_1 = require("./parameters");
14
- const get_standard_data_type_factory_1 = require("./get_standard_data_type_factory");
15
- const get_structured_type_schema_1 = require("./get_structured_type_schema");
16
- const types_1 = require("./types");
17
- function figureOutFieldCategory(field) {
18
- const fieldType = field.fieldType;
19
- if (field.category) {
20
- return field.category;
21
- }
22
- if ((0, enumerations_1.hasBuiltInEnumeration)(fieldType)) {
23
- return types_1.FieldCategory.enumeration;
24
- }
25
- else if ((0, builtin_types_1.hasBuiltInType)(fieldType)) {
26
- return types_1.FieldCategory.basic;
27
- }
28
- else if ((0, get_standard_data_type_factory_1.hasStructuredType)(fieldType)) {
29
- (0, node_opcua_assert_1.assert)(fieldType !== "LocalizedText"); // LocalizedText should be treated as BasicType!!!
30
- return types_1.FieldCategory.complex;
31
- }
32
- return types_1.FieldCategory.basic;
33
- }
34
- const regExp = /((ns[0-9]+:)?)(.*)/;
35
- function figureOutSchema(underConstructSchema, field, category) {
36
- if (field.schema) {
37
- return field.schema;
38
- }
39
- if (underConstructSchema.name === field.fieldType) {
40
- return underConstructSchema;
41
- }
42
- let returnValue = null;
43
- // may be the field.type contains a ns<X>: prefix !! like the one found in Beckhoff PLC !
44
- const m = field.fieldType.match(regExp);
45
- /* istanbul ignore next */
46
- if (!m) {
47
- throw new Error("malformed fieldType ? : " + field.fieldType);
48
- }
49
- const fieldTypeWithoutNS = m[3];
50
- switch (category) {
51
- case types_1.FieldCategory.complex:
52
- if ((0, get_standard_data_type_factory_1.hasStructuredType)(field.fieldType)) {
53
- returnValue = (0, get_structured_type_schema_1.getStructuredTypeSchema)(fieldTypeWithoutNS);
54
- }
55
- else {
56
- // LocalizedText etc ...
57
- returnValue = (0, builtin_types_1.getBuiltInType)(fieldTypeWithoutNS);
58
- }
59
- break;
60
- case types_1.FieldCategory.basic:
61
- returnValue = (0, builtin_types_1.getBuiltInType)(fieldTypeWithoutNS);
62
- if (!returnValue) {
63
- returnValue = (0, get_structured_type_schema_1.getStructuredTypeSchema)(fieldTypeWithoutNS);
64
- if (returnValue) {
65
- console.log("Why ?");
66
- }
67
- }
68
- break;
69
- case types_1.FieldCategory.enumeration:
70
- returnValue = (0, enumerations_1.getBuiltInEnumeration)(fieldTypeWithoutNS);
71
- break;
72
- }
73
- if (null === returnValue || undefined === returnValue) {
74
- try {
75
- returnValue = (0, enumerations_1.getBuiltInEnumeration)(fieldTypeWithoutNS);
76
- }
77
- catch (err) {
78
- console.log(err);
79
- }
80
- throw new Error("Cannot find Schema for field with name " +
81
- field.name +
82
- " fieldTypeWithoutNS= " +
83
- fieldTypeWithoutNS +
84
- " with type " +
85
- field.fieldType +
86
- " category = " +
87
- category +
88
- JSON.stringify(field, null, "\t"));
89
- }
90
- return returnValue;
91
- }
92
- function buildField(underConstructSchema, fieldLight) {
93
- const category = figureOutFieldCategory(fieldLight);
94
- const schema = figureOutSchema(underConstructSchema, fieldLight, category);
95
- /* istanbul ignore next */
96
- if (!schema) {
97
- throw new Error("expecting a valid schema for field with name " +
98
- fieldLight.name +
99
- " with type " +
100
- fieldLight.fieldType +
101
- " category" +
102
- category);
103
- }
104
- const { defaultValue, isArray, documentation, fieldType, switchBit, switchValue, allowSubType, dataType, basicDataType } = fieldLight;
105
- return {
106
- name: (0, node_opcua_utils_1.lowerFirstLetter)(fieldLight.name),
107
- originalName: fieldLight.name,
108
- category,
109
- defaultValue,
110
- isArray,
111
- documentation,
112
- fieldType,
113
- switchBit,
114
- switchValue,
115
- allowSubType,
116
- dataType,
117
- basicDataType,
118
- schema
119
- };
120
- }
121
- class StructuredTypeSchema extends builtin_types_1.TypeSchemaBase {
122
- constructor(options) {
123
- super(options);
124
- this.bitFields = options.bitFields;
125
- this.baseType = options.baseType;
126
- this.category = types_1.FieldCategory.complex;
127
- if ((0, builtin_types_1.hasBuiltInType)(options.name)) {
128
- this.category = types_1.FieldCategory.basic;
129
- }
130
- this.fields = options.fields.map(buildField.bind(null, this));
131
- this.id = new node_opcua_nodeid_1.NodeId();
132
- this.dataTypeNodeId = new node_opcua_nodeid_1.NodeId();
133
- this._possibleFields = this.fields.map((field) => field.name);
134
- this._baseSchema = null;
135
- }
136
- toString() {
137
- var _a, _b, _c;
138
- const str = [];
139
- str.push("name = " + this.name);
140
- str.push("baseType = " + this.baseType);
141
- str.push("id = " + this.id.toString());
142
- str.push("bitFields = " + (this.bitFields ? this.bitFields.map((b) => b.name).join(" ") : undefined));
143
- str.push("dataTypeNodeId = " + (this.dataTypeNodeId ? this.dataTypeNodeId.toString() : undefined));
144
- str.push("documentation = " + this.documentation);
145
- str.push("encodingDefaultBinary = " + ((_a = this.encodingDefaultBinary) === null || _a === void 0 ? void 0 : _a.toString()));
146
- str.push("encodingDefaultXml = " + ((_b = this.encodingDefaultXml) === null || _b === void 0 ? void 0 : _b.toString()));
147
- str.push("encodingDefaultJson = " + ((_c = this.encodingDefaultJson) === null || _c === void 0 ? void 0 : _c.toString()));
148
- for (const f of this.fields) {
149
- str.push(" field = " +
150
- f.name.padEnd(30) +
151
- " isArray= " +
152
- (f.isArray ? true : false) +
153
- " " +
154
- f.fieldType.toString().padEnd(30) +
155
- (f.switchBit !== undefined ? " switchBit " + f.switchBit : "") +
156
- (f.switchValue !== undefined ? " switchValue " + f.switchValue : ""));
157
- }
158
- return str.join("\n");
159
- }
160
- }
161
- exports.StructuredTypeSchema = StructuredTypeSchema;
162
- /**
163
- *
164
- * @method get_base_schema
165
- * @param schema
166
- * @return {*}
167
- *
168
- */
169
- function get_base_schema(schema) {
170
- let baseSchema = schema._baseSchema;
171
- if (baseSchema) {
172
- return baseSchema;
173
- }
174
- if (schema.baseType === "ExtensionObject" || schema.baseType === "DataTypeDefinition") {
175
- return null;
176
- }
177
- if (schema.baseType === "Union") {
178
- return null;
179
- }
180
- if (schema.baseType && schema.baseType !== "BaseUAObject" && schema.baseType !== "DataTypeDefinition") {
181
- if (!(0, get_standard_data_type_factory_1.hasStructuredType)(schema.baseType)) {
182
- return null;
183
- }
184
- const baseType = (0, get_standard_data_type_factory_1.getStructureTypeConstructor)(schema.baseType);
185
- // istanbul ignore next
186
- if (!baseType) {
187
- throw new Error(" cannot find factory for " + schema.baseType);
188
- }
189
- if (baseType.prototype.schema) {
190
- baseSchema = baseType.prototype.schema;
191
- }
192
- }
193
- // put in cache for speedup
194
- schema._baseSchema = baseSchema;
195
- return baseSchema;
196
- }
197
- exports.get_base_schema = get_base_schema;
198
- /**
199
- * extract a list of all possible fields for a schema
200
- * (by walking up the inheritance chain)
201
- *
202
- */
203
- function extract_all_fields(schema) {
204
- // returns cached result if any
205
- // istanbul ignore next
206
- if (schema._possibleFields) {
207
- return schema._possibleFields;
208
- }
209
- // extract the possible fields from the schema.
210
- let possibleFields = schema.fields.map((field) => field.name);
211
- const baseSchema = get_base_schema(schema);
212
- // istanbul ignore next
213
- if (baseSchema) {
214
- const fields = extract_all_fields(baseSchema);
215
- possibleFields = fields.concat(possibleFields);
216
- }
217
- // put in cache to speed up
218
- schema._possibleFields = possibleFields;
219
- return possibleFields;
220
- }
221
- exports.extract_all_fields = extract_all_fields;
222
- /**
223
- * check correctness of option fields against scheme
224
- *
225
- * @method check_options_correctness_against_schema
226
- *
227
- */
228
- function check_options_correctness_against_schema(obj, schema, options) {
229
- if (!parameters_1.parameters.debugSchemaHelper) {
230
- return true; // ignoring set
231
- }
232
- options = options || {};
233
- // istanbul ignore next
234
- if (!(options !== null && typeof options === "object") && !(typeof options === "object")) {
235
- let message = chalk.red(" Invalid options specified while trying to construct a ") + " " + chalk.yellow(schema.name);
236
- message += "\n";
237
- message += chalk.red(" expecting a ") + chalk.yellow(" Object ");
238
- message += "\n";
239
- message += chalk.red(" and got a ") + chalk.yellow(typeof options) + chalk.red(" instead ");
240
- throw new Error(message);
241
- }
242
- // istanbul ignore next
243
- if (options instanceof obj.constructor) {
244
- return true;
245
- }
246
- // extract the possible fields from the schema.
247
- const possibleFields = obj.constructor.possibleFields || schema._possibleFields;
248
- // extracts the fields exposed by the option object
249
- const currentFields = Object.keys(options);
250
- // get a list of field that are in the 'options' object but not in schema
251
- // https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore
252
- function difference(a1, a2) {
253
- return [a1, a2].reduce((a, b) => a.filter((value) => !b.includes(value)));
254
- }
255
- const invalidOptionsFields = difference(currentFields, possibleFields);
256
- /* istanbul ignore next */
257
- if (invalidOptionsFields.length > 0) {
258
- // tslint:disable:no-console
259
- console.log("expected schema", schema.name);
260
- console.log(chalk.yellow("possible fields= "), possibleFields.sort().join(" "));
261
- console.log(chalk.red("current fields= "), currentFields.sort().join(" "));
262
- console.log(chalk.cyan("invalid_options_fields= "), invalidOptionsFields.sort().join(" "));
263
- console.log("options = ", options);
264
- }
265
- /* istanbul ignore next */
266
- if (invalidOptionsFields.length !== 0) {
267
- // tslint:disable:no-console
268
- console.log(chalk.yellow("possible fields= "), possibleFields.sort().join(" "));
269
- console.log(chalk.red("current fields= "), currentFields.sort().join(" "));
270
- throw new Error(" invalid field found in option :" + JSON.stringify(invalidOptionsFields));
271
- }
272
- return true;
273
- }
274
- exports.check_options_correctness_against_schema = check_options_correctness_against_schema;
275
- function buildStructuredType(schemaLight) {
276
- return new StructuredTypeSchema(schemaLight);
277
- }
278
- exports.buildStructuredType = buildStructuredType;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildStructuredType = exports.check_options_correctness_against_schema = exports.extract_all_fields = exports.get_base_schema = exports.StructuredTypeSchema = void 0;
4
+ /**
5
+ * @module node-opcua-factory
6
+ */
7
+ const chalk = require("chalk");
8
+ const node_opcua_assert_1 = require("node-opcua-assert");
9
+ const node_opcua_nodeid_1 = require("node-opcua-nodeid");
10
+ const node_opcua_utils_1 = require("node-opcua-utils");
11
+ const builtin_types_1 = require("./builtin_types");
12
+ const enumerations_1 = require("./enumerations");
13
+ const parameters_1 = require("./parameters");
14
+ const get_standard_data_type_factory_1 = require("./get_standard_data_type_factory");
15
+ const get_structured_type_schema_1 = require("./get_structured_type_schema");
16
+ const types_1 = require("./types");
17
+ function figureOutFieldCategory(field) {
18
+ const fieldType = field.fieldType;
19
+ if (field.category) {
20
+ return field.category;
21
+ }
22
+ if ((0, enumerations_1.hasBuiltInEnumeration)(fieldType)) {
23
+ return types_1.FieldCategory.enumeration;
24
+ }
25
+ else if ((0, builtin_types_1.hasBuiltInType)(fieldType)) {
26
+ return types_1.FieldCategory.basic;
27
+ }
28
+ else if ((0, get_standard_data_type_factory_1.hasStructuredType)(fieldType)) {
29
+ (0, node_opcua_assert_1.assert)(fieldType !== "LocalizedText"); // LocalizedText should be treated as BasicType!!!
30
+ return types_1.FieldCategory.complex;
31
+ }
32
+ return types_1.FieldCategory.basic;
33
+ }
34
+ const regExp = /((ns[0-9]+:)?)(.*)/;
35
+ function figureOutSchema(underConstructSchema, field, category) {
36
+ if (field.schema) {
37
+ return field.schema;
38
+ }
39
+ if (underConstructSchema.name === field.fieldType) {
40
+ return underConstructSchema;
41
+ }
42
+ let returnValue = null;
43
+ // may be the field.type contains a ns<X>: prefix !! like the one found in Beckhoff PLC !
44
+ const m = field.fieldType.match(regExp);
45
+ /* istanbul ignore next */
46
+ if (!m) {
47
+ throw new Error("malformed fieldType ? : " + field.fieldType);
48
+ }
49
+ const fieldTypeWithoutNS = m[3];
50
+ switch (category) {
51
+ case types_1.FieldCategory.complex:
52
+ if ((0, get_standard_data_type_factory_1.hasStructuredType)(field.fieldType)) {
53
+ returnValue = (0, get_structured_type_schema_1.getStructuredTypeSchema)(fieldTypeWithoutNS);
54
+ }
55
+ else {
56
+ // LocalizedText etc ...
57
+ returnValue = (0, builtin_types_1.getBuiltInType)(fieldTypeWithoutNS);
58
+ }
59
+ break;
60
+ case types_1.FieldCategory.basic:
61
+ returnValue = (0, builtin_types_1.getBuiltInType)(fieldTypeWithoutNS);
62
+ if (!returnValue) {
63
+ returnValue = (0, get_structured_type_schema_1.getStructuredTypeSchema)(fieldTypeWithoutNS);
64
+ if (returnValue) {
65
+ console.log("Why ?");
66
+ }
67
+ }
68
+ break;
69
+ case types_1.FieldCategory.enumeration:
70
+ returnValue = (0, enumerations_1.getBuiltInEnumeration)(fieldTypeWithoutNS);
71
+ break;
72
+ }
73
+ if (null === returnValue || undefined === returnValue) {
74
+ try {
75
+ returnValue = (0, enumerations_1.getBuiltInEnumeration)(fieldTypeWithoutNS);
76
+ }
77
+ catch (err) {
78
+ console.log(err);
79
+ }
80
+ throw new Error("Cannot find Schema for field with name " +
81
+ field.name +
82
+ " fieldTypeWithoutNS= " +
83
+ fieldTypeWithoutNS +
84
+ " with type " +
85
+ field.fieldType +
86
+ " category = " +
87
+ category +
88
+ JSON.stringify(field, null, "\t"));
89
+ }
90
+ return returnValue;
91
+ }
92
+ function buildField(underConstructSchema, fieldLight) {
93
+ const category = figureOutFieldCategory(fieldLight);
94
+ const schema = figureOutSchema(underConstructSchema, fieldLight, category);
95
+ /* istanbul ignore next */
96
+ if (!schema) {
97
+ throw new Error("expecting a valid schema for field with name " +
98
+ fieldLight.name +
99
+ " with type " +
100
+ fieldLight.fieldType +
101
+ " category" +
102
+ category);
103
+ }
104
+ const { defaultValue, isArray, documentation, fieldType, switchBit, switchValue, allowSubType, dataType, basicDataType } = fieldLight;
105
+ return {
106
+ name: (0, node_opcua_utils_1.lowerFirstLetter)(fieldLight.name),
107
+ originalName: fieldLight.name,
108
+ category,
109
+ defaultValue,
110
+ isArray,
111
+ documentation,
112
+ fieldType,
113
+ switchBit,
114
+ switchValue,
115
+ allowSubType,
116
+ dataType,
117
+ basicDataType,
118
+ schema
119
+ };
120
+ }
121
+ class StructuredTypeSchema extends builtin_types_1.TypeSchemaBase {
122
+ constructor(options) {
123
+ super(options);
124
+ this.bitFields = options.bitFields;
125
+ this.baseType = options.baseType;
126
+ this.category = types_1.FieldCategory.complex;
127
+ if ((0, builtin_types_1.hasBuiltInType)(options.name)) {
128
+ this.category = types_1.FieldCategory.basic;
129
+ }
130
+ this.fields = options.fields.map(buildField.bind(null, this));
131
+ this.id = new node_opcua_nodeid_1.NodeId();
132
+ this.dataTypeNodeId = new node_opcua_nodeid_1.NodeId();
133
+ this._possibleFields = this.fields.map((field) => field.name);
134
+ this._baseSchema = null;
135
+ }
136
+ toString() {
137
+ var _a, _b, _c;
138
+ const str = [];
139
+ str.push("name = " + this.name);
140
+ str.push("baseType = " + this.baseType);
141
+ str.push("id = " + this.id.toString());
142
+ str.push("bitFields = " + (this.bitFields ? this.bitFields.map((b) => b.name).join(" ") : undefined));
143
+ str.push("dataTypeNodeId = " + (this.dataTypeNodeId ? this.dataTypeNodeId.toString() : undefined));
144
+ str.push("documentation = " + this.documentation);
145
+ str.push("encodingDefaultBinary = " + ((_a = this.encodingDefaultBinary) === null || _a === void 0 ? void 0 : _a.toString()));
146
+ str.push("encodingDefaultXml = " + ((_b = this.encodingDefaultXml) === null || _b === void 0 ? void 0 : _b.toString()));
147
+ str.push("encodingDefaultJson = " + ((_c = this.encodingDefaultJson) === null || _c === void 0 ? void 0 : _c.toString()));
148
+ for (const f of this.fields) {
149
+ str.push(" field = " +
150
+ f.name.padEnd(30) +
151
+ " isArray= " +
152
+ (f.isArray ? true : false) +
153
+ " " +
154
+ f.fieldType.toString().padEnd(30) +
155
+ (f.switchBit !== undefined ? " switchBit " + f.switchBit : "") +
156
+ (f.switchValue !== undefined ? " switchValue " + f.switchValue : ""));
157
+ }
158
+ return str.join("\n");
159
+ }
160
+ }
161
+ exports.StructuredTypeSchema = StructuredTypeSchema;
162
+ /**
163
+ *
164
+ * @method get_base_schema
165
+ * @param schema
166
+ * @return {*}
167
+ *
168
+ */
169
+ function get_base_schema(schema) {
170
+ let baseSchema = schema._baseSchema;
171
+ if (baseSchema) {
172
+ return baseSchema;
173
+ }
174
+ if (schema.baseType === "ExtensionObject" || schema.baseType === "DataTypeDefinition") {
175
+ return null;
176
+ }
177
+ if (schema.baseType === "Union") {
178
+ return null;
179
+ }
180
+ if (schema.baseType && schema.baseType !== "BaseUAObject" && schema.baseType !== "DataTypeDefinition") {
181
+ if (!(0, get_standard_data_type_factory_1.hasStructuredType)(schema.baseType)) {
182
+ return null;
183
+ }
184
+ const baseType = (0, get_standard_data_type_factory_1.getStructureTypeConstructor)(schema.baseType);
185
+ // istanbul ignore next
186
+ if (!baseType) {
187
+ throw new Error(" cannot find factory for " + schema.baseType);
188
+ }
189
+ if (baseType.prototype.schema) {
190
+ baseSchema = baseType.prototype.schema;
191
+ }
192
+ }
193
+ // put in cache for speedup
194
+ schema._baseSchema = baseSchema;
195
+ return baseSchema;
196
+ }
197
+ exports.get_base_schema = get_base_schema;
198
+ /**
199
+ * extract a list of all possible fields for a schema
200
+ * (by walking up the inheritance chain)
201
+ *
202
+ */
203
+ function extract_all_fields(schema) {
204
+ // returns cached result if any
205
+ // istanbul ignore next
206
+ if (schema._possibleFields) {
207
+ return schema._possibleFields;
208
+ }
209
+ // extract the possible fields from the schema.
210
+ let possibleFields = schema.fields.map((field) => field.name);
211
+ const baseSchema = get_base_schema(schema);
212
+ // istanbul ignore next
213
+ if (baseSchema) {
214
+ const fields = extract_all_fields(baseSchema);
215
+ possibleFields = fields.concat(possibleFields);
216
+ }
217
+ // put in cache to speed up
218
+ schema._possibleFields = possibleFields;
219
+ return possibleFields;
220
+ }
221
+ exports.extract_all_fields = extract_all_fields;
222
+ /**
223
+ * check correctness of option fields against scheme
224
+ *
225
+ * @method check_options_correctness_against_schema
226
+ *
227
+ */
228
+ function check_options_correctness_against_schema(obj, schema, options) {
229
+ if (!parameters_1.parameters.debugSchemaHelper) {
230
+ return true; // ignoring set
231
+ }
232
+ options = options || {};
233
+ // istanbul ignore next
234
+ if (!(options !== null && typeof options === "object") && !(typeof options === "object")) {
235
+ let message = chalk.red(" Invalid options specified while trying to construct a ") + " " + chalk.yellow(schema.name);
236
+ message += "\n";
237
+ message += chalk.red(" expecting a ") + chalk.yellow(" Object ");
238
+ message += "\n";
239
+ message += chalk.red(" and got a ") + chalk.yellow(typeof options) + chalk.red(" instead ");
240
+ throw new Error(message);
241
+ }
242
+ // istanbul ignore next
243
+ if (options instanceof obj.constructor) {
244
+ return true;
245
+ }
246
+ // extract the possible fields from the schema.
247
+ const possibleFields = obj.constructor.possibleFields || schema._possibleFields;
248
+ // extracts the fields exposed by the option object
249
+ const currentFields = Object.keys(options);
250
+ // get a list of field that are in the 'options' object but not in schema
251
+ // https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore
252
+ function difference(a1, a2) {
253
+ return [a1, a2].reduce((a, b) => a.filter((value) => !b.includes(value)));
254
+ }
255
+ const invalidOptionsFields = difference(currentFields, possibleFields);
256
+ /* istanbul ignore next */
257
+ if (invalidOptionsFields.length > 0) {
258
+ // tslint:disable:no-console
259
+ console.log("expected schema", schema.name);
260
+ console.log(chalk.yellow("possible fields= "), possibleFields.sort().join(" "));
261
+ console.log(chalk.red("current fields= "), currentFields.sort().join(" "));
262
+ console.log(chalk.cyan("invalid_options_fields= "), invalidOptionsFields.sort().join(" "));
263
+ console.log("options = ", options);
264
+ }
265
+ /* istanbul ignore next */
266
+ if (invalidOptionsFields.length !== 0) {
267
+ // tslint:disable:no-console
268
+ console.log(chalk.yellow("possible fields= "), possibleFields.sort().join(" "));
269
+ console.log(chalk.red("current fields= "), currentFields.sort().join(" "));
270
+ throw new Error(" invalid field found in option :" + JSON.stringify(invalidOptionsFields));
271
+ }
272
+ return true;
273
+ }
274
+ exports.check_options_correctness_against_schema = check_options_correctness_against_schema;
275
+ function buildStructuredType(schemaLight) {
276
+ return new StructuredTypeSchema(schemaLight);
277
+ }
278
+ exports.buildStructuredType = buildStructuredType;
279
279
  //# sourceMappingURL=structured_type_schema.js.map