Skip to content

Commit

Permalink
docs(docs): 📝 Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed May 14, 2024
1 parent 8028983 commit 1c10142
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions federation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,40 @@ Compilation (bundling/minifying) time is a few seconds, almost all of which is s

#### Roadmap

- [x] Zod objects
- [x] Validation
- [ ] Signing code
- [ ] Advanced validator

#### Validation

[**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
// Note is a Zod object
import { Note, type InferType } from "@lysand-org/federation";

const badObject = {
IamBad: "Note",
import { EntityValidator, type ValidationError } from "@lysand-org/federation";

const validator = new EntityValidator();

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 parsed = await Note.parseAsync(badObject);
const validNote = await validator.Note(validNoteObject);

// Infer the TypeScript type from the object
type NoteType = InferType<typeof Note>;
// validNote is still the same as noteObject
```

For more information about Note's methods, see the [**Zod documentation**](https://zod.dev/docs/).
Expand Down

0 comments on commit 1c10142

Please sign in to comment.