effect-zod.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import { zod } from "../../src/util/effect-zod"
  4. describe("util.effect-zod", () => {
  5. test("converts class schemas for route dto shapes", () => {
  6. class Method extends Schema.Class<Method>("ProviderAuthMethod")({
  7. type: Schema.Union([Schema.Literal("oauth"), Schema.Literal("api")]),
  8. label: Schema.String,
  9. }) {}
  10. const out = zod(Method)
  11. expect(out.meta()?.ref).toBe("ProviderAuthMethod")
  12. expect(
  13. out.parse({
  14. type: "oauth",
  15. label: "OAuth",
  16. }),
  17. ).toEqual({
  18. type: "oauth",
  19. label: "OAuth",
  20. })
  21. })
  22. test("converts structs with optional fields, arrays, and records", () => {
  23. const out = zod(
  24. Schema.Struct({
  25. foo: Schema.optional(Schema.String),
  26. bar: Schema.Array(Schema.Number),
  27. baz: Schema.Record(Schema.String, Schema.Boolean),
  28. }),
  29. )
  30. expect(
  31. out.parse({
  32. bar: [1, 2],
  33. baz: { ok: true },
  34. }),
  35. ).toEqual({
  36. bar: [1, 2],
  37. baz: { ok: true },
  38. })
  39. expect(
  40. out.parse({
  41. foo: "hi",
  42. bar: [1],
  43. baz: { ok: false },
  44. }),
  45. ).toEqual({
  46. foo: "hi",
  47. bar: [1],
  48. baz: { ok: false },
  49. })
  50. })
  51. test("throws for unsupported tuple schemas", () => {
  52. expect(() => zod(Schema.Tuple([Schema.String, Schema.Number]))).toThrow("unsupported effect schema")
  53. })
  54. })