prototypey.org - atproto lexicon typescript toolkit - mirror https://github.com/tylersayshi/prototypey

update docs to explain validate()

Tyler 65e17d36 9d0dc75f

+62 -2
+1 -1
packages/cli/package.json
··· 1 1 { 2 2 "name": "@prototypey/cli", 3 - "version": "0.2.0", 3 + "version": "0.2.1", 4 4 "description": "CLI tool for generating types from ATProto lexicon schemas", 5 5 "repository": { 6 6 "type": "git",
+60
packages/prototypey/README.md
··· 60 60 61 61 you could also access the json definition with `lex.json()`. 62 62 63 + ### Runtime Validation 64 + 65 + Prototypey provides runtime validation using [@atproto/lexicon](https://www.npmjs.com/package/@atproto/lexicon): 66 + 67 + ```ts 68 + const lex = lx.lexicon("app.bsky.actor.profile", { 69 + main: lx.record({ 70 + key: "self", 71 + record: lx.object({ 72 + displayName: lx.string({ maxLength: 64, maxGraphemes: 64 }), 73 + description: lx.string({ maxLength: 256, maxGraphemes: 256 }), 74 + }), 75 + }), 76 + }); 77 + 78 + // Validate data against the schema 79 + const result = lex.validate({ 80 + displayName: "Alice", 81 + description: "Software engineer", 82 + }); 83 + 84 + if (result.success) { 85 + console.log("Valid data:", result.value); 86 + } else { 87 + console.error("Validation error:", result.error); 88 + } 89 + ``` 90 + 91 + **Validating against specific definitions:** 92 + 93 + If your lexicon has multiple definitions, you can validate against a specific one: 94 + 95 + ```ts 96 + const lex = lx.lexicon("app.bsky.feed.post", { 97 + user: lx.object({ 98 + handle: lx.string({ required: true }), 99 + displayName: lx.string(), 100 + }), 101 + main: lx.record({ 102 + key: "tid", 103 + record: lx.object({ 104 + text: lx.string({ required: true }), 105 + author: lx.ref("#user", { required: true }), 106 + }), 107 + }), 108 + }); 109 + 110 + // Validate against the "user" definition 111 + const userResult = lex.validate( 112 + { handle: "alice.bsky.social", displayName: "Alice" }, 113 + "user", 114 + ); 115 + 116 + // Validate against "main" (default if not specified) 117 + const postResult = lex.validate({ 118 + text: "Hello world", 119 + author: { handle: "bob.bsky.social" }, 120 + }); 121 + ``` 122 + 63 123 ### CLI Commands 64 124 65 125 The `prototypey` package includes a CLI with two main commands:
+1 -1
packages/prototypey/package.json
··· 1 1 { 2 2 "name": "prototypey", 3 - "version": "0.2.0", 3 + "version": "0.2.1", 4 4 "description": "atproto lexicon typescript toolkit", 5 5 "repository": { 6 6 "type": "git",