|
| 1 | +import { S } from "effect-app" |
| 2 | +import { describe, expect, it } from "vitest" |
| 3 | +import { generateMetaFromSchema } from "../../src/components/OmegaForm/OmegaFormStuff" |
| 4 | + |
| 5 | +describe("optionalKey required handling", () => { |
| 6 | + it("should mark optionalKey fields as not required", () => { |
| 7 | + const schema = S.Struct({ |
| 8 | + number: S.optionalKey(S.Int.pipe(S.check(S.isBetween({ minimum: 1, maximum: 20 })))), |
| 9 | + height: S.NonEmptyString100.pipe(S.check(S.isMinLength(10))) |
| 10 | + }) |
| 11 | + |
| 12 | + const { meta } = generateMetaFromSchema(schema) |
| 13 | + |
| 14 | + // optionalKey field should NOT be required |
| 15 | + expect(meta.number?.required).toBe(false) |
| 16 | + |
| 17 | + // regular field should still be required |
| 18 | + expect(meta.height?.required).toBe(true) |
| 19 | + }) |
| 20 | + |
| 21 | + it("should mark optionalKey with decodingDefault as not required", () => { |
| 22 | + const schema = S.Struct({ |
| 23 | + name: S.optionalKey(S.String).pipe(S.withDecodingDefault(() => "defaultName")), |
| 24 | + age: S.NonEmptyString255 |
| 25 | + }) |
| 26 | + |
| 27 | + const { meta } = generateMetaFromSchema(schema) |
| 28 | + |
| 29 | + expect(meta.name?.required).toBe(false) |
| 30 | + expect(meta.age?.required).toBe(true) |
| 31 | + }) |
| 32 | + |
| 33 | + it("should handle optionalKey in tagged union branches", () => { |
| 34 | + const schema = S.Union([ |
| 35 | + S.TaggedStruct("one", { |
| 36 | + a: S.Struct({ |
| 37 | + number: S.optionalKey(S.Int.pipe(S.check(S.isBetween({ minimum: 1, maximum: 20 })))), |
| 38 | + height: S.NonEmptyString100.pipe(S.check(S.isMinLength(10))), |
| 39 | + z: S.NonEmptyString100.pipe(S.check(S.isMinLength(10))) |
| 40 | + }) |
| 41 | + }), |
| 42 | + S.TaggedStruct("two", { |
| 43 | + a: S.Struct({ |
| 44 | + number: S.optionalKey(S.Int.pipe(S.check(S.isBetween({ minimum: 1, maximum: 20 })))), |
| 45 | + height: S.NonEmptyString100.pipe(S.check(S.isMinLength(10))), |
| 46 | + y: S.NonEmptyString100.pipe(S.check(S.isMinLength(10))) |
| 47 | + }) |
| 48 | + }) |
| 49 | + ]) |
| 50 | + |
| 51 | + const { meta } = generateMetaFromSchema(schema) |
| 52 | + |
| 53 | + // optionalKey in both union branches should not be required |
| 54 | + expect(meta["a.number"]?.required).toBe(false) |
| 55 | + |
| 56 | + // regular fields should be required |
| 57 | + expect(meta["a.height"]?.required).toBe(true) |
| 58 | + expect(meta["a.z"]?.required).toBe(true) |
| 59 | + expect(meta["a.y"]?.required).toBe(true) |
| 60 | + }) |
| 61 | +}) |
0 commit comments