json-validate-contract.test.mjs
40 lines 1.2 KB
Raw
sha256:94ec65bd2b200240ac785a97cf14c5db066832bd608a24d6a9c151f17b918b02 feat(calendar): hosted bridge/gateway route parity and time… Human minor ⚠ breaking 5 hours ago
1 /**
2 * Contract examples for hub/icp JsonValidate.mo (enrich fragments).
3 * Motoko implementation mirrors RFC 8259 subset; these cases must stay parseable as JSON
4 * values and match JSON.parse for acceptance (including trailing whitespace after the value).
5 */
6 import assert from 'node:assert';
7 import test from 'node:test';
8
9 const validArrays = ['[]', '[1]', ' [ "a" ] ', '[1]\n', '["x","y"]'];
10 const validObjects = ['{}', '{"a":1}', ' { } ', '{"k":[1,2]}'];
11
12 for (const s of validArrays) {
13 test(`valid JSON array: ${JSON.stringify(s)}`, () => {
14 const v = JSON.parse(s);
15 assert.ok(Array.isArray(v));
16 });
17 }
18
19 for (const s of validObjects) {
20 test(`valid JSON object: ${JSON.stringify(s)}`, () => {
21 const v = JSON.parse(s);
22 assert.ok(v !== null && typeof v === 'object' && !Array.isArray(v));
23 });
24 }
25
26 const invalidAsArray = ['{}', '[', '[1,]', '{"a":1}', '[broken', ''];
27 for (const s of invalidAsArray) {
28 test(`not a JSON array (parse fails or not array): ${JSON.stringify(s)}`, () => {
29 try {
30 const v = JSON.parse(s);
31 assert.ok(notArray(v), 'expected not array');
32 } catch {
33 assert.ok(true);
34 }
35 });
36 }
37
38 function notArray(v) {
39 return !Array.isArray(v);
40 }
File History 1 commit
sha256:94ec65bd2b200240ac785a97cf14c5db066832bd608a24d6a9c151f17b918b02 feat(calendar): hosted bridge/gateway route parity and time… Human minor 5 hours ago