feat: Move to filtered values

This commit is contained in:
BluJ
2023-05-01 16:44:08 -06:00
parent 04c8e23e21
commit f953160866
4 changed files with 24 additions and 25 deletions

View File

@@ -585,8 +585,13 @@ export class Value<Type, WD, ConfigType> {
default?: string | null default?: string | null
}, },
aVariants: Variants<Type, WrapperData, ConfigType>, aVariants: Variants<Type, WrapperData, ConfigType>,
getDisabledFn: LazyBuild<
WrapperData,
ConfigType,
Array<Type extends { unionSelectKey: infer B } ? B & string : never>
>,
) { ) {
return new Value<AsRequired<Type, Required>, WrapperData, ConfigType>( return new Value<Type | null | undefined, WrapperData, ConfigType>(
async (options) => ({ async (options) => ({
type: "union" as const, type: "union" as const,
description: null, description: null,
@@ -594,8 +599,9 @@ export class Value<Type, WD, ConfigType> {
...a, ...a,
variants: await aVariants.build(options as any), variants: await aVariants.build(options as any),
...requiredLikeToAbove(a.required), ...requiredLikeToAbove(a.required),
disabled: (await getDisabledFn(options)) || [],
}), }),
asRequiredParser(aVariants.validator, a), aVariants.validator.optional(),
) )
} }

View File

@@ -103,23 +103,4 @@ export class Variants<Type, WD, ConfigType> {
return variants return variants
}, validator) }, validator)
} }
/** Danger, don't filter everything!! */
disableVariants(
fn: LazyBuild<
WD,
ConfigType,
Array<Type extends { unionSelectKey: infer B } ? B : never>
>,
) {
const previousMe = this
return new Variants<Type, WD, ConfigType>(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)
}
} }

View File

@@ -93,6 +93,7 @@ export interface ValueSpecUnion extends WithStandalone {
spec: InputSpec spec: InputSpec
} }
> >
disabled?: string[]
required: boolean required: boolean
default: string | null default: string | null
} }

View File

@@ -507,7 +507,7 @@ describe("values", () => {
}) })
describe("filtering", () => { describe("filtering", () => {
test("union", async () => { test("union", async () => {
const value = Value.union( const value = Value.filteredUnion(
{ {
name: "Testing", name: "Testing",
required: { default: null }, required: { default: null },
@@ -538,11 +538,12 @@ describe("values", () => {
}), }),
}), }),
}, },
}).disableVariants(() => [ }),
() => [
"a", "a",
// @ts-expect-error // @ts-expect-error
"c", "c",
]), ],
) )
const validator = value.validator const validator = value.validator
validator.unsafeCast({ unionSelectKey: "a", unionValueKey: { b: false } }) validator.unsafeCast({ unionSelectKey: "a", unionValueKey: { b: false } })
@@ -551,6 +552,8 @@ describe("values", () => {
Test, Test,
| { unionSelectKey: "a"; unionValueKey: { b: boolean } } | { unionSelectKey: "a"; unionValueKey: { b: boolean } }
| { unionSelectKey: "b"; unionValueKey: { b: boolean } } | { unionSelectKey: "b"; unionValueKey: { b: boolean } }
| null
| undefined
>()(null) >()(null)
const built = await value.build({} as any) const built = await value.build({} as any)
@@ -560,13 +563,21 @@ describe("values", () => {
b: {}, b: {},
}, },
}) })
expect(built).not.toMatchObject({ expect(built).toMatchObject({
name: "Testing", name: "Testing",
variants: { variants: {
a: {}, a: {},
b: {}, b: {},
}, },
}) })
expect(built).toMatchObject({
name: "Testing",
variants: {
a: {},
b: {},
},
disabled: ["a", "c"],
})
}) })
}) })
}) })