From 2bed621195e7723b0f132f2f1879b3d066b2d8b5 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 5 Apr 2023 10:46:08 -0600 Subject: [PATCH] unionSelectValue --- lib/config/builder/config-types.test.ts | 23 --- lib/config/builder/index.test.ts | 204 ------------------------ lib/config/config-types.ts | 5 +- 3 files changed, 4 insertions(+), 228 deletions(-) delete mode 100644 lib/config/builder/config-types.test.ts delete mode 100644 lib/config/builder/index.test.ts diff --git a/lib/config/builder/config-types.test.ts b/lib/config/builder/config-types.test.ts deleted file mode 100644 index 101aada..0000000 --- a/lib/config/builder/config-types.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { ListValueSpecOf, ValueSpecList, isValueSpecListOf } from "../config-types"; -import { Config } from "./config"; -import { List } from "./list"; -import { Value } from "./value"; - -describe("Config Types", () => { - test("isValueSpecListOf", () => { - const options = [List.obj, List.string, List.number]; - for (const option of options) { - const test = option({} as any, { spec: Config.of({}) } as any) as any; - const someList = Value.list(test).build(); - if (isValueSpecListOf(someList, "string")) { - someList.spec satisfies ListValueSpecOf<"string">; - } else if (isValueSpecListOf(someList, "number")) { - someList.spec satisfies ListValueSpecOf<"number">; - } else if (isValueSpecListOf(someList, "object")) { - someList.spec satisfies ListValueSpecOf<"object">; - } else { - throw new Error("Failed to figure out the type: " + JSON.stringify(someList)); - } - } - }); -}); diff --git a/lib/config/builder/index.test.ts b/lib/config/builder/index.test.ts deleted file mode 100644 index ab7660d..0000000 --- a/lib/config/builder/index.test.ts +++ /dev/null @@ -1,204 +0,0 @@ -import { testOutput } from "../../test/output.test"; -import { Config } from "./config"; -import { List } from "./list"; -import { Value } from "./value"; -import { Variants } from "./variants"; - -describe("builder tests", () => { - test("String", () => { - const bitcoinPropertiesBuilt: { - "peer-tor-address": { - name: string; - description: string | null; - type: "string"; - }; - } = Config.of({ - "peer-tor-address": Value.string({ - name: "Peer tor address", - default: "", - description: "The Tor address of the peer interface", - warning: null, - required: true, - masked: true, - placeholder: null, - pattern: null, - patternDescription: null, - }), - }).build(); - expect(JSON.stringify(bitcoinPropertiesBuilt)).toEqual( - /*json*/ `{ - "peer-tor-address": { - "type": "string", - "default": "", - "description": "The Tor address of the peer interface", - "warning": null, - "masked": true, - "placeholder": null, - "pattern": null, - "patternDescription": null, - "inputmode":"text", - "name": "Peer tor address", - "required": true - }}` - .replaceAll("\n", " ") - .replaceAll(/\s{2,}/g, "") - .replaceAll(": ", ":") - ); - }); -}); - -describe("values", () => { - test("boolean", () => { - const value = Value.boolean({ - name: "Testing", - }); - const validator = value.validator(); - validator.unsafeCast(false); - testOutput()(null); - }); - test("string", () => { - const value = Value.string({ - name: "Testing", - required: false, - }); - const validator = value.validator(); - validator.unsafeCast("test text"); - testOutput()(null); - }); - test("textarea", () => { - const value = Value.textarea({ - name: "Testing", - required: false, - }); - const validator = value.validator(); - validator.unsafeCast("test text"); - testOutput()(null); - }); - test("number", () => { - const value = Value.number({ - name: "Testing", - required: false, - integral: false, - }); - const validator = value.validator(); - validator.unsafeCast(2); - testOutput()(null); - }); - test("select", () => { - const value = Value.select({ - name: "Testing", - required: false, - values: { - a: "A", - b: "B", - }, - }); - const validator = value.validator(); - validator.unsafeCast("a"); - validator.unsafeCast("b"); - testOutput()(null); - }); - test("multiselect", () => { - const value = Value.multiselect({ - name: "Testing", - values: { - a: "A", - b: "B", - }, - }); - const validator = value.validator(); - validator.unsafeCast([]); - validator.unsafeCast(["a", "b"]); - testOutput>()(null); - }); - test("object", () => { - const value = Value.object({ - name: "Testing", - spec: Config.of({ - a: Value.boolean({ - name: "test", - }), - }), - }); - const validator = value.validator(); - validator.unsafeCast({ a: true }); - testOutput()(null); - }); - test("union", () => { - const value = Value.union( - { - name: "Testing", - required: true, - }, - Variants.of({ - a: { - name: "a", - spec: Config.of({ b: Value.boolean({ name: "b" }) }), - }, - }) - ); - const validator = value.validator(); - validator.unsafeCast({ unionSelectKey: "a", b: false }); - type Test = typeof validator._TYPE; - testOutput()(null); - }); - test("list", () => { - const value = Value.list( - List.number( - { - name: "test", - }, - { - integral: false, - } - ) - ); - const validator = value.validator(); - validator.unsafeCast([1, 2, 3]); - testOutput()(null); - }); -}); - -describe("Builder List", () => { - test("obj", () => { - const value = Value.list( - List.obj( - { - name: "test", - }, - { - spec: Config.of({ test: Value.boolean({ name: "test" }) }), - } - ) - ); - const validator = value.validator(); - validator.unsafeCast([{ test: true }]); - testOutput()(null); - }); - test("string", () => { - const value = Value.list( - List.string( - { - name: "test", - }, - {} - ) - ); - const validator = value.validator(); - validator.unsafeCast(["test", "text"]); - testOutput()(null); - }); - test("number", () => { - const value = Value.list( - List.number( - { - name: "test", - }, - { integral: true } - ) - ); - const validator = value.validator(); - validator.unsafeCast([12, 45]); - testOutput()(null); - }); -}); diff --git a/lib/config/config-types.ts b/lib/config/config-types.ts index 39a2cad..c83f19a 100644 --- a/lib/config/config-types.ts +++ b/lib/config/config-types.ts @@ -171,5 +171,8 @@ export type UniqueBy = export type DefaultString = string | { charset: string; len: number }; -export const unionSelectKey = "unionSelectKey" as const; +export declare const unionSelectKey: "unionSelectKey"; export type UnionSelectKey = typeof unionSelectKey; + +export declare const unionValueKey: "unionValueKey"; +export type UnionValueKey = typeof unionValueKey;