Skip to content

Commit 3ec4291

Browse files
✨ Added Valibot Support
1 parent 43048fa commit 3ec4291

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A lightweight, type-safe TypeScript library for generating prefixed IDs with cus
1111
- ⚙️ **Highly Customizable** - Configure separators, length, and character sets
1212
- 📦 **Lightweight** - Minimal dependencies (only `nanoid`)
1313
- 🔍 **Zod Integration** - Built-in Zod validation schemas (optional)
14+
-**Valibot Integration** - Built-in Valibot validation schemas (optional)
1415
- 🧪 **Well Tested** - Comprehensive test coverage
1516
- 📚 **Modern ESM** - ES modules with CommonJS support
1617

@@ -34,6 +35,12 @@ For Zod validation support:
3435
npm install zod
3536
```
3637

38+
For Valibot validation support:
39+
40+
```bash
41+
npm install valibot
42+
```
43+
3744
## 📖 Usage
3845

3946
### Basic Usage
@@ -136,6 +143,33 @@ const userSchema = z.object({
136143
});
137144
```
138145

146+
## ✅ Valibot Integration
147+
148+
If you're using Valibot for validation, typed-id provides built-in schema creators:
149+
150+
```typescript
151+
import { IdHelper } from "typed-id";
152+
import { createValibotIdSchema } from "typed-id/validators/validbot";
153+
import { safeParse } from "valibot";
154+
155+
const userIdHelper = new IdHelper("user");
156+
const userIdSchema = createValibotIdSchema(userIdHelper);
157+
158+
// Validate IDs
159+
const validId = userIdHelper.generate();
160+
console.log(safeParse(userIdSchema, validId).success); // true
161+
console.log(safeParse(userIdSchema, "invalid_id").success); // false
162+
163+
// Use in your Valibot schemas
164+
import { object, string, email } from "valibot";
165+
166+
const userSchema = object({
167+
id: userIdSchema,
168+
name: string(),
169+
email: string([email()]),
170+
});
171+
```
172+
139173
## ⚙️ Configuration Options
140174

141175
| Option | Type | Default | Description |
@@ -265,6 +299,7 @@ This library has comprehensive test coverage including:
265299
- ID generation with default options
266300
- ID generation with custom options
267301
- Zod validation schemas
302+
- Valibot validation schemas
268303
- Type safety verification
269304

270305
Run tests:

bun.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
},
1717
"peerDependencies": {
1818
"typescript": "^5",
19+
"valibot": "^1.1.0",
1920
"zod": "^4.0.17",
2021
},
2122
"optionalPeers": [
23+
"valibot",
2224
"zod",
2325
],
2426
},

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@
7171
},
7272
"peerDependencies": {
7373
"typescript": "^5",
74+
"valibot": "^1.1.0",
7475
"zod": "^4.0.17"
7576
},
7677
"dependencies": {
7778
"nanoid": "^5.1.5"
7879
},
7980
"peerDependenciesMeta": {
81+
"valibot": {
82+
"optional": true
83+
},
8084
"zod": {
8185
"optional": true
8286
}

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function getIdRegex(
2+
prefix: string,
3+
separator: string,
4+
length: number,
5+
customAlphabets: string
6+
): RegExp {
7+
return new RegExp(`^${prefix}${separator}[${customAlphabets}]{${length}}$`);
8+
}

src/validators/validbot.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { IdHelper } from "../id-helper";
2+
import { custom as vCustom } from "valibot";
3+
import { getIdRegex } from "../utils";
4+
import type { GeneratedId, SeparatorOrDefault } from "../types";
5+
6+
export function createValibotIdSchema<
7+
P extends string,
8+
S extends string | undefined = undefined
9+
>(idHelper: IdHelper<P, S>) {
10+
const {
11+
separator = IdHelper.DEFAULT_SEPARATOR,
12+
length = IdHelper.DEFAULT_LENGTH,
13+
customAlphabets = IdHelper.DEFAULT_ALPHABETS,
14+
} = idHelper.options;
15+
16+
const idRegex = getIdRegex(
17+
idHelper.prefix,
18+
separator,
19+
length,
20+
customAlphabets
21+
);
22+
23+
return vCustom<GeneratedId<P, SeparatorOrDefault<S>>>(val => {
24+
return typeof val === "string" && idRegex.test(val);
25+
}, "Invalid ID Format");
26+
}

tests/validators/validbot.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { describe, it, expect } from "vitest";
2+
import { safeParse } from "valibot";
3+
import { IdHelper } from "../../src";
4+
import { createValibotIdSchema } from "../../src/validators/validbot";
5+
6+
describe("Valibot ID Validator", () => {
7+
it("should validate ID with default options", () => {
8+
const userIdHelper = new IdHelper("user");
9+
10+
const id = userIdHelper.generate();
11+
12+
const schema = createValibotIdSchema(userIdHelper);
13+
14+
// Test that the schema validates the generated ID
15+
expect(safeParse(schema, id)).toBe(true);
16+
17+
// Test that the schema rejects IDs with different prefix
18+
expect(safeParse(schema, "person_0123456789")).toBe(false);
19+
20+
// Test that the schema rejects IDs with different separator
21+
expect(safeParse(schema, "user::0123456789")).toBe(false);
22+
23+
// Test that the schema rejects IDs with different length
24+
expect(safeParse(schema, "user_1234")).toBe(false);
25+
});
26+
27+
it("should validate ID with custom options", () => {
28+
const userIdHelper = new IdHelper("user", {
29+
separator: "::",
30+
length: 12,
31+
customAlphabets: "abcdefghijklmnopqrstuvwxyz",
32+
});
33+
34+
const id = userIdHelper.generate();
35+
36+
const schema = createValibotIdSchema(userIdHelper);
37+
38+
// Test that the schema validates the generated ID
39+
expect(safeParse(schema, id)).toBe(true);
40+
41+
// Test that the schema rejects IDs with different prefix
42+
expect(safeParse(schema, "person::abcdefghijkl")).toBe(false);
43+
44+
// Test that the schema rejects IDs with different separator
45+
expect(safeParse(schema, "user_abcdefghijkl")).toBe(false);
46+
47+
// Test that the schema rejects IDs with different length
48+
expect(safeParse(schema, "user::abcdefghijk")).toBe(false);
49+
50+
// Test that the schema rejects IDs with different alphabets
51+
expect(safeParse(schema, "user::0123456789123")).toBe(false);
52+
});
53+
54+
it("should validate ID with custom separator", () => {
55+
const userIdHelper = new IdHelper("user", { separator: "::" });
56+
57+
const id = userIdHelper.generate();
58+
59+
const schema = createValibotIdSchema(userIdHelper);
60+
61+
// Test that the schema validates the generated ID
62+
expect(safeParse(schema, id)).toBe(true);
63+
64+
// Test that the schema rejects IDs with different prefix
65+
expect(safeParse(schema, "person::0123456789")).toBe(false);
66+
67+
// Test that the schema rejects IDs with different separator
68+
expect(safeParse(schema, "user_0123456789")).toBe(false);
69+
});
70+
71+
it("should validate ID with custom length", () => {
72+
const userIdHelper = new IdHelper("user", { length: 12 });
73+
74+
const id = userIdHelper.generate();
75+
76+
const schema = createValibotIdSchema(userIdHelper);
77+
78+
// Test that the schema validates the generated ID
79+
expect(safeParse(schema, id)).toBe(true);
80+
81+
// Test that the schema rejects IDs with different prefix
82+
expect(safeParse(schema, "person_0123456789")).toBe(false);
83+
84+
// Test that the schema rejects IDs with different length
85+
expect(safeParse(schema, "user_0123456789")).toBe(false);
86+
});
87+
88+
it("should validate ID with custom alphabets", () => {
89+
const userIdHelper = new IdHelper("user", {
90+
customAlphabets: "abcdefghijklmnopqrstuvwxyz",
91+
});
92+
93+
const id = userIdHelper.generate();
94+
95+
const schema = createValibotIdSchema(userIdHelper);
96+
97+
// Test that the schema validates the generated ID
98+
expect(safeParse(schema, id)).toBe(true);
99+
100+
// Test that the schema rejects IDs with different prefix
101+
expect(safeParse(schema, "person_0123456789")).toBe(false);
102+
103+
// Test that the schema rejects IDs with different alphabets
104+
expect(safeParse(schema, "user_0123456789123")).toBe(false);
105+
});
106+
});

0 commit comments

Comments
 (0)