|
| 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