mirror of
https://github.com/versia-pub/api.git
synced 2025-12-06 08:28:19 +01:00
docs(docs): 📝 Update docs
This commit is contained in:
parent
8028983faa
commit
1c10142c04
|
|
@ -18,7 +18,7 @@ Compilation (bundling/minifying) time is a few seconds, almost all of which is s
|
||||||
|
|
||||||
#### Roadmap
|
#### Roadmap
|
||||||
|
|
||||||
- [x] Zod objects
|
- [x] Validation
|
||||||
- [ ] Signing code
|
- [ ] Signing code
|
||||||
- [ ] Advanced validator
|
- [ ] Advanced validator
|
||||||
|
|
||||||
|
|
@ -26,21 +26,32 @@ Compilation (bundling/minifying) time is a few seconds, almost all of which is s
|
||||||
|
|
||||||
[**Zod**](https://zod.dev) is used to validate and parse the objects. All Lysand objects are already written for you.
|
[**Zod**](https://zod.dev) is used to validate and parse the objects. All Lysand objects are already written for you.
|
||||||
|
|
||||||
You may use the `InferType<T>` export to get a direct type from the object.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Note is a Zod object
|
import { EntityValidator, type ValidationError } from "@lysand-org/federation";
|
||||||
import { Note, type InferType } from "@lysand-org/federation";
|
|
||||||
|
|
||||||
const badObject = {
|
const validator = new EntityValidator();
|
||||||
IamBad: "Note",
|
|
||||||
|
try {
|
||||||
|
// Will throw an error when the object is invalid, otherwise return the correct object
|
||||||
|
const invalidNote = await validator.Note({
|
||||||
|
// This is invalid
|
||||||
|
type: "Note",
|
||||||
|
content: 123,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
// ToString returns the human-friendly error message
|
||||||
|
sendUser((error as ValidationError).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Types are also included for TypeScript users that don't use the extracted ones
|
||||||
|
const validNoteObject: typeof EntityValidator.$Note = {
|
||||||
|
type: "Note",
|
||||||
|
// ...
|
||||||
};
|
};
|
||||||
|
|
||||||
// Will throw an error
|
const validNote = await validator.Note(validNoteObject);
|
||||||
const parsed = await Note.parseAsync(badObject);
|
|
||||||
|
|
||||||
// Infer the TypeScript type from the object
|
// validNote is still the same as noteObject
|
||||||
type NoteType = InferType<typeof Note>;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
For more information about Note's methods, see the [**Zod documentation**](https://zod.dev/docs/).
|
For more information about Note's methods, see the [**Zod documentation**](https://zod.dev/docs/).
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue