From e78295060ff30f05b64164a50e3dcb5e6fd94aa7 Mon Sep 17 00:00:00 2001 From: BluJ Date: Mon, 1 May 2023 15:30:57 -0600 Subject: [PATCH] chore: Update dynamic for lists --- lib/config/builder/list.ts | 94 ++++++++++++++++++++++++++++++++++ lib/test/configBuilder.test.ts | 39 ++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/lib/config/builder/list.ts b/lib/config/builder/list.ts index 1cf09aa..5822067 100644 --- a/lib/config/builder/list.ts +++ b/lib/config/builder/list.ts @@ -67,6 +67,54 @@ export class List { } }, arrayOf(string)) } + static dynamicText( + getA: LazyBuild< + WD, + CT, + { + name: string + description?: string | null + warning?: string | null + /** Default = [] */ + default?: string[] + minLength?: number | null + maxLength?: number | null + spec: { + /** Default = false */ + masked?: boolean + placeholder?: string | null + minLength?: number | null + maxLength?: number | null + patterns: Pattern[] + /** Default = "text" */ + inputmode?: ListValueSpecText["inputmode"] + } + } + >, + ) { + return new List(async (options) => { + const { spec: aSpec, ...a } = await getA(options) + const spec = { + type: "text" as const, + placeholder: null, + minLength: null, + maxLength: null, + masked: false, + inputmode: "text" as const, + ...aSpec, + } + return { + description: null, + warning: null, + default: [], + type: "list" as const, + minLength: null, + maxLength: null, + ...a, + spec, + } + }, arrayOf(string)) + } static number( a: { name: string @@ -108,6 +156,52 @@ export class List { } }, arrayOf(number)) } + static dynamicNumber( + getA: LazyBuild< + WD, + CT, + { + name: string + description?: string | null + warning?: string | null + /** Default = [] */ + default?: string[] + minLength?: number | null + maxLength?: number | null + spec: { + integer: boolean + min?: number | null + max?: number | null + step?: string | null + units?: string | null + placeholder?: string | null + } + } + >, + ) { + return new List(async (options) => { + const { spec: aSpec, ...a } = await getA(options) + const spec = { + type: "number" as const, + placeholder: null, + min: null, + max: null, + step: null, + units: null, + ...aSpec, + } + return { + description: null, + warning: null, + minLength: null, + maxLength: null, + default: [], + type: "list" as const, + ...a, + spec, + } + }, arrayOf(number)) + } static obj, WrapperData, ConfigType>( a: { name: string diff --git a/lib/test/configBuilder.test.ts b/lib/test/configBuilder.test.ts index d16c3d3..6626229 100644 --- a/lib/test/configBuilder.test.ts +++ b/lib/test/configBuilder.test.ts @@ -609,6 +609,45 @@ describe("Builder List", () => { validator.unsafeCast(["test", "text"]) testOutput()(null) }) + describe("dynamic", () => { + test("text", async () => { + const value = Value.list( + List.dynamicText(() => ({ + name: "test", + spec: { patterns: [] }, + })), + ) + const validator = value.validator + validator.unsafeCast(["test", "text"]) + expect(() => validator.unsafeCast([3,4])).toThrowError() + expect(() => validator.unsafeCast(null)).toThrowError() + testOutput()(null) + expect(await value.build({} as any)).toMatchObject({ + name: "test", + spec: { patterns: [] }, + }) + }) + }) + test("number", async () => { + const value = Value.list( + List.dynamicNumber(() => ({ + name: "test", + spec: { integer: true }, + })), + ) + const validator = value.validator + expect(() => + validator.unsafeCast(["test", "text"])).toThrowError() + validator.unsafeCast([4,2]) + expect(() => validator.unsafeCast(null)).toThrowError() + validator.unsafeCast([]) + testOutput()(null) + expect(await value.build({} as any)).toMatchObject({ + name: "test", + spec: { integer: true }, + }) + }) +}) }) describe("Nested nullable values", () => {