cerulea/lexicon

define atproto schemas in TypeScript

git clone https://git.t4t.associates/cerulea/lexicon

Charlotte Sominitial commit1945441

main
1.8 KiB47 linesraw
1import * as l from "../mod.ts";
2import { assert, throws } from "./assert.ts";
3
4Deno.test("byte values use standard base64 with optional, correctly formed padding", () => {
5  const check = l.compile(l.bytes);
6  for (const value of ["", "AA", "AA==", "AQI", "AQI=", "AQID", "+/8="]) {
7    assert(check({ $bytes: value }).success, `Rejected base64: ${value}`);
8  }
9  for (const value of ["A", "A=", "AA=", "AA===", "A===", "AQI==", "A A", "-_8=", "AB", "AQJ="]) {
10    assert(!check({ $bytes: value }).success, `Accepted malformed base64: ${value}`);
11  }
12  assert(!check({ $bytes: "AA", extra: true }).success);
13  assert(!check({ $bytes: true }).success);
14  assert(!check(new Uint8Array([1])).success);
15  assert(l.compile(l.bytesWith({ maxLength: 1 }))({ $bytes: "AA==" }).success);
16  assert(!l.compile(l.bytesWith({ minLength: 1 }))({ $bytes: "" }).success);
17});
18
19Deno.test("objects cannot themselves be compound values", () => {
20  const check = l.compile(l.object({}));
21  assert(!check({ $bytes: "AQI=" }).success);
22  assert(!check({ $type: "blob" }).success);
23  throws(() => l.object({ $bytes: l.string }), "reserved");
24});
25
26Deno.test("malformed CID headers are rejected even if their base32 encoding is valid", () => {
27  const check = l.compile(l.cid);
28  // Independently encode binary CID headers to exercise version/codec/hash validation.
29  for (
30    const header of [[2, 0x71, 0x12, 32], [1, 0x70, 0x12, 32], [1, 0x71, 0x13, 32], [
31      1,
32      0x71,
33      0x12,
34      31,
35    ]]
36  ) {
37    const bits = [...header, ...new Array<number>(32).fill(0)].map((n) =>
38      n.toString(2).padStart(8, "0")
39    ).join("");
40    let encoded = "b";
41    for (let i = 0; i < bits.length; i += 5) {
42      encoded +=
43        "abcdefghijklmnopqrstuvwxyz234567"[parseInt(bits.slice(i, i + 5).padEnd(5, "0"), 2)];
44    }
45    assert(!check(encoded).success);
46  }
47});