@rootintf/json-msg 0.2.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ name: npm-json-msg
2
+ title: npm-json-msg
3
+ version: "0.2"
4
+ nav:
5
+ - modules/ROOT/nav.adoc
@@ -0,0 +1 @@
1
+ // * xref:index.adoc[]
@@ -0,0 +1,76 @@
1
+ = npm-json-msg
2
+
3
+ Protocols: xref:protocols::JSON_Msg.adoc[]
4
+
5
+ [cols="24h,~"]
6
+ |===
7
+ |npm-json-msg
8
+ |https://gitlab.com/interop-protocols/npm-json-msg.git[^]
9
+ |===
10
+
11
+ == `JSON_Msg`
12
+
13
+ .JSON Schema, Draft-07
14
+ https://json-schema.org/specification-links.html#draft-7[^]
15
+ [source,json-schema/draft-07]
16
+ ----
17
+ {
18
+ "$id": "validate_Msg",
19
+ "$schema": "http://json-schema.org/draft-07/schema#",
20
+ "type" : "object",
21
+ "required" : ["id", "referer", "dt"],
22
+ "properties" : {
23
+ "id" : { "type" : "string"},
24
+ "referer" : {
25
+ "type" : "array",
26
+ "minItems" : 2,
27
+ "maxItems": 2,
28
+ "items": { "type" : "string" }
29
+ },
30
+ "dt" : { "type" : "string" }
31
+ }
32
+ }
33
+ ----
34
+
35
+ * *referer* [_sic_] --
36
+ ** `[0]` -- package, product or service name.
37
+ ** `[1]` -- instance specifier.
38
+
39
+ == `JSON_Msg_msg`
40
+
41
+ [source,json-schema/draft-07]
42
+ ----
43
+ {
44
+ "$id": "validate_Msg_msg",
45
+ "$schema": "http://json-schema.org/draft-07/schema#",
46
+ "type" : "object",
47
+ "required" : ["_msg"]
48
+
49
+ // ... JSON_Msg format
50
+ }
51
+ ----
52
+
53
+ * *_msg* -- original message this message is in regards to.
54
+
55
+ == `JSON_MsgAck`
56
+
57
+ [source,json-schema/draft-07]
58
+ ----
59
+ {
60
+ "$id": "validate_MsgAck",
61
+ "$schema": "http://json-schema.org/draft-07/schema#",
62
+ "type" : "object",
63
+ "required" : ["ack"],
64
+ "properties" : {
65
+ "ack" : {
66
+ "type" : "object",
67
+ "required" : ["ok"],
68
+ "properties" : {
69
+ "ok" : { "type" : "boolean" }
70
+ }
71
+ }
72
+ }
73
+
74
+ // ... JSON_Msg_msg format
75
+ }
76
+ ----
@@ -1,72 +1,61 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
3
+ const schema_Msg = validate18;function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};const schema_Msg_msg = validate19;function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};const schema_MsgAck_msg = validate20;function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
4
4
 
5
- const validate_Msg = validate10;function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){let data1 = data.referer;const _errs3 = errors;{if(Array.isArray(data1)){if(data1.length > 2){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/maxItems",keyword:"maxItems",params:{limit: 2},message:"must NOT have more than 2 items"}];return false;}else {if(data1.length < 2){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/minItems",keyword:"minItems",params:{limit: 2},message:"must NOT have fewer than 2 items"}];return false;}else {var valid1 = true;const len0 = data1.length;for(let i0=0; i0<len0; i0++){const _errs5 = errors;if(typeof data1[i0] !== "string"){validate10.errors = [{instancePath:instancePath+"/referer/" + i0,schemaPath:"#/properties/referer/items/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid1 = _errs5 === errors;if(!valid1){break;}}}}}else {validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "array"},message:"must be array"}];return false;}}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs7 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs7 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}const validate_Msg_msg = validate11;function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}const validate_MsgAck = validate12;function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
5
+ const byteToHex = [];
6
+ for (let i = 0; i < 256; ++i) {
7
+ byteToHex.push((i + 0x100).toString(16).slice(1));
8
+ }
9
+ function unsafeStringify(arr, offset = 0) {
10
+ return (byteToHex[arr[offset + 0]] +
11
+ byteToHex[arr[offset + 1]] +
12
+ byteToHex[arr[offset + 2]] +
13
+ byteToHex[arr[offset + 3]] +
14
+ '-' +
15
+ byteToHex[arr[offset + 4]] +
16
+ byteToHex[arr[offset + 5]] +
17
+ '-' +
18
+ byteToHex[arr[offset + 6]] +
19
+ byteToHex[arr[offset + 7]] +
20
+ '-' +
21
+ byteToHex[arr[offset + 8]] +
22
+ byteToHex[arr[offset + 9]] +
23
+ '-' +
24
+ byteToHex[arr[offset + 10]] +
25
+ byteToHex[arr[offset + 11]] +
26
+ byteToHex[arr[offset + 12]] +
27
+ byteToHex[arr[offset + 13]] +
28
+ byteToHex[arr[offset + 14]] +
29
+ byteToHex[arr[offset + 15]]).toLowerCase();
30
+ }
6
31
 
7
- // Unique ID creation requires a high quality random # generator. In the browser we therefore
8
- // require the crypto API and do not support built-in fallback to lower quality random number
9
- // generators (like Math.random()).
10
32
  let getRandomValues;
11
33
  const rnds8 = new Uint8Array(16);
12
34
  function rng() {
13
- // lazy load so that environments that need to polyfill have a chance to do so
14
- if (!getRandomValues) {
15
- // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
16
- getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
17
-
18
35
  if (!getRandomValues) {
19
- throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
36
+ if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
37
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
38
+ }
39
+ getRandomValues = crypto.getRandomValues.bind(crypto);
20
40
  }
21
- }
22
-
23
- return getRandomValues(rnds8);
24
- }
25
-
26
- /**
27
- * Convert array of 16 byte values to UUID string format of the form:
28
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
29
- */
30
-
31
- const byteToHex = [];
32
-
33
- for (let i = 0; i < 256; ++i) {
34
- byteToHex.push((i + 0x100).toString(16).slice(1));
35
- }
36
-
37
- function unsafeStringify(arr, offset = 0) {
38
- // Note: Be careful editing this code! It's been tuned for performance
39
- // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
40
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
41
+ return getRandomValues(rnds8);
41
42
  }
42
43
 
43
44
  const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
44
- var native = {
45
- randomUUID
46
- };
45
+ var native = { randomUUID };
47
46
 
48
47
  function v4(options, buf, offset) {
49
- if (native.randomUUID && !buf && !options) {
50
- return native.randomUUID();
51
- }
52
-
53
- options = options || {};
54
- const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
55
-
56
- rnds[6] = rnds[6] & 0x0f | 0x40;
57
- rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
58
-
59
- if (buf) {
60
- offset = offset || 0;
61
-
62
- for (let i = 0; i < 16; ++i) {
63
- buf[offset + i] = rnds[i];
48
+ if (native.randomUUID && true && !options) {
49
+ return native.randomUUID();
64
50
  }
65
-
66
- return buf;
67
- }
68
-
69
- return unsafeStringify(rnds);
51
+ options = options || {};
52
+ const rnds = options.random ?? options.rng?.() ?? rng();
53
+ if (rnds.length < 16) {
54
+ throw new Error('Random bytes length must be >= 16');
55
+ }
56
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
57
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
58
+ return unsafeStringify(rnds);
70
59
  }
71
60
 
72
61
  // console.log('OUT', __name({variableName}) );
@@ -154,25 +143,19 @@ const l_= {
154
143
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
155
144
  var JSON_Msg = (function() {
156
145
  return {
157
- merge : function(json) {
158
- const dt_Msg= new Date();
159
- let json_msg= {
160
- id : v4(),
161
- referer : null,
162
- dt : dt_Msg.toISOString(),
163
- };
164
- // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
165
- for (var key of Object.keys(json)) {
166
- json_msg[key]= json[key];
167
- }
168
- return json_msg;
146
+ merge: function(json) {
147
+ return Object.assign({
148
+ id: v4(),
149
+ referer: null,
150
+ dt: new Date().toISOString()
151
+ }, json || {});
169
152
  },
170
- validate : function(json) {
171
- const b_valid= validate_Msg(json);
172
- if (!b_valid)
173
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors);
153
+ validate : function (json) {
154
+ const b_valid = schema_Msg(json);
155
+ if (!b_valid)
156
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
174
157
  return b_valid;
175
- }
158
+ },
176
159
  // TODO: requires a separate ajv module require, and JTD specific schema
177
160
  // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
178
161
  // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
@@ -181,26 +164,24 @@ var JSON_Msg = (function() {
181
164
  // }
182
165
  }
183
166
  })();
184
-
167
+
185
168
  var JSON_Msg_msg = (function() {
186
169
  return {
187
- create_with_msg_merge : function(_msg, json) {
188
- let json_msg= JSON_Msg.merge(json);
189
- json_msg._msg= _msg;
190
- return json_msg;
170
+ create_with_msg_merge: function(_msg, json) {
171
+ return Object.assign(JSON_Msg.merge(json), { _msg });
191
172
  },
192
173
  validate : function(json) {
193
174
  if (! JSON_Msg.validate(json))
194
175
  return false;
195
- const b_valid= validate_Msg_msg(json);
176
+ const b_valid= schema_Msg_msg(json);
196
177
  if (!b_valid)
197
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors);
178
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
198
179
  return b_valid;
199
180
  }
200
181
  }
201
182
  })();
202
183
 
203
- var JSON_MsgAck = (function() {
184
+ var JSON_MsgAck_msg = (function() {
204
185
  return {
205
186
  create_with_msgOk : function(_msg, b_ok= true) {
206
187
  return JSON_Msg_msg.create_with_msg_merge(_msg, {
@@ -220,14 +201,14 @@ var JSON_MsgAck = (function() {
220
201
  validate : function(json) {
221
202
  if (! JSON_Msg_msg.validate(json))
222
203
  return false;
223
- const b_valid= validate_MsgAck(json);
204
+ const b_valid= schema_MsgAck_msg(json);
224
205
  if (!b_valid)
225
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors);
206
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
226
207
  return b_valid;
227
208
  }
228
209
  }
229
210
  })();
230
211
 
231
212
  exports.JSON_Msg = JSON_Msg;
232
- exports.JSON_MsgAck = JSON_MsgAck;
213
+ exports.JSON_MsgAck_msg = JSON_MsgAck_msg;
233
214
  exports.JSON_Msg_msg = JSON_Msg_msg;
@@ -1,68 +1,59 @@
1
- const validate_Msg = validate10;function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){let data1 = data.referer;const _errs3 = errors;{if(Array.isArray(data1)){if(data1.length > 2){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/maxItems",keyword:"maxItems",params:{limit: 2},message:"must NOT have more than 2 items"}];return false;}else {if(data1.length < 2){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/minItems",keyword:"minItems",params:{limit: 2},message:"must NOT have fewer than 2 items"}];return false;}else {var valid1 = true;const len0 = data1.length;for(let i0=0; i0<len0; i0++){const _errs5 = errors;if(typeof data1[i0] !== "string"){validate10.errors = [{instancePath:instancePath+"/referer/" + i0,schemaPath:"#/properties/referer/items/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid1 = _errs5 === errors;if(!valid1){break;}}}}}else {validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "array"},message:"must be array"}];return false;}}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs7 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs7 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}const validate_Msg_msg = validate11;function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}const validate_MsgAck = validate12;function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
1
+ const schema_Msg = validate18;function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};const schema_Msg_msg = validate19;function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};const schema_MsgAck_msg = validate20;function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
2
+
3
+ const byteToHex = [];
4
+ for (let i = 0; i < 256; ++i) {
5
+ byteToHex.push((i + 0x100).toString(16).slice(1));
6
+ }
7
+ function unsafeStringify(arr, offset = 0) {
8
+ return (byteToHex[arr[offset + 0]] +
9
+ byteToHex[arr[offset + 1]] +
10
+ byteToHex[arr[offset + 2]] +
11
+ byteToHex[arr[offset + 3]] +
12
+ '-' +
13
+ byteToHex[arr[offset + 4]] +
14
+ byteToHex[arr[offset + 5]] +
15
+ '-' +
16
+ byteToHex[arr[offset + 6]] +
17
+ byteToHex[arr[offset + 7]] +
18
+ '-' +
19
+ byteToHex[arr[offset + 8]] +
20
+ byteToHex[arr[offset + 9]] +
21
+ '-' +
22
+ byteToHex[arr[offset + 10]] +
23
+ byteToHex[arr[offset + 11]] +
24
+ byteToHex[arr[offset + 12]] +
25
+ byteToHex[arr[offset + 13]] +
26
+ byteToHex[arr[offset + 14]] +
27
+ byteToHex[arr[offset + 15]]).toLowerCase();
28
+ }
2
29
 
3
- // Unique ID creation requires a high quality random # generator. In the browser we therefore
4
- // require the crypto API and do not support built-in fallback to lower quality random number
5
- // generators (like Math.random()).
6
30
  let getRandomValues;
7
31
  const rnds8 = new Uint8Array(16);
8
32
  function rng() {
9
- // lazy load so that environments that need to polyfill have a chance to do so
10
- if (!getRandomValues) {
11
- // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
12
- getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
13
-
14
33
  if (!getRandomValues) {
15
- throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
34
+ if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
35
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
36
+ }
37
+ getRandomValues = crypto.getRandomValues.bind(crypto);
16
38
  }
17
- }
18
-
19
- return getRandomValues(rnds8);
20
- }
21
-
22
- /**
23
- * Convert array of 16 byte values to UUID string format of the form:
24
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
25
- */
26
-
27
- const byteToHex = [];
28
-
29
- for (let i = 0; i < 256; ++i) {
30
- byteToHex.push((i + 0x100).toString(16).slice(1));
31
- }
32
-
33
- function unsafeStringify(arr, offset = 0) {
34
- // Note: Be careful editing this code! It's been tuned for performance
35
- // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
36
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
39
+ return getRandomValues(rnds8);
37
40
  }
38
41
 
39
42
  const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
40
- var native = {
41
- randomUUID
42
- };
43
+ var native = { randomUUID };
43
44
 
44
45
  function v4(options, buf, offset) {
45
- if (native.randomUUID && !buf && !options) {
46
- return native.randomUUID();
47
- }
48
-
49
- options = options || {};
50
- const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
51
-
52
- rnds[6] = rnds[6] & 0x0f | 0x40;
53
- rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
54
-
55
- if (buf) {
56
- offset = offset || 0;
57
-
58
- for (let i = 0; i < 16; ++i) {
59
- buf[offset + i] = rnds[i];
46
+ if (native.randomUUID && true && !options) {
47
+ return native.randomUUID();
60
48
  }
61
-
62
- return buf;
63
- }
64
-
65
- return unsafeStringify(rnds);
49
+ options = options || {};
50
+ const rnds = options.random ?? options.rng?.() ?? rng();
51
+ if (rnds.length < 16) {
52
+ throw new Error('Random bytes length must be >= 16');
53
+ }
54
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
55
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
56
+ return unsafeStringify(rnds);
66
57
  }
67
58
 
68
59
  // console.log('OUT', __name({variableName}) );
@@ -150,25 +141,19 @@ const l_= {
150
141
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
151
142
  var JSON_Msg = (function() {
152
143
  return {
153
- merge : function(json) {
154
- const dt_Msg= new Date();
155
- let json_msg= {
156
- id : v4(),
157
- referer : null,
158
- dt : dt_Msg.toISOString(),
159
- };
160
- // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
161
- for (var key of Object.keys(json)) {
162
- json_msg[key]= json[key];
163
- }
164
- return json_msg;
144
+ merge: function(json) {
145
+ return Object.assign({
146
+ id: v4(),
147
+ referer: null,
148
+ dt: new Date().toISOString()
149
+ }, json || {});
165
150
  },
166
- validate : function(json) {
167
- const b_valid= validate_Msg(json);
168
- if (!b_valid)
169
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors);
151
+ validate : function (json) {
152
+ const b_valid = schema_Msg(json);
153
+ if (!b_valid)
154
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
170
155
  return b_valid;
171
- }
156
+ },
172
157
  // TODO: requires a separate ajv module require, and JTD specific schema
173
158
  // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
174
159
  // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
@@ -177,26 +162,24 @@ var JSON_Msg = (function() {
177
162
  // }
178
163
  }
179
164
  })();
180
-
165
+
181
166
  var JSON_Msg_msg = (function() {
182
167
  return {
183
- create_with_msg_merge : function(_msg, json) {
184
- let json_msg= JSON_Msg.merge(json);
185
- json_msg._msg= _msg;
186
- return json_msg;
168
+ create_with_msg_merge: function(_msg, json) {
169
+ return Object.assign(JSON_Msg.merge(json), { _msg });
187
170
  },
188
171
  validate : function(json) {
189
172
  if (! JSON_Msg.validate(json))
190
173
  return false;
191
- const b_valid= validate_Msg_msg(json);
174
+ const b_valid= schema_Msg_msg(json);
192
175
  if (!b_valid)
193
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors);
176
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
194
177
  return b_valid;
195
178
  }
196
179
  }
197
180
  })();
198
181
 
199
- var JSON_MsgAck = (function() {
182
+ var JSON_MsgAck_msg = (function() {
200
183
  return {
201
184
  create_with_msgOk : function(_msg, b_ok= true) {
202
185
  return JSON_Msg_msg.create_with_msg_merge(_msg, {
@@ -216,12 +199,12 @@ var JSON_MsgAck = (function() {
216
199
  validate : function(json) {
217
200
  if (! JSON_Msg_msg.validate(json))
218
201
  return false;
219
- const b_valid= validate_MsgAck(json);
202
+ const b_valid= schema_MsgAck_msg(json);
220
203
  if (!b_valid)
221
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors);
204
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
222
205
  return b_valid;
223
206
  }
224
207
  }
225
208
  })();
226
209
 
227
- export { JSON_Msg, JSON_MsgAck, JSON_Msg_msg };
210
+ export { JSON_Msg, JSON_MsgAck_msg, JSON_Msg_msg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rootintf/json-msg",
3
- "version": "0.2.1",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "main": "dist/json-msg.cjs.js",
6
6
  "browser": "dist/json-msg.es.mjs",
@@ -8,7 +8,7 @@
8
8
  "build": "node src/ajv-gen-validate.cjs && rollup --config",
9
9
  "script": "node --experimental-json-modules tests/json-msg.test.mjs",
10
10
  "test": "jasmine --config=jasmine.json",
11
- "clean": "del dist && del src/validate-esm.mjs && echo OK"
11
+ "clean": "del-cli dist && del-cli src/validate-esm.mjs && echo OK"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -25,15 +25,16 @@
25
25
  "homepage": "https://gitlab.com/interface-x/npm-json-msg.git#readme",
26
26
  "dependencies": {
27
27
  "@knev/bitlogr": "^0.2.12",
28
- "uuid": "^9.0.0"
28
+ "ajv": "^8.17.1",
29
+ "uuid": "^11.1.0"
29
30
  },
30
31
  "devDependencies": {
31
- "@rollup/plugin-commonjs": "^23.0.0",
32
- "@rollup/plugin-json": "^5.0.0",
33
- "@rollup/plugin-node-resolve": "^15.0.0",
32
+ "@rollup/plugin-commonjs": "^28.0.3",
33
+ "@rollup/plugin-json": "^6.1.0",
34
+ "@rollup/plugin-node-resolve": "^16.0.1",
34
35
  "ajv-cli": "^5.0.0",
35
- "del-cli": "^5.0.0",
36
- "jasmine": "^4.5.0",
37
- "rollup": "^2.79.1"
36
+ "del-cli": "^6.0.0",
37
+ "jasmine": "^5.6.0",
38
+ "rollup": "^4.35.0"
38
39
  }
39
40
  }
@@ -1,11 +1,10 @@
1
1
  {
2
- "$id": "validate_Msg",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/JSON_Msg/schema_Msg",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["id", "referer", "dt"],
6
6
  "properties" : {
7
7
  "id" : { "type" : "string"},
8
- "referer" : { "type" : "array", "minItems" : 2, "maxItems": 2, "items": { "type" : "string" } },
9
8
  "dt" : { "type" : "string" }
10
9
  }
11
10
  }
@@ -1,6 +1,6 @@
1
1
  {
2
- "$id": "validate_MsgAck",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["ack"],
6
6
  "properties" : {
@@ -1,6 +1,6 @@
1
1
  {
2
- "$id": "validate_Msg_msg",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["_msg"]
6
6
  }
@@ -1,13 +1,13 @@
1
1
 
2
- var schema_json_msg_= require('../schema/schema_Msg.json');
3
- var schema_json_msg_msg_= require('../schema/schema_Msg_msg.json');
4
- var schema_json_msgAck_= require('../schema/schema_MsgAck.json');
2
+ var json_schema_msg_= require('../schema/schema_Msg.json');
3
+ var json_schema_msg_msg_= require('../schema/schema_Msg_msg.json');
4
+ var json_schema__msgAck_msg_= require('../schema/schema_MsgAck_msg.json');
5
5
 
6
6
  // https://ajv.js.org/standalone.html
7
7
 
8
8
  const fs = require("fs")
9
9
  const path = require("path")
10
- const Ajv = require("ajv")
10
+ const Ajv2019 = require("ajv/dist/2019");
11
11
  const standaloneCode = require("ajv/dist/standalone").default
12
12
 
13
13
  // For ESM, the export name needs to be a valid export name, it can not be `export const #/definitions/Foo = ...;` so we
@@ -15,8 +15,18 @@ const standaloneCode = require("ajv/dist/standalone").default
15
15
  // `export const Foo = ...;export const Bar = ...;`
16
16
  // This mapping would not have been needed if the `$ids` was just `Bar` and `Foo` instead of `#/definitions/Foo`
17
17
  // and `#/definitions/Bar` respectfully
18
- const ajv = new Ajv({schemas: [schema_json_msg_, schema_json_msg_msg_, schema_json_msgAck_], code: {source: true, esm: true}})
19
- let moduleCode = standaloneCode(ajv)
18
+ const ajv = new Ajv2019({schemas: [
19
+ json_schema_msg_,
20
+ json_schema_msg_msg_,
21
+ json_schema__msgAck_msg_
22
+ ],
23
+ code: {source: true, esm: true}
24
+ });
25
+ let moduleCode = standaloneCode(ajv, {
26
+ "schema_Msg": "https://root-interface.se/protocols/JSON_Msg/schema_Msg",
27
+ "schema_Msg_msg" : "https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg",
28
+ "schema_MsgAck_msg" : "https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg"
29
+ })
20
30
 
21
31
  // Now you can write the module code to file
22
32
  fs.writeFileSync(path.join(__dirname, "./validate-esm.mjs"), moduleCode)
@@ -1,5 +1,5 @@
1
1
 
2
- import { validate_Msg, validate_Msg_msg, validate_MsgAck } from './validate-esm.mjs';
2
+ import { schema_Msg, schema_Msg_msg, schema_MsgAck_msg } from './validate-esm.mjs';
3
3
 
4
4
  import { v4 as uuidv4 } from 'uuid';
5
5
  import { BitLogr, l_LL } from '@knev/bitlogr';
@@ -15,25 +15,19 @@ const l_= {
15
15
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
16
16
  var JSON_Msg = (function() {
17
17
  return {
18
- merge : function(json) {
19
- const dt_Msg= new Date();
20
- let json_msg= {
21
- id : uuidv4(),
22
- referer : null,
23
- dt : dt_Msg.toISOString(),
24
- }
25
- // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
26
- for (var key of Object.keys(json)) {
27
- json_msg[key]= json[key];
28
- }
29
- return json_msg;
18
+ merge: function(json) {
19
+ return Object.assign({
20
+ id: uuidv4(),
21
+ referer: null,
22
+ dt: new Date().toISOString()
23
+ }, json || {});
30
24
  },
31
- validate : function(json) {
32
- const b_valid= validate_Msg(json);
33
- if (!b_valid)
34
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors)
25
+ validate : function (json) {
26
+ const b_valid = schema_Msg(json);
27
+ if (!b_valid)
28
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors)
35
29
  return b_valid;
36
- }
30
+ },
37
31
  // TODO: requires a separate ajv module require, and JTD specific schema
38
32
  // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
39
33
  // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
@@ -42,26 +36,24 @@ var JSON_Msg = (function() {
42
36
  // }
43
37
  }
44
38
  })();
45
-
39
+
46
40
  var JSON_Msg_msg = (function() {
47
41
  return {
48
- create_with_msg_merge : function(_msg, json) {
49
- let json_msg= JSON_Msg.merge(json);
50
- json_msg._msg= _msg;
51
- return json_msg;
42
+ create_with_msg_merge: function(_msg, json) {
43
+ return Object.assign(JSON_Msg.merge(json), { _msg });
52
44
  },
53
45
  validate : function(json) {
54
46
  if (! JSON_Msg.validate(json))
55
47
  return false;
56
- const b_valid= validate_Msg_msg(json);
48
+ const b_valid= schema_Msg_msg(json);
57
49
  if (!b_valid)
58
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors)
50
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors)
59
51
  return b_valid;
60
52
  }
61
53
  }
62
54
  })();
63
55
 
64
- var JSON_MsgAck = (function() {
56
+ var JSON_MsgAck_msg = (function() {
65
57
  return {
66
58
  create_with_msgOk : function(_msg, b_ok= true) {
67
59
  return JSON_Msg_msg.create_with_msg_merge(_msg, {
@@ -81,9 +73,9 @@ var JSON_MsgAck = (function() {
81
73
  validate : function(json) {
82
74
  if (! JSON_Msg_msg.validate(json))
83
75
  return false;
84
- const b_valid= validate_MsgAck(json);
76
+ const b_valid= schema_MsgAck_msg(json);
85
77
  if (!b_valid)
86
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors)
78
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors)
87
79
  return b_valid;
88
80
  }
89
81
  }
@@ -94,5 +86,5 @@ var JSON_MsgAck = (function() {
94
86
  export {
95
87
  JSON_Msg,
96
88
  JSON_Msg_msg,
97
- JSON_MsgAck
89
+ JSON_MsgAck_msg
98
90
  }
@@ -1 +1 @@
1
- "use strict";export const validate_Msg = validate10;const schema11 = {"$id":"validate_Msg","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["id","referer","dt"],"properties":{"id":{"type":"string"},"referer":{"type":"array","minItems":2,"maxItems":2,"items":{"type":"string"}},"dt":{"type":"string"}}};function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_Msg" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){let data1 = data.referer;const _errs3 = errors;if(errors === _errs3){if(Array.isArray(data1)){if(data1.length > 2){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/maxItems",keyword:"maxItems",params:{limit: 2},message:"must NOT have more than 2 items"}];return false;}else {if(data1.length < 2){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/minItems",keyword:"minItems",params:{limit: 2},message:"must NOT have fewer than 2 items"}];return false;}else {var valid1 = true;const len0 = data1.length;for(let i0=0; i0<len0; i0++){const _errs5 = errors;if(typeof data1[i0] !== "string"){validate10.errors = [{instancePath:instancePath+"/referer/" + i0,schemaPath:"#/properties/referer/items/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid1 = _errs5 === errors;if(!valid1){break;}}}}}else {validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "array"},message:"must be array"}];return false;}}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs7 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs7 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}export const validate_Msg_msg = validate11;const schema12 = {"$id":"validate_Msg_msg","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["_msg"]};function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_Msg_msg" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}export const validate_MsgAck = validate12;const schema13 = {"$id":"validate_MsgAck","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["ack"],"properties":{"ack":{"type":"object","required":["ok"],"properties":{"ok":{"type":"boolean"}}}}};function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_MsgAck" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;const _errs1 = errors;if(errors === _errs1){if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
1
+ "use strict";export const schema_Msg = validate18;const schema21 = {"$id":"https://root-interface.se/protocols/JSON_Msg/schema_Msg","$schema":"https://json-schema.org/draft/2019-09/schema","type":"object","required":["id","referer","dt"],"properties":{"id":{"type":"string"},"dt":{"type":"string"}}};function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){/*# sourceURL="https://root-interface.se/protocols/JSON_Msg/schema_Msg" */;let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};export const schema_Msg_msg = validate19;const schema22 = {"$id":"https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg","$schema":"https://json-schema.org/draft/2019-09/schema","type":"object","required":["_msg"]};function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){/*# sourceURL="https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg" */;let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};export const schema_MsgAck_msg = validate20;const schema23 = {"$id":"https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg","$schema":"https://json-schema.org/draft/2019-09/schema","type":"object","required":["ack"],"properties":{"ack":{"type":"object","required":["ok"],"properties":{"ok":{"type":"boolean"}}}}};function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){/*# sourceURL="https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg" */;let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;const _errs1 = errors;if(errors === _errs1){if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
@@ -1,5 +1,24 @@
1
1
 
2
- import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck } from '../src/json_msg+ack.mjs';
2
+ import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck_msg } from '../src/json_msg+ack.mjs';
3
+
4
+ const json_msgWarp_nomsg = {
5
+ "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
6
+ "referer": [ "ipsme.dev/js-blue/blue.html", "afab6b6b-2284-4abd-b71e-1e369b7e9220" ],
7
+ "dt": "2023-03-10T20:10:39.408Z",
8
+ "ack": {
9
+ "ok": true,
10
+ }
11
+ };
12
+
13
+ const json_msgWarp_msg_null = {
14
+ "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
15
+ "referer": [ "ipsme.dev/js-blue/blue.html", "afab6b6b-2284-4abd-b71e-1e369b7e9220" ],
16
+ "dt": "2023-03-10T20:10:39.408Z",
17
+ "ack": {
18
+ "ok": true,
19
+ },
20
+ "_msg": null
21
+ };
3
22
 
4
23
  const json_msgAckWarp = {
5
24
  "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
@@ -14,21 +33,47 @@ const json_msgAckWarp = {
14
33
  //-------------------------------------------------------------------------------------------------
15
34
  // JSON_Msg
16
35
 
17
- describe("Validation", function () {
36
+ describe("JSON_Msg validation", function () {
37
+ it("JSON_Msg.validate({})", function () {
38
+ const merged_msg = JSON_Msg.merge({});
39
+ expect( JSON_Msg.validate(merged_msg) ).toBe(true);
40
+ });
41
+
42
+ it("JSON_Msg.merge({ foo: \"bar\" })", function () {
43
+ const merged_msg = JSON_Msg.merge({ foo: "bar" });
44
+
45
+ expect( merged_msg.foo ).toBe('bar');
46
+ expect( JSON_Msg.validate(merged_msg) ).toBe(true);
47
+ });
48
+
18
49
  it("JSON_Msg.validate(json_msgAckWarp)", function () {
19
50
  expect( JSON_Msg.validate(json_msgAckWarp) ).toBe(true);
20
51
  });
21
52
  });
22
53
 
23
- describe("Validation", function () {
54
+ //-------------------------------------------------------------------------------------------------
55
+ // JSON_Msg_msg
56
+
57
+ describe("JSON_Msg_msg validation", function () {
24
58
  it("JSON_Msg_msg.validate(json_msgAckWarp)", function () {
25
59
  expect( JSON_Msg_msg.validate(json_msgAckWarp) ).toBe(true);
26
60
  });
61
+
62
+ it("JSON_Msg_msg.validate(json_msgWarp_nomsg)", function () {
63
+ expect( JSON_Msg_msg.validate(json_msgWarp_nomsg) ).toBe(false);
64
+ });
65
+
66
+ it("JSON_Msg_msg.validate(json_msgWarp_msg_null)", function () {
67
+ expect( JSON_Msg_msg.validate(json_msgWarp_msg_null) ).toBe(true);
68
+ });
27
69
  });
28
70
 
29
- describe("Validation", function () {
71
+ //-------------------------------------------------------------------------------------------------
72
+ // JSON_MsgAck_msg
73
+
74
+ describe("JSON_MsgAck_msg validation", function () {
30
75
  it("JSON_MsgAck.validate(json_msgAckWarp)", function () {
31
- expect( JSON_MsgAck.validate(json_msgAckWarp) ).toBe(true);
76
+ expect( JSON_MsgAck_msg.validate(json_msgAckWarp) ).toBe(true);
32
77
  });
33
78
  });
34
79