Skip to content

Commit d5fa602

Browse files
authored
docs: update README
[skip ci]
2 parents 8ff2a07 + 617f3e0 commit d5fa602

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,111 @@ Features of the library:
1616
- Custom validation rules can be added using the JSON Expression language.
1717
- Can generate random JSON values that match the schema.
1818

19+
## Quick Start
20+
21+
Define a user schema and validate data in just a few lines:
22+
23+
```ts
24+
import {t} from '@jsonjoy.com/json-type';
25+
26+
// Define a user type
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+
});
34+
35+
// Validate data
36+
const isValid = User.validateSchema();
37+
User.validate({
38+
id: 1,
39+
name: "Alice",
40+
41+
age: 30
42+
}); // ✅ Valid
43+
44+
// Generate random test data
45+
const randomUser = User.random();
46+
// { id: 42, name: "xyz123", email: "abc", age: 25 }
47+
```
48+
49+
## Advanced Features
50+
51+
JSON Type goes beyond basic validation with powerful JIT compilation:
52+
53+
```ts
54+
// Compile ultra-fast validators
55+
const validator = User.compileValidator({errors: 'boolean'});
56+
const isValid = validator(userData); // Blazing fast validation
57+
58+
// Generate TypeScript types
59+
const tsCode = User.toTypeScript();
60+
// type User = { id: number; name: string; email: string; age?: number; }
61+
62+
// Compile optimized serializers
63+
const toJson = User.compileEncoder('json');
64+
const jsonString = toJson(userData); // Faster than JSON.stringify
65+
66+
const toCbor = User.compileCborEncoder();
67+
const cborBytes = toCbor(userData); // Binary serialization
68+
```
69+
70+
## Real-World Example
71+
72+
Build type-safe APIs with complex schemas:
73+
74+
```ts
75+
import {t} from '@jsonjoy.com/json-type';
76+
77+
// Define API request/response types
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+
};
123+
```
19124

20125
## Usage
21126

0 commit comments

Comments
 (0)