Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
jkonowitch committed Mar 27, 2024
1 parent 7f36ecd commit 662da7c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ npm install serializable-ts-zod zod

### Defining Schemas and Creating Classes

To ensure runtime safety and correct type inference, **classes must define a public readonly SCHEMA property**, referencing the corresponding zod schema. Additionally, classes [**must use this SCHEMA as the constructor argument for instantiation**](#note-constructors). This is all enforced via types, so your IDE and `tsc` will error if you do not.
To ensure runtime safety and correct type inference, <u>(1)</u> **classes must define a public readonly SCHEMA property**, referencing the corresponding zod schema, and <u>(2)</u> classes [**must use this SCHEMA as the constructor argument for instantiation**](#note-constructors). This is all enforced via types, so your IDE and `tsc` will error if you do not.

```typescript
import { z } from 'zod';
Expand Down Expand Up @@ -85,8 +85,8 @@ class Address {
@serializable('details')
protected accessor details: { city: string; zipCode: string };

// this should never be called directly by clients
constructor(parameters: z.infer<typeof AddressSchema>) {
// this would never really be called directly by clients
protected constructor(parameters: z.infer<typeof AddressSchema>) {
this.details = parameters.details;
}

Expand Down Expand Up @@ -233,6 +233,11 @@ class Email {
set foo(s: string) {
this.#foo = s;
}

constructor(parameters: z.infer<Email['SCHEMA']>) {
this.foo = parameters.foo;
this.address = parameters.address;
}
}
```

Expand Down

0 comments on commit 662da7c

Please sign in to comment.