cerulea/lexicon
define atproto schemas in TypeScript
git clone https://git.t4t.associates/cerulea/lexicon
1945441
main
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" , () => { 5const check = l . compile ( l . bytes ); 6for ( const value of [ "" , "AA" , "AA==" , "AQI" , "AQI=" , "AQID" , "+/8=" ]) { 7assert ( check ({ $bytes :value }). success , `Rejected base64: ${ value } ` ); 8} 9for ( const value of [ "A" , "A=" , "AA=" , "AA===" , "A===" , "AQI==" , "A A" , "-_8=" , "AB" , "AQJ=" ]) { 10assert ( ! check ({ $bytes :value }). success , `Accepted malformed base64: ${ value } ` ); 11} 12assert ( ! check ({ $bytes :"AA" , extra :true }). success ); 13assert ( ! check ({ $bytes :true }). success ); 14assert ( ! check ( new Uint8Array ([ 1 ])). success ); 15assert ( l . compile ( l . bytesWith ({ maxLength :1 }))({ $bytes :"AA==" }). success ); 16assert ( ! l . compile ( l . bytesWith ({ minLength :1 }))({ $bytes :"" }). success ); 17}); 18 19Deno . test ( "objects cannot themselves be compound values" , () => { 20const check = l . compile ( l . object ({})); 21assert ( ! check ({ $bytes :"AQI=" }). success ); 22assert ( ! check ({ $type :"blob" }). success ); 23throws (() => l . object ({ $bytes :l . string }), "reserved" ); 24}); 25 26Deno . test ( "malformed CID headers are rejected even if their base32 encoding is valid" , () => { 27const check = l . compile ( l . cid ); 28// Independently encode binary CID headers to exercise version/codec/hash validation. 29for ( 30const header of [[ 2 , 0x71 , 0x12 , 32 ], [ 1 , 0x70 , 0x12 , 32 ], [ 1 , 0x71 , 0x13 , 32 ], [ 311 , 320x71 , 330x12 , 3431 , 35]] 36) { 37const bits = [ ...header , ...new Array < number >( 32 ). fill ( 0 )]. map (( n ) => 38n . toString ( 2 ). padStart ( 8 , "0" ) 39). join ( "" ); 40let encoded = "b" ; 41for ( let i = 0 ; i < bits . length ; i += 5 ) { 42encoded += 43"abcdefghijklmnopqrstuvwxyz234567" [ parseInt ( bits . slice ( i , i + 5 ). padEnd ( 5 , "0" ), 2 )]; 44} 45assert ( ! check ( encoded ). success ); 46} 47});