chore: Update dynamic for lists

This commit is contained in:
BluJ
2023-05-01 15:30:57 -06:00
parent ec51315c23
commit e78295060f
2 changed files with 133 additions and 0 deletions

View File

@@ -67,6 +67,54 @@ export class List<Type, WD, ConfigType> {
}
}, arrayOf(string))
}
static dynamicText<WD, CT>(
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<string[], WD, CT>(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<WD, CT>(
a: {
name: string
@@ -108,6 +156,52 @@ export class List<Type, WD, ConfigType> {
}
}, arrayOf(number))
}
static dynamicNumber<WD, CT>(
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<number[], WD, CT>(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<Type extends Record<string, any>, WrapperData, ConfigType>(
a: {
name: string

View File

@@ -609,6 +609,45 @@ describe("Builder List", () => {
validator.unsafeCast(["test", "text"])
testOutput<typeof validator._TYPE, string[]>()(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<typeof validator._TYPE, string[]>()(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<typeof validator._TYPE, number[]>()(null)
expect(await value.build({} as any)).toMatchObject({
name: "test",
spec: { integer: true },
})
})
})
})
describe("Nested nullable values", () => {