Skip to content

Commit 2de390f

Browse files
Copilotstreamich
andcommitted
feat: add user-friendly examples to top of README
Co-authored-by: streamich <[email protected]>
1 parent 1a48c1c commit 2de390f

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,99 @@ 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+
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+
]);
33+
34+
// Validate data
35+
const isValid = User.validateSchema();
36+
User.validate({
37+
id: 1,
38+
name: "Alice",
39+
40+
age: 30
41+
}); // ✅ Valid
42+
43+
// Generate random test data
44+
const randomUser = User.random();
45+
// { id: 42, name: "xyz123", email: "abc", age: 25 }
46+
```
47+
48+
## Advanced Features
49+
50+
JSON Type goes beyond basic validation with powerful JIT compilation:
51+
52+
```ts
53+
// Compile ultra-fast validators
54+
const validator = User.compileValidator({errors: 'boolean'});
55+
const isValid = validator(userData); // Blazing fast validation
56+
57+
// Generate TypeScript types
58+
const tsCode = User.toTypeScript();
59+
// type User = { id: number; name: string; email: string; age?: number; }
60+
61+
// Compile optimized serializers
62+
const toJson = User.compileEncoder('json');
63+
const jsonString = toJson(userData); // Faster than JSON.stringify
64+
65+
const toCbor = User.compileCborEncoder();
66+
const cborBytes = toCbor(userData); // Binary serialization
67+
```
68+
69+
## Real-World Example
70+
71+
Build type-safe APIs with complex schemas:
72+
73+
```ts
74+
import {t} from '@jsonjoy.com/json-type';
75+
76+
// 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+
}
111+
```
19112

20113
## Usage
21114

0 commit comments

Comments
 (0)