tangled
alpha
login
or
join now
tylur.dev
/
prototypey
prototypey.org - atproto lexicon typescript toolkit - mirror https://github.com/tylersayshi/prototypey
1
fork
atom
overview
issues
pulls
pipelines
update docs to explain validate()
Tyler
3 months ago
65e17d36
9d0dc75f
+62
-2
3 changed files
expand all
collapse all
unified
split
packages
cli
package.json
prototypey
README.md
package.json
+1
-1
packages/cli/package.json
···
1
1
{
2
2
"name": "@prototypey/cli",
3
3
-
"version": "0.2.0",
3
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
63
+
### Runtime Validation
64
64
+
65
65
+
Prototypey provides runtime validation using [@atproto/lexicon](https://www.npmjs.com/package/@atproto/lexicon):
66
66
+
67
67
+
```ts
68
68
+
const lex = lx.lexicon("app.bsky.actor.profile", {
69
69
+
main: lx.record({
70
70
+
key: "self",
71
71
+
record: lx.object({
72
72
+
displayName: lx.string({ maxLength: 64, maxGraphemes: 64 }),
73
73
+
description: lx.string({ maxLength: 256, maxGraphemes: 256 }),
74
74
+
}),
75
75
+
}),
76
76
+
});
77
77
+
78
78
+
// Validate data against the schema
79
79
+
const result = lex.validate({
80
80
+
displayName: "Alice",
81
81
+
description: "Software engineer",
82
82
+
});
83
83
+
84
84
+
if (result.success) {
85
85
+
console.log("Valid data:", result.value);
86
86
+
} else {
87
87
+
console.error("Validation error:", result.error);
88
88
+
}
89
89
+
```
90
90
+
91
91
+
**Validating against specific definitions:**
92
92
+
93
93
+
If your lexicon has multiple definitions, you can validate against a specific one:
94
94
+
95
95
+
```ts
96
96
+
const lex = lx.lexicon("app.bsky.feed.post", {
97
97
+
user: lx.object({
98
98
+
handle: lx.string({ required: true }),
99
99
+
displayName: lx.string(),
100
100
+
}),
101
101
+
main: lx.record({
102
102
+
key: "tid",
103
103
+
record: lx.object({
104
104
+
text: lx.string({ required: true }),
105
105
+
author: lx.ref("#user", { required: true }),
106
106
+
}),
107
107
+
}),
108
108
+
});
109
109
+
110
110
+
// Validate against the "user" definition
111
111
+
const userResult = lex.validate(
112
112
+
{ handle: "alice.bsky.social", displayName: "Alice" },
113
113
+
"user",
114
114
+
);
115
115
+
116
116
+
// Validate against "main" (default if not specified)
117
117
+
const postResult = lex.validate({
118
118
+
text: "Hello world",
119
119
+
author: { handle: "bob.bsky.social" },
120
120
+
});
121
121
+
```
122
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
3
-
"version": "0.2.0",
3
3
+
"version": "0.2.1",
4
4
"description": "atproto lexicon typescript toolkit",
5
5
"repository": {
6
6
"type": "git",