@@ -24,12 +24,13 @@ Define a user schema and validate data in just a few lines:
2424import {t } from ' @jsonjoy.com/json-type' ;
2525
2626// Define a user type
27- const User = t .Object ([
28- t .prop (' id' , t .Number ()),
29- t .prop (' name' , t .String ()),
30- t .prop (' email' , t .String ()),
31- t .propOpt (' age' , t .Number ({gte: 0 , lte: 120 }))
32- ]);
27+ const User = t .object ({
28+ id: t .num ,
29+ name: t .str ,
30+ email: t .str ,
31+ }).optional ({
32+ age: t .num .gte (0 ).lte (120 ),
33+ });
3334
3435// Validate data
3536const isValid = User .validateSchema ();
@@ -74,40 +75,51 @@ Build type-safe APIs with complex schemas:
7475import {t } from ' @jsonjoy.com/json-type' ;
7576
7677// Define API request/response types
77- const CreatePostRequest = t .Object ([
78- t .prop (' title' , t .String ({min: 1 , max: 100 })),
79- t .prop (' content' , t .String ({min: 10 })),
80- t .prop (' tags' , t .Array (t .String (), {max: 5 })),
81- t .prop (' published' , t .Boolean ())
82- ]);
83-
84- const Post = t .Object ([
85- t .prop (' id' , t .String ()),
86- t .prop (' title' , t .String ()),
87- t .prop (' content' , t .String ()),
88- t .prop (' tags' , t .Array (t .String ())),
89- t .prop (' published' , t .Boolean ()),
90- t .prop (' createdAt' , t .Number ({format: ' u64' })),
91- t .prop (' author' , t .Object ([
92- t .prop (' id' , t .String ()),
93- t .prop (' name' , t .String ())
94- ]))
95- ]);
96-
97- const CreatePostResponse = t .Object ([
98- t .prop (' success' , t .Boolean ()),
99- t .prop (' post' , Post ),
100- t .propOpt (' error' , t .String ())
101- ]);
102-
103- // Use in your API
104- function createPost(data : unknown ) {
105- CreatePostRequest .validate (data ); // Throws if invalid
106-
107- // Your business logic here...
108-
109- return CreatePostResponse .random (); // Type-safe response
110- }
78+ const CreatePostRequest = t .object ({
79+ title: t .str .options ({min: 1 , max: 100 }),
80+ content: t .str .options ({min: 10 }),
81+ tags: t .array (t .str ).options ({max: 5 }),
82+ published: t .bool ,
83+ });
84+
85+ const Post = t .object ({
86+ id: t .str ,
87+ title: t .str ,
88+ content: t .str ,
89+ tags: t .array (t .str ),
90+ published: t .bool ,
91+ createdAt: t .num .options ({format: ' u64' }),
92+ author: t .object ({
93+ id: t .str ,
94+ name: t .str ,
95+ }),
96+ });
97+
98+ const CreatePostResponse = t .object ({
99+ success: t .bool ,
100+ post: Post ,
101+ }).optional ({
102+ error: t .str ,
103+ });
104+
105+ // Extract TypeScript types using t.infer
106+ type PostType = t .infer <typeof Post >;
107+ type CreateRequestType = t .infer <typeof CreatePostRequest >;
108+ type CreateResponseType = t .infer <typeof CreatePostResponse >;
109+
110+ // Now you have full type safety!
111+ const newPost: PostType = {
112+ id: " 123" ,
113+ title: " My Blog Post" ,
114+ content: " This is the content..." ,
115+ tags: [" typescript" , " json" ],
116+ published: true ,
117+ createdAt: Date .now (),
118+ author: {
119+ id: " user456" ,
120+ name: " John Doe"
121+ }
122+ };
111123```
112124
113125## Usage
0 commit comments