Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alii/azs
🤓 Amplify Zod schemas with methods
https://github.com/alii/azs
method node struct ts typescript zod
Last synced: 12 days ago
JSON representation
🤓 Amplify Zod schemas with methods
- Host: GitHub
- URL: https://github.com/alii/azs
- Owner: alii
- Created: 2021-10-25T15:27:30.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-14T16:54:32.000Z (10 months ago)
- Last Synced: 2024-05-02T02:32:15.896Z (6 months ago)
- Topics: method, node, struct, ts, typescript, zod
- Language: TypeScript
- Homepage: https://alistair.sh
- Size: 1.83 MB
- Stars: 328
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# azs
azs allows you to add virtual methods to zod schemas, tying data types to functions. here's a small example for a `user` type.
A more advanced example can be found [here](./example/basic.ts).
```ts
import {z} from 'zod';
import {azs} from 'azs';const userSchema = z.object({
age: z.number(),
name: z.string(),
github: z.string().url(),
});const schema = azs(userSchema, {
// Basic example
isAdult: user => {
return user.age >= 18;
},// Methods can take arguments, but the
// first argument will always be the parsed
// value. You don't have to specify a type
// for the first argument.
is: (user, name: string) => {
return user.name === name;
},// Or, you can access `this` which will be
// the parsed value. Notice how this is a
// method, not an arrow function property.
getName() {
return this.name;
},
});const user = schema.parse(someRandomJSONThatMightBeAUser);
user.isAdult();
user.getName();
user.is('Colin McDonnell');
```