-
Notifications
You must be signed in to change notification settings - Fork 5
/
basic.ts
39 lines (32 loc) · 805 Bytes
/
basic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {z} from 'zod';
import {azs} from '../src/index.ts';
const userSchema = z.object({
age: z.number(),
name: z.string(),
github: z.string().url(),
});
const schema = azs(userSchema, {
isAdult: user => {
return user.age >= 18;
},
is: (user, name: string) => {
return user.name === name;
},
// Or, you can access `this` which will be
// the parsed value
getName() {
return this.name;
},
});
const user = schema.parse({
age: 18,
name: 'Alistair',
github: 'https://github.com/alii',
});
// Access the methods
console.log('is adult?', user.isAdult() ? 'Yes!' : 'Nope!');
console.log('is John?', user.is('John') ? 'Yes!' : 'Nope!');
console.log('name:', user.getName());
// Safely still access the original properties
console.log('age', user.age);
console.log('name', user.name);