From f953160866c94aedba1a7e008959b97b58176185 Mon Sep 17 00:00:00 2001 From: BluJ Date: Mon, 1 May 2023 16:44:08 -0600 Subject: [PATCH] feat: Move to filtered values --- lib/config/builder/value.ts | 10 ++++++++-- lib/config/builder/variants.ts | 19 ------------------- lib/config/configTypes.ts | 1 + lib/test/configBuilder.test.ts | 19 +++++++++++++++---- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/config/builder/value.ts b/lib/config/builder/value.ts index f84e236..36552d5 100644 --- a/lib/config/builder/value.ts +++ b/lib/config/builder/value.ts @@ -585,8 +585,13 @@ export class Value { default?: string | null }, aVariants: Variants, + getDisabledFn: LazyBuild< + WrapperData, + ConfigType, + Array + >, ) { - return new Value, WrapperData, ConfigType>( + return new Value( async (options) => ({ type: "union" as const, description: null, @@ -594,8 +599,9 @@ export class Value { ...a, variants: await aVariants.build(options as any), ...requiredLikeToAbove(a.required), + disabled: (await getDisabledFn(options)) || [], }), - asRequiredParser(aVariants.validator, a), + aVariants.validator.optional(), ) } diff --git a/lib/config/builder/variants.ts b/lib/config/builder/variants.ts index 5e5c112..0aa39a5 100644 --- a/lib/config/builder/variants.ts +++ b/lib/config/builder/variants.ts @@ -103,23 +103,4 @@ export class Variants { return variants }, validator) } - - /** Danger, don't filter everything!! */ - disableVariants( - fn: LazyBuild< - WD, - ConfigType, - Array - >, - ) { - const previousMe = this - return new Variants(async (options) => { - const answer = { ...(await previousMe.build(options)) } - const filterValues = await fn(options) - for (const key of filterValues) { - delete answer[key as any] - } - return answer - }, this.validator) - } } diff --git a/lib/config/configTypes.ts b/lib/config/configTypes.ts index 29a3899..68119bc 100644 --- a/lib/config/configTypes.ts +++ b/lib/config/configTypes.ts @@ -93,6 +93,7 @@ export interface ValueSpecUnion extends WithStandalone { spec: InputSpec } > + disabled?: string[] required: boolean default: string | null } diff --git a/lib/test/configBuilder.test.ts b/lib/test/configBuilder.test.ts index ccf72f1..25fb249 100644 --- a/lib/test/configBuilder.test.ts +++ b/lib/test/configBuilder.test.ts @@ -507,7 +507,7 @@ describe("values", () => { }) describe("filtering", () => { test("union", async () => { - const value = Value.union( + const value = Value.filteredUnion( { name: "Testing", required: { default: null }, @@ -538,11 +538,12 @@ describe("values", () => { }), }), }, - }).disableVariants(() => [ + }), + () => [ "a", // @ts-expect-error "c", - ]), + ], ) const validator = value.validator validator.unsafeCast({ unionSelectKey: "a", unionValueKey: { b: false } }) @@ -551,6 +552,8 @@ describe("values", () => { Test, | { unionSelectKey: "a"; unionValueKey: { b: boolean } } | { unionSelectKey: "b"; unionValueKey: { b: boolean } } + | null + | undefined >()(null) const built = await value.build({} as any) @@ -560,13 +563,21 @@ describe("values", () => { b: {}, }, }) - expect(built).not.toMatchObject({ + expect(built).toMatchObject({ name: "Testing", variants: { a: {}, b: {}, }, }) + expect(built).toMatchObject({ + name: "Testing", + variants: { + a: {}, + b: {}, + }, + disabled: ["a", "c"], + }) }) }) })