@rootintf/json-msg 0.2.1 → 1.0.4
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/antora-docs/antora.yml +5 -0
- package/antora-docs/modules/ROOT/nav.adoc +1 -0
- package/antora-docs/modules/ROOT/pages/index.adoc +76 -0
- package/dist/json-msg.cjs.js +111 -124
- package/dist/json-msg.es.mjs +112 -123
- package/package.json +10 -9
- package/schema/schema_Msg.json +2 -3
- package/schema/{schema_MsgAck.json → schema_MsgAck_msg.json} +2 -2
- package/schema/schema_Msg_msg.json +2 -2
- package/src/ajv-gen-validate.cjs +16 -6
- package/src/json_msg+ack.mjs +66 -68
- package/src/validate-esm.mjs +1 -1
- package/tests/json_msg+ack.spec.mjs +53 -5
|
@@ -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
|
+
----
|
package/dist/json-msg.cjs.js
CHANGED
|
@@ -1,72 +1,61 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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}) );
|
|
@@ -153,81 +142,79 @@ const l_= {
|
|
|
153
142
|
|
|
154
143
|
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
155
144
|
var JSON_Msg = (function() {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
// }
|
|
182
|
-
}
|
|
145
|
+
function JSON_Msg() {}
|
|
146
|
+
JSON_Msg.prototype.merge = function (json) {
|
|
147
|
+
const dt_Msg = new Date();
|
|
148
|
+
let json_msg = {
|
|
149
|
+
id: v4(),
|
|
150
|
+
referer: null,
|
|
151
|
+
dt: dt_Msg.toISOString(),
|
|
152
|
+
};
|
|
153
|
+
const instance = Object.create(JSON_Msg.prototype);
|
|
154
|
+
Object.assign(instance, json_msg, json);
|
|
155
|
+
return instance;
|
|
156
|
+
};
|
|
157
|
+
JSON_Msg.prototype.validate = function (json) {
|
|
158
|
+
const b_valid = schema_Msg(json);
|
|
159
|
+
if (!b_valid)
|
|
160
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
|
|
161
|
+
return b_valid;
|
|
162
|
+
};
|
|
163
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
164
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
165
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
166
|
+
// static parse(json) {
|
|
167
|
+
// return this.#ajv_parse_(json);
|
|
168
|
+
// }
|
|
169
|
+
return new JSON_Msg();
|
|
183
170
|
})();
|
|
184
|
-
|
|
171
|
+
|
|
185
172
|
var JSON_Msg_msg = (function() {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
173
|
+
function JSON_Msg_msg() {}
|
|
174
|
+
JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
|
|
175
|
+
let json_msg= JSON_Msg.merge(json);
|
|
176
|
+
json_msg._msg= _msg;
|
|
177
|
+
return json_msg;
|
|
178
|
+
};
|
|
179
|
+
JSON_Msg_msg.prototype.validate = function(json) {
|
|
180
|
+
if (! JSON_Msg.validate(json))
|
|
181
|
+
return false;
|
|
182
|
+
const b_valid= schema_Msg_msg(json);
|
|
183
|
+
if (!b_valid)
|
|
184
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
|
|
185
|
+
return b_valid;
|
|
186
|
+
};
|
|
187
|
+
return new JSON_Msg_msg()
|
|
201
188
|
})();
|
|
202
189
|
|
|
203
|
-
var
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
190
|
+
var JSON_MsgAck_msg = (function() {
|
|
191
|
+
function JSON_MsgAck_msg() {}
|
|
192
|
+
JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
|
|
193
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
194
|
+
ack : {
|
|
195
|
+
"ok" : b_ok,
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
|
|
200
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
201
|
+
ack : {
|
|
202
|
+
"ok" : false,
|
|
203
|
+
"expired" : true
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
JSON_MsgAck_msg.prototype.validate = function(json) {
|
|
208
|
+
if (! JSON_Msg_msg.validate(json))
|
|
209
|
+
return false;
|
|
210
|
+
const b_valid= schema_MsgAck_msg(json);
|
|
211
|
+
if (!b_valid)
|
|
212
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
|
|
213
|
+
return b_valid;
|
|
214
|
+
};
|
|
215
|
+
return new JSON_MsgAck_msg();
|
|
229
216
|
})();
|
|
230
217
|
|
|
231
218
|
exports.JSON_Msg = JSON_Msg;
|
|
232
|
-
exports.
|
|
219
|
+
exports.JSON_MsgAck_msg = JSON_MsgAck_msg;
|
|
233
220
|
exports.JSON_Msg_msg = JSON_Msg_msg;
|
package/dist/json-msg.es.mjs
CHANGED
|
@@ -1,68 +1,59 @@
|
|
|
1
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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}) );
|
|
@@ -149,79 +140,77 @@ const l_= {
|
|
|
149
140
|
|
|
150
141
|
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
151
142
|
var JSON_Msg = (function() {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// }
|
|
178
|
-
}
|
|
143
|
+
function JSON_Msg() {}
|
|
144
|
+
JSON_Msg.prototype.merge = function (json) {
|
|
145
|
+
const dt_Msg = new Date();
|
|
146
|
+
let json_msg = {
|
|
147
|
+
id: v4(),
|
|
148
|
+
referer: null,
|
|
149
|
+
dt: dt_Msg.toISOString(),
|
|
150
|
+
};
|
|
151
|
+
const instance = Object.create(JSON_Msg.prototype);
|
|
152
|
+
Object.assign(instance, json_msg, json);
|
|
153
|
+
return instance;
|
|
154
|
+
};
|
|
155
|
+
JSON_Msg.prototype.validate = function (json) {
|
|
156
|
+
const b_valid = schema_Msg(json);
|
|
157
|
+
if (!b_valid)
|
|
158
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
|
|
159
|
+
return b_valid;
|
|
160
|
+
};
|
|
161
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
162
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
163
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
164
|
+
// static parse(json) {
|
|
165
|
+
// return this.#ajv_parse_(json);
|
|
166
|
+
// }
|
|
167
|
+
return new JSON_Msg();
|
|
179
168
|
})();
|
|
180
|
-
|
|
169
|
+
|
|
181
170
|
var JSON_Msg_msg = (function() {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
171
|
+
function JSON_Msg_msg() {}
|
|
172
|
+
JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
|
|
173
|
+
let json_msg= JSON_Msg.merge(json);
|
|
174
|
+
json_msg._msg= _msg;
|
|
175
|
+
return json_msg;
|
|
176
|
+
};
|
|
177
|
+
JSON_Msg_msg.prototype.validate = function(json) {
|
|
178
|
+
if (! JSON_Msg.validate(json))
|
|
179
|
+
return false;
|
|
180
|
+
const b_valid= schema_Msg_msg(json);
|
|
181
|
+
if (!b_valid)
|
|
182
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
|
|
183
|
+
return b_valid;
|
|
184
|
+
};
|
|
185
|
+
return new JSON_Msg_msg()
|
|
197
186
|
})();
|
|
198
187
|
|
|
199
|
-
var
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
188
|
+
var JSON_MsgAck_msg = (function() {
|
|
189
|
+
function JSON_MsgAck_msg() {}
|
|
190
|
+
JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
|
|
191
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
192
|
+
ack : {
|
|
193
|
+
"ok" : b_ok,
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
|
|
198
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
199
|
+
ack : {
|
|
200
|
+
"ok" : false,
|
|
201
|
+
"expired" : true
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
JSON_MsgAck_msg.prototype.validate = function(json) {
|
|
206
|
+
if (! JSON_Msg_msg.validate(json))
|
|
207
|
+
return false;
|
|
208
|
+
const b_valid= schema_MsgAck_msg(json);
|
|
209
|
+
if (!b_valid)
|
|
210
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
|
|
211
|
+
return b_valid;
|
|
212
|
+
};
|
|
213
|
+
return new JSON_MsgAck_msg();
|
|
225
214
|
})();
|
|
226
215
|
|
|
227
|
-
export { JSON_Msg,
|
|
216
|
+
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.
|
|
3
|
+
"version": "1.0.4",
|
|
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
|
-
"
|
|
28
|
+
"ajv": "^8.17.1",
|
|
29
|
+
"uuid": "^11.1.0"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"@rollup/plugin-commonjs": "^
|
|
32
|
-
"@rollup/plugin-json": "^
|
|
33
|
-
"@rollup/plugin-node-resolve": "^
|
|
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": "^
|
|
36
|
-
"jasmine": "^
|
|
37
|
-
"rollup": "^
|
|
36
|
+
"del-cli": "^6.0.0",
|
|
37
|
+
"jasmine": "^5.6.0",
|
|
38
|
+
"rollup": "^4.35.0"
|
|
38
39
|
}
|
|
39
40
|
}
|
package/schema/schema_Msg.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"$id": "
|
|
3
|
-
"$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": "
|
|
3
|
-
"$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": "
|
|
3
|
-
"$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
|
}
|
package/src/ajv-gen-validate.cjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
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
|
|
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
|
|
19
|
-
|
|
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)
|
package/src/json_msg+ack.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
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';
|
|
@@ -14,79 +14,77 @@ const l_= {
|
|
|
14
14
|
|
|
15
15
|
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
16
16
|
var JSON_Msg = (function() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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;
|
|
30
|
-
},
|
|
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)
|
|
35
|
-
return b_valid;
|
|
17
|
+
function JSON_Msg() {}
|
|
18
|
+
JSON_Msg.prototype.merge = function (json) {
|
|
19
|
+
const dt_Msg = new Date();
|
|
20
|
+
let json_msg = {
|
|
21
|
+
id: uuidv4(),
|
|
22
|
+
referer: null,
|
|
23
|
+
dt: dt_Msg.toISOString(),
|
|
36
24
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
25
|
+
const instance = Object.create(JSON_Msg.prototype);
|
|
26
|
+
Object.assign(instance, json_msg, json);
|
|
27
|
+
return instance;
|
|
28
|
+
};
|
|
29
|
+
JSON_Msg.prototype.validate = function (json) {
|
|
30
|
+
const b_valid = schema_Msg(json);
|
|
31
|
+
if (!b_valid)
|
|
32
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors)
|
|
33
|
+
return b_valid;
|
|
34
|
+
};
|
|
35
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
36
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
37
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
38
|
+
// static parse(json) {
|
|
39
|
+
// return this.#ajv_parse_(json);
|
|
40
|
+
// }
|
|
41
|
+
return new JSON_Msg();
|
|
44
42
|
})();
|
|
45
|
-
|
|
43
|
+
|
|
46
44
|
var JSON_Msg_msg = (function() {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
45
|
+
function JSON_Msg_msg() {}
|
|
46
|
+
JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
|
|
47
|
+
let json_msg= JSON_Msg.merge(json);
|
|
48
|
+
json_msg._msg= _msg;
|
|
49
|
+
return json_msg;
|
|
50
|
+
};
|
|
51
|
+
JSON_Msg_msg.prototype.validate = function(json) {
|
|
52
|
+
if (! JSON_Msg.validate(json))
|
|
53
|
+
return false;
|
|
54
|
+
const b_valid= schema_Msg_msg(json);
|
|
55
|
+
if (!b_valid)
|
|
56
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors)
|
|
57
|
+
return b_valid;
|
|
61
58
|
}
|
|
59
|
+
return new JSON_Msg_msg()
|
|
62
60
|
})();
|
|
63
61
|
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
62
|
+
var JSON_MsgAck_msg = (function() {
|
|
63
|
+
function JSON_MsgAck_msg() {}
|
|
64
|
+
JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
|
|
65
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
66
|
+
ack : {
|
|
67
|
+
"ok" : b_ok,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
|
|
72
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
73
|
+
ack : {
|
|
74
|
+
"ok" : false,
|
|
75
|
+
"expired" : true
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
JSON_MsgAck_msg.prototype.validate = function(json) {
|
|
80
|
+
if (! JSON_Msg_msg.validate(json))
|
|
81
|
+
return false;
|
|
82
|
+
const b_valid= schema_MsgAck_msg(json);
|
|
83
|
+
if (!b_valid)
|
|
84
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors)
|
|
85
|
+
return b_valid;
|
|
89
86
|
}
|
|
87
|
+
return new JSON_MsgAck_msg();
|
|
90
88
|
})();
|
|
91
89
|
|
|
92
90
|
//-------------------------------------------------------------------------------------------------
|
|
@@ -94,5 +92,5 @@ var JSON_MsgAck = (function() {
|
|
|
94
92
|
export {
|
|
95
93
|
JSON_Msg,
|
|
96
94
|
JSON_Msg_msg,
|
|
97
|
-
|
|
95
|
+
JSON_MsgAck_msg
|
|
98
96
|
}
|
package/src/validate-esm.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";export const
|
|
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,
|
|
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,50 @@ const json_msgAckWarp = {
|
|
|
14
33
|
//-------------------------------------------------------------------------------------------------
|
|
15
34
|
// JSON_Msg
|
|
16
35
|
|
|
17
|
-
describe("
|
|
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.constructor.name ).toBe('JSON_Msg');
|
|
46
|
+
expect( merged_msg.foo ).toBe('bar');
|
|
47
|
+
expect( JSON_Msg.validate(merged_msg) ).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// console.log(); // true
|
|
51
|
+
|
|
18
52
|
it("JSON_Msg.validate(json_msgAckWarp)", function () {
|
|
19
53
|
expect( JSON_Msg.validate(json_msgAckWarp) ).toBe(true);
|
|
20
54
|
});
|
|
21
55
|
});
|
|
22
56
|
|
|
23
|
-
|
|
57
|
+
//-------------------------------------------------------------------------------------------------
|
|
58
|
+
// JSON_Msg_msg
|
|
59
|
+
|
|
60
|
+
describe("JSON_Msg_msg validation", function () {
|
|
24
61
|
it("JSON_Msg_msg.validate(json_msgAckWarp)", function () {
|
|
25
62
|
expect( JSON_Msg_msg.validate(json_msgAckWarp) ).toBe(true);
|
|
26
63
|
});
|
|
64
|
+
|
|
65
|
+
it("JSON_Msg_msg.validate(json_msgWarp_nomsg)", function () {
|
|
66
|
+
expect( JSON_Msg_msg.validate(json_msgWarp_nomsg) ).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("JSON_Msg_msg.validate(json_msgWarp_msg_null)", function () {
|
|
70
|
+
expect( JSON_Msg_msg.validate(json_msgWarp_msg_null) ).toBe(true);
|
|
71
|
+
});
|
|
27
72
|
});
|
|
28
73
|
|
|
29
|
-
|
|
74
|
+
//-------------------------------------------------------------------------------------------------
|
|
75
|
+
// JSON_MsgAck_msg
|
|
76
|
+
|
|
77
|
+
describe("JSON_MsgAck_msg validation", function () {
|
|
30
78
|
it("JSON_MsgAck.validate(json_msgAckWarp)", function () {
|
|
31
|
-
expect(
|
|
79
|
+
expect( JSON_MsgAck_msg.validate(json_msgAckWarp) ).toBe(true);
|
|
32
80
|
});
|
|
33
81
|
});
|
|
34
82
|
|