From 31352a72c36b6ce4a9d3f5f4b7cf3e3ed9521aa6 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Fri, 20 Feb 2026 16:24:35 -0700 Subject: [PATCH] chore: migrate from ts-matches to zod across all TypeScript packages --- container-runtime/package-lock.json | 13 +- container-runtime/package.json | 1 - .../src/Adapters/EffectCreator.ts | 42 +- container-runtime/src/Adapters/RpcListener.ts | 276 ++++++----- .../DockerProcedureContainer.ts | 8 +- .../Systems/SystemForEmbassy/index.ts | 215 +++++---- .../SystemForEmbassy/matchManifest.test.ts | 4 +- .../Systems/SystemForEmbassy/matchManifest.ts | 183 ++++---- .../Systems/SystemForEmbassy/matchVolume.ts | 42 +- .../transformConfigSpec.test.ts | 20 +- .../SystemForEmbassy/transformConfigSpec.ts | 439 ++++++++++-------- .../src/Models/DockerProcedure.ts | 48 +- container-runtime/src/Models/Duration.ts | 4 +- container-runtime/src/Models/JsonPath.ts | 18 +- sdk/Makefile | 9 +- .../lib/actions/input/builder/inputSpec.ts | 16 +- .../actions/input/builder/inputSpecTools.ts | 6 +- sdk/base/lib/actions/input/builder/list.ts | 14 +- sdk/base/lib/actions/input/builder/value.ts | 132 +++--- .../lib/actions/input/builder/variants.ts | 63 +-- sdk/base/lib/actions/setupActions.ts | 6 +- sdk/base/lib/index.ts | 2 +- sdk/base/lib/interfaces/Host.ts | 7 +- sdk/base/lib/util/deepEqual.ts | 6 +- sdk/base/lib/util/graph.ts | 1 - sdk/base/lib/util/splitCommand.ts | 4 +- sdk/base/package-lock.json | 19 +- sdk/base/package.json | 4 +- sdk/package/lib/health/HealthCheck.ts | 4 +- sdk/package/lib/index.ts | 4 +- sdk/package/lib/test/inputSpecBuilder.test.ts | 213 ++++----- sdk/package/lib/test/makeOutput.ts | 428 ----------------- sdk/package/lib/test/output.test.ts | 148 ------ sdk/package/lib/util/fileHelper.ts | 16 +- sdk/package/package-lock.json | 23 +- sdk/package/package.json | 18 +- sdk/package/scripts/oldSpecToBuilder.ts | 384 --------------- sdk/package/tsconfig.json | 2 +- web/package-lock.json | 11 +- web/package.json | 1 - 40 files changed, 963 insertions(+), 1891 deletions(-) delete mode 100644 sdk/package/lib/test/makeOutput.ts delete mode 100644 sdk/package/lib/test/output.test.ts delete mode 100644 sdk/package/scripts/oldSpecToBuilder.ts diff --git a/container-runtime/package-lock.json b/container-runtime/package-lock.json index dfdc22ae3..b9d142a3e 100644 --- a/container-runtime/package-lock.json +++ b/container-runtime/package-lock.json @@ -19,7 +19,6 @@ "lodash.merge": "^4.6.2", "mime": "^4.0.7", "node-fetch": "^3.1.0", - "ts-matches": "^6.3.2", "tslib": "^2.5.3", "typescript": "^5.1.3", "yaml": "^2.3.1" @@ -38,7 +37,7 @@ }, "../sdk/dist": { "name": "@start9labs/start-sdk", - "version": "0.4.0-beta.50", + "version": "0.4.0-beta.51", "license": "MIT", "dependencies": { "@iarna/toml": "^3.0.0", @@ -49,8 +48,8 @@ "ini": "^5.0.0", "isomorphic-fetch": "^3.0.0", "mime": "^4.0.7", - "ts-matches": "^6.3.2", - "yaml": "^2.7.1" + "yaml": "^2.7.1", + "zod": "^4.3.6" }, "devDependencies": { "@types/jest": "^29.4.0", @@ -6494,12 +6493,6 @@ } } }, - "node_modules/ts-matches": { - "version": "6.3.2", - "resolved": "https://registry.npmjs.org/ts-matches/-/ts-matches-6.3.2.tgz", - "integrity": "sha512-UhSgJymF8cLd4y0vV29qlKVCkQpUtekAaujXbQVc729FezS8HwqzepqvtjzQ3HboatIqN/Idor85O2RMwT7lIQ==", - "license": "MIT" - }, "node_modules/tslib": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", diff --git a/container-runtime/package.json b/container-runtime/package.json index 583f90f6c..7ded801d3 100644 --- a/container-runtime/package.json +++ b/container-runtime/package.json @@ -28,7 +28,6 @@ "lodash.merge": "^4.6.2", "mime": "^4.0.7", "node-fetch": "^3.1.0", - "ts-matches": "^6.3.2", "tslib": "^2.5.3", "typescript": "^5.1.3", "yaml": "^2.3.1" diff --git a/container-runtime/src/Adapters/EffectCreator.ts b/container-runtime/src/Adapters/EffectCreator.ts index daba0304e..c244347eb 100644 --- a/container-runtime/src/Adapters/EffectCreator.ts +++ b/container-runtime/src/Adapters/EffectCreator.ts @@ -3,33 +3,39 @@ import { types as T, utils, VersionRange, + z, } from "@start9labs/start-sdk" import * as net from "net" -import { object, string, number, literals, some, unknown } from "ts-matches" import { Effects } from "../Models/Effects" import { CallbackHolder } from "../Models/CallbackHolder" import { asError } from "@start9labs/start-sdk/base/lib/util" -const matchRpcError = object({ - error: object({ - code: number, - message: string, - data: some( - string, - object({ - details: string, - debug: string.nullable().optional(), - }), - ) +const matchRpcError = z.object({ + error: z.object({ + code: z.number(), + message: z.string(), + data: z + .union([ + z.string(), + z.object({ + details: z.string(), + debug: z.string().nullable().optional(), + }), + ]) .nullable() .optional(), }), }) -const testRpcError = matchRpcError.test -const testRpcResult = object({ - result: unknown, -}).test -type RpcError = typeof matchRpcError._TYPE +function testRpcError(v: unknown): v is RpcError { + return matchRpcError.safeParse(v).success +} +const matchRpcResult = z.object({ + result: z.unknown(), +}) +function testRpcResult(v: unknown): v is z.infer { + return matchRpcResult.safeParse(v).success +} +type RpcError = z.infer const SOCKET_PATH = "/media/startos/rpc/host.sock" let hostSystemId = 0 @@ -71,7 +77,7 @@ const rpcRoundFor = "Error in host RPC:", utils.asError({ method, params, error: res.error }), ) - if (string.test(res.error.data)) { + if (typeof res.error.data === "string") { message += ": " + res.error.data console.error(`Details: ${res.error.data}`) } else { diff --git a/container-runtime/src/Adapters/RpcListener.ts b/container-runtime/src/Adapters/RpcListener.ts index 5998bc4da..f9dd0fac2 100644 --- a/container-runtime/src/Adapters/RpcListener.ts +++ b/container-runtime/src/Adapters/RpcListener.ts @@ -1,25 +1,13 @@ // @ts-check import * as net from "net" -import { - object, - some, - string, - literal, - array, - number, - matches, - any, - shape, - anyOf, - literals, -} from "ts-matches" import { ExtendedVersion, types as T, utils, VersionRange, + z, } from "@start9labs/start-sdk" import * as fs from "fs" @@ -29,89 +17,92 @@ import { jsonPath, unNestPath } from "../Models/JsonPath" import { System } from "../Interfaces/System" import { makeEffects } from "./EffectCreator" type MaybePromise = T | Promise -export const matchRpcResult = anyOf( - object({ result: any }), - object({ - error: object({ - code: number, - message: string, - data: object({ - details: string.optional(), - debug: any.optional(), - }) +export const matchRpcResult = z.union([ + z.object({ result: z.any() }), + z.object({ + error: z.object({ + code: z.number(), + message: z.string(), + data: z + .object({ + details: z.string().optional(), + debug: z.any().optional(), + }) .nullable() .optional(), }), }), -) +]) -export type RpcResult = typeof matchRpcResult._TYPE +export type RpcResult = z.infer type SocketResponse = ({ jsonrpc: "2.0"; id: IdType } & RpcResult) | null const SOCKET_PARENT = "/media/startos/rpc" const SOCKET_PATH = "/media/startos/rpc/service.sock" const jsonrpc = "2.0" as const -const isResult = object({ result: any }).test +const isResultSchema = z.object({ result: z.any() }) +const isResult = (v: unknown): v is z.infer => + isResultSchema.safeParse(v).success -const idType = some(string, number, literal(null)) +const idType = z.union([z.string(), z.number(), z.literal(null)]) type IdType = null | string | number | undefined -const runType = object({ +const runType = z.object({ id: idType.optional(), - method: literal("execute"), - params: object({ - id: string, - procedure: string, - input: any, - timeout: number.nullable().optional(), + method: z.literal("execute"), + params: z.object({ + id: z.string(), + procedure: z.string(), + input: z.any(), + timeout: z.number().nullable().optional(), }), }) -const sandboxRunType = object({ +const sandboxRunType = z.object({ id: idType.optional(), - method: literal("sandbox"), - params: object({ - id: string, - procedure: string, - input: any, - timeout: number.nullable().optional(), + method: z.literal("sandbox"), + params: z.object({ + id: z.string(), + procedure: z.string(), + input: z.any(), + timeout: z.number().nullable().optional(), }), }) -const callbackType = object({ - method: literal("callback"), - params: object({ - id: number, - args: array, +const callbackType = z.object({ + method: z.literal("callback"), + params: z.object({ + id: z.number(), + args: z.array(z.unknown()), }), }) -const initType = object({ +const initType = z.object({ id: idType.optional(), - method: literal("init"), - params: object({ - id: string, - kind: literals("install", "update", "restore").nullable(), + method: z.literal("init"), + params: z.object({ + id: z.string(), + kind: z.enum(["install", "update", "restore"]).nullable(), }), }) -const startType = object({ +const startType = z.object({ id: idType.optional(), - method: literal("start"), + method: z.literal("start"), }) -const stopType = object({ +const stopType = z.object({ id: idType.optional(), - method: literal("stop"), + method: z.literal("stop"), }) -const exitType = object({ +const exitType = z.object({ id: idType.optional(), - method: literal("exit"), - params: object({ - id: string, - target: string.nullable(), + method: z.literal("exit"), + params: z.object({ + id: z.string(), + target: z.string().nullable(), }), }) -const evalType = object({ +const evalType = z.object({ id: idType.optional(), - method: literal("eval"), - params: object({ - script: string, + method: z.literal("eval"), + params: z.object({ + script: z.string(), }), }) @@ -144,7 +135,9 @@ const handleRpc = (id: IdType, result: Promise) => }, })) -const hasId = object({ id: idType }).test +const hasIdSchema = z.object({ id: idType }) +const hasId = (v: unknown): v is z.infer => + hasIdSchema.safeParse(v).success export class RpcListener { shouldExit = false unixSocketServer = net.createServer(async (server) => {}) @@ -246,40 +239,52 @@ export class RpcListener { } private dealWithInput(input: unknown): MaybePromise { - return matches(input) - .when(runType, async ({ id, params }) => { + const parsed = z.object({ method: z.string() }).safeParse(input) + if (!parsed.success) { + console.warn( + `Couldn't parse the following input ${JSON.stringify(input)}`, + ) + return { + jsonrpc, + id: (input as any)?.id, + error: { + code: -32602, + message: "invalid params", + data: { + details: JSON.stringify(input), + }, + }, + } + } + + switch (parsed.data.method) { + case "execute": { + const { id, params } = runType.parse(input) const system = this.system - const procedure = jsonPath.unsafeCast(params.procedure) - const { input, timeout, id: eventId } = params - const result = this.getResult( - procedure, - system, - eventId, - timeout, - input, - ) + const procedure = jsonPath.parse(params.procedure) + const { input: inp, timeout, id: eventId } = params + const result = this.getResult(procedure, system, eventId, timeout, inp) return handleRpc(id, result) - }) - .when(sandboxRunType, async ({ id, params }) => { + } + case "sandbox": { + const { id, params } = sandboxRunType.parse(input) const system = this.system - const procedure = jsonPath.unsafeCast(params.procedure) - const { input, timeout, id: eventId } = params - const result = this.getResult( - procedure, - system, - eventId, - timeout, - input, - ) + const procedure = jsonPath.parse(params.procedure) + const { input: inp, timeout, id: eventId } = params + const result = this.getResult(procedure, system, eventId, timeout, inp) return handleRpc(id, result) - }) - .when(callbackType, async ({ params: { id, args } }) => { + } + case "callback": { + const { + params: { id, args }, + } = callbackType.parse(input) this.callCallback(id, args) return null - }) - .when(startType, async ({ id }) => { + } + case "start": { + const { id } = startType.parse(input) const callbacks = this.callbacks?.getChild("main") || this.callbacks?.child("main") const effects = makeEffects({ @@ -290,8 +295,9 @@ export class RpcListener { id, this.system.start(effects).then((result) => ({ result })), ) - }) - .when(stopType, async ({ id }) => { + } + case "stop": { + const { id } = stopType.parse(input) return handleRpc( id, this.system.stop().then((result) => { @@ -300,8 +306,9 @@ export class RpcListener { return { result } }), ) - }) - .when(exitType, async ({ id, params }) => { + } + case "exit": { + const { id, params } = exitType.parse(input) return handleRpc( id, (async () => { @@ -323,8 +330,9 @@ export class RpcListener { } })().then((result) => ({ result })), ) - }) - .when(initType, async ({ id, params }) => { + } + case "init": { + const { id, params } = initType.parse(input) return handleRpc( id, (async () => { @@ -349,8 +357,9 @@ export class RpcListener { } })().then((result) => ({ result })), ) - }) - .when(evalType, async ({ id, params }) => { + } + case "eval": { + const { id, params } = evalType.parse(input) return handleRpc( id, (async () => { @@ -375,41 +384,28 @@ export class RpcListener { } })(), ) - }) - .when( - shape({ id: idType.optional(), method: string }), - ({ id, method }) => ({ + } + default: { + const { id, method } = z + .object({ id: idType.optional(), method: z.string() }) + .passthrough() + .parse(input) + return { jsonrpc, id, error: { code: -32601, - message: `Method not found`, + message: "Method not found", data: { details: method, }, }, - }), - ) - - .defaultToLazy(() => { - console.warn( - `Couldn't parse the following input ${JSON.stringify(input)}`, - ) - return { - jsonrpc, - id: (input as any)?.id, - error: { - code: -32602, - message: "invalid params", - data: { - details: JSON.stringify(input), - }, - }, } - }) + } + } } private getResult( - procedure: typeof jsonPath._TYPE, + procedure: z.infer, system: System, eventId: string, timeout: number | null | undefined, @@ -449,26 +445,18 @@ export class RpcListener { ) } } - })().then(ensureResultTypeShape, (error) => - matches(error) - .when( - object({ - error: string, - code: number.defaultTo(0), - }), - (error) => ({ - error: { - code: error.code, - message: error.error, - }, - }), - ) - .defaultToLazy(() => ({ - error: { - code: 0, - message: String(error), - }, - })), - ) + })().then(ensureResultTypeShape, (error) => { + const errorSchema = z.object({ + error: z.string(), + code: z.number().default(0), + }) + const parsed = errorSchema.safeParse(error) + if (parsed.success) { + return { + error: { code: parsed.data.code, message: parsed.data.error }, + } + } + return { error: { code: 0, message: String(error) } } + }) } } diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts index c18829d54..674f17556 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts @@ -2,7 +2,7 @@ import * as fs from "fs/promises" import * as cp from "child_process" import { SubContainer, types as T } from "@start9labs/start-sdk" import { promisify } from "util" -import { DockerProcedure, VolumeId } from "../../../Models/DockerProcedure" +import { DockerProcedure } from "../../../Models/DockerProcedure" import { Volume } from "./matchVolume" import { CommandOptions, @@ -28,7 +28,7 @@ export class DockerProcedureContainer extends Drop { effects: T.Effects, packageId: string, data: DockerProcedure, - volumes: { [id: VolumeId]: Volume }, + volumes: { [id: string]: Volume }, name: string, options: { subcontainer?: SubContainer } = {}, ) { @@ -47,7 +47,7 @@ export class DockerProcedureContainer extends Drop { effects: T.Effects, packageId: string, data: DockerProcedure, - volumes: { [id: VolumeId]: Volume }, + volumes: { [id: string]: Volume }, name: string, ) { const subcontainer = await SubContainerOwned.of( @@ -64,7 +64,7 @@ export class DockerProcedureContainer extends Drop { ? `${subcontainer.rootfs}${mounts[mount]}` : `${subcontainer.rootfs}/${mounts[mount]}` await fs.mkdir(path, { recursive: true }) - const volumeMount = volumes[mount] + const volumeMount: Volume = volumes[mount] if (volumeMount.type === "data") { await subcontainer.mount( Mounts.of().mountVolume({ diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts index 7786c4760..15a97178d 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts @@ -15,26 +15,11 @@ import { System } from "../../../Interfaces/System" import { matchManifest, Manifest } from "./matchManifest" import * as childProcess from "node:child_process" import { DockerProcedureContainer } from "./DockerProcedureContainer" +import { DockerProcedure } from "../../../Models/DockerProcedure" import { promisify } from "node:util" import * as U from "./oldEmbassyTypes" import { MainLoop } from "./MainLoop" -import { - matches, - boolean, - dictionary, - literal, - literals, - object, - string, - unknown, - any, - tuple, - number, - anyOf, - deferred, - Parser, - array, -} from "ts-matches" +import { z } from "@start9labs/start-sdk" import { AddSslOptions } from "@start9labs/start-sdk/base/lib/osBindings" import { BindOptionsByProtocol, @@ -57,6 +42,15 @@ function todo(): never { throw new Error("Not implemented") } +/** + * Local type for procedure values from the manifest. + * The manifest's zod schemas use ZodTypeAny casts that produce `unknown` in zod v4. + * This type restores the expected shape for type-safe property access. + */ +type Procedure = + | (DockerProcedure & { type: "docker" }) + | { type: "script"; args: unknown[] | null } + const MANIFEST_LOCATION = "/usr/lib/startos/package/embassyManifest.json" export const EMBASSY_JS_LOCATION = "/usr/lib/startos/package/embassy.js" @@ -65,26 +59,24 @@ const configFile = FileHelper.json( base: new Volume("embassy"), subpath: "config.json", }, - matches.any, + z.any(), ) const dependsOnFile = FileHelper.json( { base: new Volume("embassy"), subpath: "dependsOn.json", }, - dictionary([string, array(string)]), + z.record(z.string(), z.array(z.string())), ) -const matchResult = object({ - result: any, +const matchResult = z.object({ + result: z.any(), }) -const matchError = object({ - error: string, +const matchError = z.object({ + error: z.string(), }) -const matchErrorCode = object<{ - "error-code": [number, string] | readonly [number, string] -}>({ - "error-code": tuple(number, string), +const matchErrorCode = z.object({ + "error-code": z.tuple([z.number(), z.string()]), }) const assertNever = ( @@ -96,29 +88,34 @@ const assertNever = ( /** Should be changing the type for specific properties, and this is mostly a transformation for the old return types to the newer one. */ +function isMatchResult(a: unknown): a is z.infer { + return matchResult.safeParse(a).success +} +function isMatchError(a: unknown): a is z.infer { + return matchError.safeParse(a).success +} +function isMatchErrorCode(a: unknown): a is z.infer { + return matchErrorCode.safeParse(a).success +} const fromReturnType = (a: U.ResultType): A => { - if (matchResult.test(a)) { + if (isMatchResult(a)) { return a.result } - if (matchError.test(a)) { + if (isMatchError(a)) { console.info({ passedErrorStack: new Error().stack, error: a.error }) throw { error: a.error } } - if (matchErrorCode.test(a)) { + if (isMatchErrorCode(a)) { const [code, message] = a["error-code"] throw { error: message, code } } - return assertNever(a) + return assertNever(a as never) } -const matchSetResult = object({ - "depends-on": dictionary([string, array(string)]) - .nullable() - .optional(), - dependsOn: dictionary([string, array(string)]) - .nullable() - .optional(), - signal: literals( +const matchSetResult = z.object({ + "depends-on": z.record(z.string(), z.array(z.string())).nullable().optional(), + dependsOn: z.record(z.string(), z.array(z.string())).nullable().optional(), + signal: z.enum([ "SIGTERM", "SIGHUP", "SIGINT", @@ -151,7 +148,7 @@ const matchSetResult = object({ "SIGPWR", "SIGSYS", "SIGINFO", - ), + ]), }) type OldGetConfigRes = { @@ -233,33 +230,29 @@ const asProperty = (x: PackagePropertiesV2): PropertiesReturn => Object.fromEntries( Object.entries(x).map(([key, value]) => [key, asProperty_(value)]), ) -const [matchPackageProperties, setMatchPackageProperties] = - deferred() -const matchPackagePropertyObject: Parser = - object({ - value: matchPackageProperties, - type: literal("object"), - description: string, - }) +const matchPackagePropertyObject: z.ZodType = z.object({ + value: z.lazy(() => matchPackageProperties), + type: z.literal("object"), + description: z.string(), +}) -const matchPackagePropertyString: Parser = - object({ - type: literal("string"), - description: string.nullable().optional(), - value: string, - copyable: boolean.nullable().optional(), - qr: boolean.nullable().optional(), - masked: boolean.nullable().optional(), - }) -setMatchPackageProperties( - dictionary([ - string, - anyOf(matchPackagePropertyObject, matchPackagePropertyString), - ]), +const matchPackagePropertyString: z.ZodType = z.object({ + type: z.literal("string"), + description: z.string().nullable().optional(), + value: z.string(), + copyable: z.boolean().nullable().optional(), + qr: z.boolean().nullable().optional(), + masked: z.boolean().nullable().optional(), +}) +const matchPackageProperties: z.ZodType = z.lazy(() => + z.record( + z.string(), + z.union([matchPackagePropertyObject, matchPackagePropertyString]), + ), ) -const matchProperties = object({ - version: literal(2), +const matchProperties = z.object({ + version: z.literal(2), data: matchPackageProperties, }) @@ -303,7 +296,7 @@ export class SystemForEmbassy implements System { }) const manifestData = await fs.readFile(manifestLocation, "utf-8") return new SystemForEmbassy( - matchManifest.unsafeCast(JSON.parse(manifestData)), + matchManifest.parse(JSON.parse(manifestData)), moduleCode, ) } @@ -389,7 +382,9 @@ export class SystemForEmbassy implements System { delete this.currentRunning if (currentRunning) { await currentRunning.clean({ - timeout: fromDuration(this.manifest.main["sigterm-timeout"] || "30s"), + timeout: fromDuration( + (this.manifest.main["sigterm-timeout"] as any) || "30s", + ), }) } } @@ -623,7 +618,7 @@ export class SystemForEmbassy implements System { effects: Effects, timeoutMs: number | null, ): Promise { - const backup = this.manifest.backup.create + const backup = this.manifest.backup.create as Procedure if (backup.type === "docker") { const commands = [backup.entrypoint, ...backup.args] const container = await DockerProcedureContainer.of( @@ -656,7 +651,7 @@ export class SystemForEmbassy implements System { encoding: "utf-8", }) .catch((_) => null) - const restoreBackup = this.manifest.backup.restore + const restoreBackup = this.manifest.backup.restore as Procedure if (restoreBackup.type === "docker") { const commands = [restoreBackup.entrypoint, ...restoreBackup.args] const container = await DockerProcedureContainer.of( @@ -689,7 +684,7 @@ export class SystemForEmbassy implements System { effects: Effects, timeoutMs: number | null, ): Promise { - const config = this.manifest.config?.get + const config = this.manifest.config?.get as Procedure | undefined if (!config) return { spec: {} } if (config.type === "docker") { const commands = [config.entrypoint, ...config.args] @@ -731,7 +726,7 @@ export class SystemForEmbassy implements System { ) await updateConfig(effects, this.manifest, spec, newConfig) await configFile.write(effects, newConfig) - const setConfigValue = this.manifest.config?.set + const setConfigValue = this.manifest.config?.set as Procedure | undefined if (!setConfigValue) return if (setConfigValue.type === "docker") { const commands = [ @@ -746,7 +741,7 @@ export class SystemForEmbassy implements System { this.manifest.volumes, `Set Config - ${commands.join(" ")}`, ) - const answer = matchSetResult.unsafeCast( + const answer = matchSetResult.parse( JSON.parse( (await container.execFail(commands, timeoutMs)).stdout.toString(), ), @@ -759,7 +754,7 @@ export class SystemForEmbassy implements System { const method = moduleCode.setConfig if (!method) throw new Error("Expecting that the method setConfig exists") - const answer = matchSetResult.unsafeCast( + const answer = matchSetResult.parse( await method( polyfillEffects(effects, this.manifest), newConfig as U.Config, @@ -788,7 +783,11 @@ export class SystemForEmbassy implements System { const requiredDeps = { ...Object.fromEntries( Object.entries(this.manifest.dependencies ?? {}) - .filter(([k, v]) => v?.requirement.type === "required") + .filter( + ([k, v]) => + (v?.requirement as { type: string } | undefined)?.type === + "required", + ) .map((x) => [x[0], []]) || [], ), } @@ -856,7 +855,7 @@ export class SystemForEmbassy implements System { } if (migration) { - const [_, procedure] = migration + const [_, procedure] = migration as readonly [unknown, Procedure] if (procedure.type === "docker") { const commands = [procedure.entrypoint, ...procedure.args] const container = await DockerProcedureContainer.of( @@ -894,7 +893,10 @@ export class SystemForEmbassy implements System { effects: Effects, timeoutMs: number | null, ): Promise { - const setConfigValue = this.manifest.properties + const setConfigValue = this.manifest.properties as + | Procedure + | null + | undefined if (!setConfigValue) throw new Error("There is no properties") if (setConfigValue.type === "docker") { const commands = [setConfigValue.entrypoint, ...setConfigValue.args] @@ -905,7 +907,7 @@ export class SystemForEmbassy implements System { this.manifest.volumes, `Properties - ${commands.join(" ")}`, ) - const properties = matchProperties.unsafeCast( + const properties = matchProperties.parse( JSON.parse( (await container.execFail(commands, timeoutMs)).stdout.toString(), ), @@ -916,7 +918,7 @@ export class SystemForEmbassy implements System { const method = moduleCode.properties if (!method) throw new Error("Expecting that the method properties exists") - const properties = matchProperties.unsafeCast( + const properties = matchProperties.parse( await method(polyfillEffects(effects, this.manifest)).then( fromReturnType, ), @@ -931,7 +933,8 @@ export class SystemForEmbassy implements System { formData: unknown, timeoutMs: number | null, ): Promise { - const actionProcedure = this.manifest.actions?.[actionId]?.implementation + const actionProcedure = this.manifest.actions?.[actionId] + ?.implementation as Procedure | undefined const toActionResult = ({ message, value, @@ -998,7 +1001,9 @@ export class SystemForEmbassy implements System { oldConfig: unknown, timeoutMs: number | null, ): Promise { - const actionProcedure = this.manifest.dependencies?.[id]?.config?.check + const actionProcedure = this.manifest.dependencies?.[id]?.config?.check as + | Procedure + | undefined if (!actionProcedure) return { message: "Action not found", value: null } if (actionProcedure.type === "docker") { const commands = [ @@ -1090,40 +1095,50 @@ export class SystemForEmbassy implements System { } } -const matchPointer = object({ - type: literal("pointer"), +const matchPointer = z.object({ + type: z.literal("pointer"), }) -const matchPointerPackage = object({ - subtype: literal("package"), - target: literals("tor-key", "tor-address", "lan-address"), - "package-id": string, - interface: string, +const matchPointerPackage = z.object({ + subtype: z.literal("package"), + target: z.enum(["tor-key", "tor-address", "lan-address"]), + "package-id": z.string(), + interface: z.string(), }) -const matchPointerConfig = object({ - subtype: literal("package"), - target: literals("config"), - "package-id": string, - selector: string, - multi: boolean, +const matchPointerConfig = z.object({ + subtype: z.literal("package"), + target: z.enum(["config"]), + "package-id": z.string(), + selector: z.string(), + multi: z.boolean(), }) -const matchSpec = object({ - spec: object, +const matchSpec = z.object({ + spec: z.record(z.string(), z.unknown()), }) -const matchVariants = object({ variants: dictionary([string, unknown]) }) +const matchVariants = z.object({ variants: z.record(z.string(), z.unknown()) }) +function isMatchPointer(v: unknown): v is z.infer { + return matchPointer.safeParse(v).success +} +function isMatchSpec(v: unknown): v is z.infer { + return matchSpec.safeParse(v).success +} +function isMatchVariants(v: unknown): v is z.infer { + return matchVariants.safeParse(v).success +} function cleanSpecOfPointers(mutSpec: T): T { - if (!object.test(mutSpec)) return mutSpec + if (typeof mutSpec !== "object" || mutSpec === null) return mutSpec for (const key in mutSpec) { const value = mutSpec[key] - if (matchSpec.test(value)) value.spec = cleanSpecOfPointers(value.spec) - if (matchVariants.test(value)) + if (isMatchSpec(value)) + value.spec = cleanSpecOfPointers(value.spec) as Record + if (isMatchVariants(value)) value.variants = Object.fromEntries( Object.entries(value.variants).map(([key, value]) => [ key, cleanSpecOfPointers(value), ]), ) - if (!matchPointer.test(value)) continue + if (!isMatchPointer(value)) continue delete mutSpec[key] // // if (value.target === ) } @@ -1269,7 +1284,7 @@ function extractServiceInterfaceId(manifest: Manifest, specInterface: string) { } async function convertToNewConfig(value: OldGetConfigRes) { try { - const valueSpec: OldConfigSpec = matchOldConfigSpec.unsafeCast(value.spec) + const valueSpec: OldConfigSpec = matchOldConfigSpec.parse(value.spec) const spec = transformConfigSpec(valueSpec) if (!value.config) return { spec, config: null } const config = transformOldConfigToNew(valueSpec, value.config) ?? null diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.test.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.test.ts index 3730dd3b6..6173f00d0 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.test.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.test.ts @@ -4,9 +4,9 @@ import synapseManifest from "./__fixtures__/synapseManifest" describe("matchManifest", () => { test("gittea", () => { - matchManifest.unsafeCast(giteaManifest) + matchManifest.parse(giteaManifest) }) test("synapse", () => { - matchManifest.unsafeCast(synapseManifest) + matchManifest.parse(synapseManifest) }) }) diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.ts index 8c680cbd6..f3fe101eb 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchManifest.ts @@ -1,126 +1,121 @@ -import { - object, - literal, - string, - array, - boolean, - dictionary, - literals, - number, - unknown, - some, - every, -} from "ts-matches" +import { z } from "@start9labs/start-sdk" import { matchVolume } from "./matchVolume" import { matchDockerProcedure } from "../../../Models/DockerProcedure" -const matchJsProcedure = object({ - type: literal("script"), - args: array(unknown).nullable().optional().defaultTo([]), +const matchJsProcedure = z.object({ + type: z.literal("script"), + args: z.array(z.unknown()).nullable().optional().default([]), }) -const matchProcedure = some(matchDockerProcedure, matchJsProcedure) -export type Procedure = typeof matchProcedure._TYPE +const matchProcedure = z.union([matchDockerProcedure, matchJsProcedure]) +export type Procedure = z.infer -const matchAction = object({ - name: string, - description: string, - warning: string.nullable().optional(), +const matchAction = z.object({ + name: z.string(), + description: z.string(), + warning: z.string().nullable().optional(), implementation: matchProcedure, - "allowed-statuses": array(literals("running", "stopped")), - "input-spec": unknown.nullable().optional(), + "allowed-statuses": z.array(z.enum(["running", "stopped"])), + "input-spec": z.unknown().nullable().optional(), }) -export const matchManifest = object({ - id: string, - title: string, - version: string, +export const matchManifest = z.object({ + id: z.string(), + title: z.string(), + version: z.string(), main: matchDockerProcedure, - assets: object({ - assets: string.nullable().optional(), - scripts: string.nullable().optional(), - }) + assets: z + .object({ + assets: z.string().nullable().optional(), + scripts: z.string().nullable().optional(), + }) .nullable() .optional(), - "health-checks": dictionary([ - string, - every( + "health-checks": z.record( + z.string(), + z.intersection( matchProcedure, - object({ - name: string, - ["success-message"]: string.nullable().optional(), + z.object({ + name: z.string(), + "success-message": z.string().nullable().optional(), }), ), - ]), - config: object({ - get: matchProcedure, - set: matchProcedure, - }) + ), + config: z + .object({ + get: matchProcedure, + set: matchProcedure, + }) .nullable() .optional(), properties: matchProcedure.nullable().optional(), - volumes: dictionary([string, matchVolume]), - interfaces: dictionary([ - string, - object({ - name: string, - description: string, - "tor-config": object({ - "port-mapping": dictionary([string, string]), - }) + volumes: z.record(z.string(), matchVolume), + interfaces: z.record( + z.string(), + z.object({ + name: z.string(), + description: z.string(), + "tor-config": z + .object({ + "port-mapping": z.record(z.string(), z.string()), + }) .nullable() .optional(), - "lan-config": dictionary([ - string, - object({ - ssl: boolean, - internal: number, - }), - ]) + "lan-config": z + .record( + z.string(), + z.object({ + ssl: z.boolean(), + internal: z.number(), + }), + ) .nullable() .optional(), - ui: boolean, - protocols: array(string), + ui: z.boolean(), + protocols: z.array(z.string()), }), - ]), - backup: object({ + ), + backup: z.object({ create: matchProcedure, restore: matchProcedure, }), - migrations: object({ - to: dictionary([string, matchProcedure]), - from: dictionary([string, matchProcedure]), - }) + migrations: z + .object({ + to: z.record(z.string(), matchProcedure), + from: z.record(z.string(), matchProcedure), + }) .nullable() .optional(), - dependencies: dictionary([ - string, - object({ - version: string, - requirement: some( - object({ - type: literal("opt-in"), - how: string, - }), - object({ - type: literal("opt-out"), - how: string, - }), - object({ - type: literal("required"), - }), - ), - description: string.nullable().optional(), - config: object({ - check: matchProcedure, - "auto-configure": matchProcedure, + dependencies: z.record( + z.string(), + z + .object({ + version: z.string(), + requirement: z.union([ + z.object({ + type: z.literal("opt-in"), + how: z.string(), + }), + z.object({ + type: z.literal("opt-out"), + how: z.string(), + }), + z.object({ + type: z.literal("required"), + }), + ]), + description: z.string().nullable().optional(), + config: z + .object({ + check: matchProcedure, + "auto-configure": matchProcedure, + }) + .nullable() + .optional(), }) - .nullable() - .optional(), - }) .nullable() .optional(), - ]), + ), - actions: dictionary([string, matchAction]), + actions: z.record(z.string(), matchAction), }) -export type Manifest = typeof matchManifest._TYPE +export type Manifest = z.infer diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchVolume.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchVolume.ts index baffbdd12..ba17d007b 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchVolume.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/matchVolume.ts @@ -1,32 +1,32 @@ -import { object, literal, string, boolean, some } from "ts-matches" +import { z } from "@start9labs/start-sdk" -const matchDataVolume = object({ - type: literal("data"), - readonly: boolean.optional(), +const matchDataVolume = z.object({ + type: z.literal("data"), + readonly: z.boolean().optional(), }) -const matchAssetVolume = object({ - type: literal("assets"), +const matchAssetVolume = z.object({ + type: z.literal("assets"), }) -const matchPointerVolume = object({ - type: literal("pointer"), - "package-id": string, - "volume-id": string, - path: string, - readonly: boolean, +const matchPointerVolume = z.object({ + type: z.literal("pointer"), + "package-id": z.string(), + "volume-id": z.string(), + path: z.string(), + readonly: z.boolean(), }) -const matchCertificateVolume = object({ - type: literal("certificate"), - "interface-id": string, +const matchCertificateVolume = z.object({ + type: z.literal("certificate"), + "interface-id": z.string(), }) -const matchBackupVolume = object({ - type: literal("backup"), - readonly: boolean, +const matchBackupVolume = z.object({ + type: z.literal("backup"), + readonly: z.boolean(), }) -export const matchVolume = some( +export const matchVolume = z.union([ matchDataVolume, matchAssetVolume, matchPointerVolume, matchCertificateVolume, matchBackupVolume, -) -export type Volume = typeof matchVolume._TYPE +]) +export type Volume = z.infer diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.test.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.test.ts index f29ff94de..cebcb595b 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.test.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.test.ts @@ -12,43 +12,43 @@ import nostrConfig2 from "./__fixtures__/nostrConfig2" describe("transformConfigSpec", () => { test("matchOldConfigSpec(embassyPages.homepage.variants[web-page])", () => { - matchOldConfigSpec.unsafeCast( + matchOldConfigSpec.parse( fixtureEmbassyPagesConfig.homepage.variants["web-page"], ) }) test("matchOldConfigSpec(embassyPages)", () => { - matchOldConfigSpec.unsafeCast(fixtureEmbassyPagesConfig) + matchOldConfigSpec.parse(fixtureEmbassyPagesConfig) }) test("transformConfigSpec(embassyPages)", () => { - const spec = matchOldConfigSpec.unsafeCast(fixtureEmbassyPagesConfig) + const spec = matchOldConfigSpec.parse(fixtureEmbassyPagesConfig) expect(transformConfigSpec(spec)).toMatchSnapshot() }) test("matchOldConfigSpec(RTL.nodes)", () => { - matchOldValueSpecList.unsafeCast(fixtureRTLConfig.nodes) + matchOldValueSpecList.parse(fixtureRTLConfig.nodes) }) test("matchOldConfigSpec(RTL)", () => { - matchOldConfigSpec.unsafeCast(fixtureRTLConfig) + matchOldConfigSpec.parse(fixtureRTLConfig) }) test("transformConfigSpec(RTL)", () => { - const spec = matchOldConfigSpec.unsafeCast(fixtureRTLConfig) + const spec = matchOldConfigSpec.parse(fixtureRTLConfig) expect(transformConfigSpec(spec)).toMatchSnapshot() }) test("transformConfigSpec(searNXG)", () => { - const spec = matchOldConfigSpec.unsafeCast(searNXG) + const spec = matchOldConfigSpec.parse(searNXG) expect(transformConfigSpec(spec)).toMatchSnapshot() }) test("transformConfigSpec(bitcoind)", () => { - const spec = matchOldConfigSpec.unsafeCast(bitcoind) + const spec = matchOldConfigSpec.parse(bitcoind) expect(transformConfigSpec(spec)).toMatchSnapshot() }) test("transformConfigSpec(nostr)", () => { - const spec = matchOldConfigSpec.unsafeCast(nostr) + const spec = matchOldConfigSpec.parse(nostr) expect(transformConfigSpec(spec)).toMatchSnapshot() }) test("transformConfigSpec(nostr2)", () => { - const spec = matchOldConfigSpec.unsafeCast(nostrConfig2) + const spec = matchOldConfigSpec.parse(nostrConfig2) expect(transformConfigSpec(spec)).toMatchSnapshot() }) }) diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.ts index 77d611c95..22e56bc6b 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/transformConfigSpec.ts @@ -1,19 +1,4 @@ -import { IST } from "@start9labs/start-sdk" -import { - dictionary, - object, - anyOf, - string, - literals, - array, - number, - boolean, - Parser, - deferred, - every, - nill, - literal, -} from "ts-matches" +import { IST, z } from "@start9labs/start-sdk" export function transformConfigSpec(oldSpec: OldConfigSpec): IST.InputSpec { return Object.entries(oldSpec).reduce((inputSpec, [key, oldVal]) => { @@ -82,7 +67,7 @@ export function transformConfigSpec(oldSpec: OldConfigSpec): IST.InputSpec { name: oldVal.name, description: oldVal.description || null, warning: oldVal.warning || null, - spec: transformConfigSpec(matchOldConfigSpec.unsafeCast(oldVal.spec)), + spec: transformConfigSpec(matchOldConfigSpec.parse(oldVal.spec)), } } else if (oldVal.type === "string") { newVal = { @@ -121,7 +106,7 @@ export function transformConfigSpec(oldSpec: OldConfigSpec): IST.InputSpec { ...obj, [id]: { name: oldVal.tag["variant-names"][id] || id, - spec: transformConfigSpec(matchOldConfigSpec.unsafeCast(spec)), + spec: transformConfigSpec(matchOldConfigSpec.parse(spec)), }, }), {} as Record, @@ -153,7 +138,7 @@ export function transformOldConfigToNew( if (isObject(val)) { newVal = transformOldConfigToNew( - matchOldConfigSpec.unsafeCast(val.spec), + matchOldConfigSpec.parse(val.spec), config[key], ) } @@ -172,7 +157,7 @@ export function transformOldConfigToNew( newVal = { selection, value: transformOldConfigToNew( - matchOldConfigSpec.unsafeCast(val.variants[selection]), + matchOldConfigSpec.parse(val.variants[selection]), config[key], ), } @@ -183,10 +168,7 @@ export function transformOldConfigToNew( if (isObjectList(val)) { newVal = (config[key] as object[]).map((obj) => - transformOldConfigToNew( - matchOldConfigSpec.unsafeCast(val.spec.spec), - obj, - ), + transformOldConfigToNew(matchOldConfigSpec.parse(val.spec.spec), obj), ) } else if (isUnionList(val)) return obj } @@ -212,7 +194,7 @@ export function transformNewConfigToOld( if (isObject(val)) { newVal = transformNewConfigToOld( - matchOldConfigSpec.unsafeCast(val.spec), + matchOldConfigSpec.parse(val.spec), config[key], ) } @@ -221,7 +203,7 @@ export function transformNewConfigToOld( newVal = { [val.tag.id]: config[key].selection, ...transformNewConfigToOld( - matchOldConfigSpec.unsafeCast(val.variants[config[key].selection]), + matchOldConfigSpec.parse(val.variants[config[key].selection]), config[key].value, ), } @@ -230,10 +212,7 @@ export function transformNewConfigToOld( if (isList(val)) { if (isObjectList(val)) { newVal = (config[key] as object[]).map((obj) => - transformNewConfigToOld( - matchOldConfigSpec.unsafeCast(val.spec.spec), - obj, - ), + transformNewConfigToOld(matchOldConfigSpec.parse(val.spec.spec), obj), ) } else if (isUnionList(val)) return obj } @@ -337,9 +316,7 @@ function getListSpec( default: oldVal.default as Record[], spec: { type: "object", - spec: transformConfigSpec( - matchOldConfigSpec.unsafeCast(oldVal.spec.spec), - ), + spec: transformConfigSpec(matchOldConfigSpec.parse(oldVal.spec.spec)), uniqueBy: oldVal.spec["unique-by"] || null, displayAs: oldVal.spec["display-as"] || null, }, @@ -393,211 +370,281 @@ function isUnionList( } export type OldConfigSpec = Record -const [_matchOldConfigSpec, setMatchOldConfigSpec] = deferred() -export const matchOldConfigSpec = _matchOldConfigSpec as Parser< - unknown, - OldConfigSpec -> -export const matchOldDefaultString = anyOf( - string, - object({ charset: string, len: number }), +export const matchOldConfigSpec: z.ZodType = z.lazy(() => + z.record(z.string(), matchOldValueSpec), ) -type OldDefaultString = typeof matchOldDefaultString._TYPE +export const matchOldDefaultString = z.union([ + z.string(), + z.object({ charset: z.string(), len: z.number() }), +]) +type OldDefaultString = z.infer -export const matchOldValueSpecString = object({ - type: literals("string"), - name: string, - masked: boolean.nullable().optional(), - copyable: boolean.nullable().optional(), - nullable: boolean.nullable().optional(), - placeholder: string.nullable().optional(), - pattern: string.nullable().optional(), - "pattern-description": string.nullable().optional(), +export const matchOldValueSpecString = z.object({ + type: z.enum(["string"]), + name: z.string(), + masked: z.boolean().nullable().optional(), + copyable: z.boolean().nullable().optional(), + nullable: z.boolean().nullable().optional(), + placeholder: z.string().nullable().optional(), + pattern: z.string().nullable().optional(), + "pattern-description": z.string().nullable().optional(), default: matchOldDefaultString.nullable().optional(), - textarea: boolean.nullable().optional(), - description: string.nullable().optional(), - warning: string.nullable().optional(), + textarea: z.boolean().nullable().optional(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), }) -export const matchOldValueSpecNumber = object({ - type: literals("number"), - nullable: boolean, - name: string, - range: string, - integral: boolean, - default: number.nullable().optional(), - description: string.nullable().optional(), - warning: string.nullable().optional(), - units: string.nullable().optional(), - placeholder: anyOf(number, string).nullable().optional(), +export const matchOldValueSpecNumber = z.object({ + type: z.enum(["number"]), + nullable: z.boolean(), + name: z.string(), + range: z.string(), + integral: z.boolean(), + default: z.number().nullable().optional(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), + units: z.string().nullable().optional(), + placeholder: z.union([z.number(), z.string()]).nullable().optional(), }) -type OldValueSpecNumber = typeof matchOldValueSpecNumber._TYPE +type OldValueSpecNumber = z.infer -export const matchOldValueSpecBoolean = object({ - type: literals("boolean"), - default: boolean, - name: string, - description: string.nullable().optional(), - warning: string.nullable().optional(), +export const matchOldValueSpecBoolean = z.object({ + type: z.enum(["boolean"]), + default: z.boolean(), + name: z.string(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), }) -type OldValueSpecBoolean = typeof matchOldValueSpecBoolean._TYPE +type OldValueSpecBoolean = z.infer -const matchOldValueSpecObject = object({ - type: literals("object"), - spec: _matchOldConfigSpec, - name: string, - description: string.nullable().optional(), - warning: string.nullable().optional(), +type OldValueSpecObject = { + type: "object" + spec: OldConfigSpec + name: string + description?: string | null + warning?: string | null +} +const matchOldValueSpecObject: z.ZodType = z.object({ + type: z.enum(["object"]), + spec: z.lazy(() => matchOldConfigSpec), + name: z.string(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), }) -type OldValueSpecObject = typeof matchOldValueSpecObject._TYPE -const matchOldValueSpecEnum = object({ - values: array(string), - "value-names": dictionary([string, string]), - type: literals("enum"), - default: string, - name: string, - description: string.nullable().optional(), - warning: string.nullable().optional(), +const matchOldValueSpecEnum = z.object({ + values: z.array(z.string()), + "value-names": z.record(z.string(), z.string()), + type: z.enum(["enum"]), + default: z.string(), + name: z.string(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), }) -type OldValueSpecEnum = typeof matchOldValueSpecEnum._TYPE +type OldValueSpecEnum = z.infer -const matchOldUnionTagSpec = object({ - id: string, // The name of the field containing one of the union variants - "variant-names": dictionary([string, string]), // The name of each variant - name: string, - description: string.nullable().optional(), - warning: string.nullable().optional(), +const matchOldUnionTagSpec = z.object({ + id: z.string(), // The name of the field containing one of the union variants + "variant-names": z.record(z.string(), z.string()), // The name of each variant + name: z.string(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), }) -const matchOldValueSpecUnion = object({ - type: literals("union"), +type OldValueSpecUnion = { + type: "union" + tag: z.infer + variants: Record + default: string +} +const matchOldValueSpecUnion: z.ZodType = z.object({ + type: z.enum(["union"]), tag: matchOldUnionTagSpec, - variants: dictionary([string, _matchOldConfigSpec]), - default: string, + variants: z.record( + z.string(), + z.lazy(() => matchOldConfigSpec), + ), + default: z.string(), }) -type OldValueSpecUnion = typeof matchOldValueSpecUnion._TYPE -const [matchOldUniqueBy, setOldUniqueBy] = deferred() type OldUniqueBy = | null | string | { any: OldUniqueBy[] } | { all: OldUniqueBy[] } -setOldUniqueBy( - anyOf( - nill, - string, - object({ any: array(matchOldUniqueBy) }), - object({ all: array(matchOldUniqueBy) }), - ), +const matchOldUniqueBy: z.ZodType = z.lazy(() => + z.union([ + z.null(), + z.string(), + z.object({ any: z.array(matchOldUniqueBy) }), + z.object({ all: z.array(matchOldUniqueBy) }), + ]), ) -const matchOldListValueSpecObject = object({ - spec: _matchOldConfigSpec, // this is a mapped type of the config object at this level, replacing the object's values with specs on those values - "unique-by": matchOldUniqueBy.nullable().optional(), // indicates whether duplicates can be permitted in the list - "display-as": string.nullable().optional(), // this should be a handlebars template which can make use of the entire config which corresponds to 'spec' -}) -const matchOldListValueSpecUnion = object({ +type OldListValueSpecObject = { + spec: OldConfigSpec + "unique-by"?: OldUniqueBy | null + "display-as"?: string | null +} +const matchOldListValueSpecObject: z.ZodType = z.object( + { + spec: z.lazy(() => matchOldConfigSpec), // this is a mapped type of the config object at this level, replacing the object's values with specs on those values + "unique-by": matchOldUniqueBy.nullable().optional(), // indicates whether duplicates can be permitted in the list + "display-as": z.string().nullable().optional(), // this should be a handlebars template which can make use of the entire config which corresponds to 'spec' + }, +) +type OldListValueSpecUnion = { + "unique-by"?: OldUniqueBy | null + "display-as"?: string | null + tag: z.infer + variants: Record +} +const matchOldListValueSpecUnion: z.ZodType = z.object({ "unique-by": matchOldUniqueBy.nullable().optional(), - "display-as": string.nullable().optional(), + "display-as": z.string().nullable().optional(), tag: matchOldUnionTagSpec, - variants: dictionary([string, _matchOldConfigSpec]), + variants: z.record( + z.string(), + z.lazy(() => matchOldConfigSpec), + ), }) -const matchOldListValueSpecString = object({ - masked: boolean.nullable().optional(), - copyable: boolean.nullable().optional(), - pattern: string.nullable().optional(), - "pattern-description": string.nullable().optional(), - placeholder: string.nullable().optional(), +const matchOldListValueSpecString = z.object({ + masked: z.boolean().nullable().optional(), + copyable: z.boolean().nullable().optional(), + pattern: z.string().nullable().optional(), + "pattern-description": z.string().nullable().optional(), + placeholder: z.string().nullable().optional(), }) -const matchOldListValueSpecEnum = object({ - values: array(string), - "value-names": dictionary([string, string]), +const matchOldListValueSpecEnum = z.object({ + values: z.array(z.string()), + "value-names": z.record(z.string(), z.string()), }) -const matchOldListValueSpecNumber = object({ - range: string, - integral: boolean, - units: string.nullable().optional(), - placeholder: anyOf(number, string).nullable().optional(), +const matchOldListValueSpecNumber = z.object({ + range: z.string(), + integral: z.boolean(), + units: z.string().nullable().optional(), + placeholder: z.union([z.number(), z.string()]).nullable().optional(), }) +type OldValueSpecListBase = { + type: "list" + range: string + default: string[] | number[] | OldDefaultString[] | Record[] + name: string + description?: string | null + warning?: string | null +} + +type OldValueSpecList = OldValueSpecListBase & + ( + | { subtype: "string"; spec: z.infer } + | { subtype: "enum"; spec: z.infer } + | { subtype: "object"; spec: OldListValueSpecObject } + | { subtype: "number"; spec: z.infer } + | { subtype: "union"; spec: OldListValueSpecUnion } + ) + // represents a spec for a list -export const matchOldValueSpecList = every( - object({ - type: literals("list"), - range: string, // '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules - default: anyOf( - array(string), - array(number), - array(matchOldDefaultString), - array(object), - ), - name: string, - description: string.nullable().optional(), - warning: string.nullable().optional(), - }), - anyOf( - object({ - subtype: literals("string"), - spec: matchOldListValueSpecString, +export const matchOldValueSpecList: z.ZodType = + z.intersection( + z.object({ + type: z.enum(["list"]), + range: z.string(), // '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules + default: z.union([ + z.array(z.string()), + z.array(z.number()), + z.array(matchOldDefaultString), + z.array(z.object({}).passthrough()), + ]), + name: z.string(), + description: z.string().nullable().optional(), + warning: z.string().nullable().optional(), }), - object({ - subtype: literals("enum"), - spec: matchOldListValueSpecEnum, - }), - object({ - subtype: literals("object"), - spec: matchOldListValueSpecObject, - }), - object({ - subtype: literals("number"), - spec: matchOldListValueSpecNumber, - }), - object({ - subtype: literals("union"), - spec: matchOldListValueSpecUnion, - }), - ), -) -type OldValueSpecList = typeof matchOldValueSpecList._TYPE + z.union([ + z.object({ + subtype: z.enum(["string"]), + spec: matchOldListValueSpecString, + }), + z.object({ + subtype: z.enum(["enum"]), + spec: matchOldListValueSpecEnum, + }), + z.object({ + subtype: z.enum(["object"]), + spec: matchOldListValueSpecObject, + }), + z.object({ + subtype: z.enum(["number"]), + spec: matchOldListValueSpecNumber, + }), + z.object({ + subtype: z.enum(["union"]), + spec: matchOldListValueSpecUnion, + }), + ]), + ) as unknown as z.ZodType -const matchOldValueSpecPointer = every( - object({ - type: literal("pointer"), - }), - anyOf( - object({ - subtype: literal("package"), - target: literals("tor-key", "tor-address", "lan-address"), - "package-id": string, - interface: string, - }), - object({ - subtype: literal("package"), - target: literals("config"), - "package-id": string, - selector: string, - multi: boolean, - }), - ), +type OldValueSpecPointer = { + type: "pointer" +} & ( + | { + subtype: "package" + target: "tor-key" | "tor-address" | "lan-address" + "package-id": string + interface: string + } + | { + subtype: "package" + target: "config" + "package-id": string + selector: string + multi: boolean + } ) -type OldValueSpecPointer = typeof matchOldValueSpecPointer._TYPE +const matchOldValueSpecPointer: z.ZodType = z.intersection( + z.object({ + type: z.literal("pointer"), + }), + z.union([ + z.object({ + subtype: z.literal("package"), + target: z.enum(["tor-key", "tor-address", "lan-address"]), + "package-id": z.string(), + interface: z.string(), + }), + z.object({ + subtype: z.literal("package"), + target: z.enum(["config"]), + "package-id": z.string(), + selector: z.string(), + multi: z.boolean(), + }), + ]), +) as unknown as z.ZodType -export const matchOldValueSpec = anyOf( +type OldValueSpecString = z.infer + +type OldValueSpec = + | OldValueSpecString + | OldValueSpecNumber + | OldValueSpecBoolean + | OldValueSpecObject + | OldValueSpecEnum + | OldValueSpecList + | OldValueSpecUnion + | OldValueSpecPointer + +export const matchOldValueSpec: z.ZodType = z.union([ matchOldValueSpecString, matchOldValueSpecNumber, matchOldValueSpecBoolean, - matchOldValueSpecObject, + matchOldValueSpecObject as z.ZodType, matchOldValueSpecEnum, - matchOldValueSpecList, - matchOldValueSpecUnion, - matchOldValueSpecPointer, -) -type OldValueSpec = typeof matchOldValueSpec._TYPE - -setMatchOldConfigSpec(dictionary([string, matchOldValueSpec])) + matchOldValueSpecList as z.ZodType, + matchOldValueSpecUnion as z.ZodType, + matchOldValueSpecPointer as z.ZodType, +]) export class Range { min?: number diff --git a/container-runtime/src/Models/DockerProcedure.ts b/container-runtime/src/Models/DockerProcedure.ts index 5bcd9d5a9..8392fa5ad 100644 --- a/container-runtime/src/Models/DockerProcedure.ts +++ b/container-runtime/src/Models/DockerProcedure.ts @@ -1,41 +1,19 @@ -import { - object, - literal, - string, - boolean, - array, - dictionary, - literals, - number, - Parser, - some, -} from "ts-matches" +import { z } from "@start9labs/start-sdk" import { matchDuration } from "./Duration" -const VolumeId = string -const Path = string - -export type VolumeId = string -export type Path = string -export const matchDockerProcedure = object({ - type: literal("docker"), - image: string, - system: boolean.optional(), - entrypoint: string, - args: array(string).defaultTo([]), - mounts: dictionary([VolumeId, Path]).optional(), - "io-format": literals( - "json", - "json-pretty", - "yaml", - "cbor", - "toml", - "toml-pretty", - ) +export const matchDockerProcedure = z.object({ + type: z.literal("docker"), + image: z.string(), + system: z.boolean().optional(), + entrypoint: z.string(), + args: z.array(z.string()).default([]), + mounts: z.record(z.string(), z.string()).optional(), + "io-format": z + .enum(["json", "json-pretty", "yaml", "cbor", "toml", "toml-pretty"]) .nullable() .optional(), - "sigterm-timeout": some(number, matchDuration).onMismatch(30), - inject: boolean.defaultTo(false), + "sigterm-timeout": z.union([z.number(), matchDuration]).catch(30), + inject: z.boolean().default(false), }) -export type DockerProcedure = typeof matchDockerProcedure._TYPE +export type DockerProcedure = z.infer diff --git a/container-runtime/src/Models/Duration.ts b/container-runtime/src/Models/Duration.ts index 5f61c362a..8d6ca3da2 100644 --- a/container-runtime/src/Models/Duration.ts +++ b/container-runtime/src/Models/Duration.ts @@ -1,11 +1,11 @@ -import { string } from "ts-matches" +import { z } from "@start9labs/start-sdk" export type TimeUnit = "d" | "h" | "s" | "ms" | "m" | "µs" | "ns" export type Duration = `${number}${TimeUnit}` const durationRegex = /^([0-9]*(\.[0-9]+)?)(ns|µs|ms|s|m|d)$/ -export const matchDuration = string.refine(isDuration) +export const matchDuration = z.string().refine(isDuration) export function isDuration(value: string): value is Duration { return durationRegex.test(value) } diff --git a/container-runtime/src/Models/JsonPath.ts b/container-runtime/src/Models/JsonPath.ts index d101836da..4748e7505 100644 --- a/container-runtime/src/Models/JsonPath.ts +++ b/container-runtime/src/Models/JsonPath.ts @@ -1,10 +1,10 @@ -import { literals, some, string } from "ts-matches" +import { z } from "@start9labs/start-sdk" type NestedPath = `/${A}/${string}/${B}` type NestedPaths = NestedPath<"actions", "run" | "getInput"> // prettier-ignore -type UnNestPaths = - A extends `${infer A}/${infer B}` ? [...UnNestPaths, ... UnNestPaths] : +type UnNestPaths = + A extends `${infer A}/${infer B}` ? [...UnNestPaths, ... UnNestPaths] : [A] export function unNestPath(a: A): UnNestPaths { @@ -17,14 +17,14 @@ function isNestedPath(path: string): path is NestedPaths { return true return false } -export const jsonPath = some( - literals( +export const jsonPath = z.union([ + z.enum([ "/packageInit", "/packageUninit", "/backup/create", "/backup/restore", - ), - string.refine(isNestedPath, "isNestedPath"), -) + ]), + z.string().refine(isNestedPath), +]) -export type JsonPath = typeof jsonPath._TYPE +export type JsonPath = z.infer diff --git a/sdk/Makefile b/sdk/Makefile index fc065466e..c8442063e 100644 --- a/sdk/Makefile +++ b/sdk/Makefile @@ -1,12 +1,12 @@ -PACKAGE_TS_FILES := $(shell git ls-files package/lib) package/lib/test/output.ts -BASE_TS_FILES := $(shell git ls-files base/lib) package/lib/test/output.ts +PACKAGE_TS_FILES := $(shell git ls-files package/lib) +BASE_TS_FILES := $(shell git ls-files base/lib) version = $(shell git tag --sort=committerdate | tail -1) .PHONY: test base/test package/test clean bundle fmt buildOutput check all: bundle -package/test: $(PACKAGE_TS_FILES) package/lib/test/output.ts package/node_modules base/node_modules +package/test: $(PACKAGE_TS_FILES) package/node_modules base/node_modules cd package && npm test base/test: $(BASE_TS_FILES) base/node_modules @@ -21,9 +21,6 @@ clean: rm -f package/lib/test/output.ts rm -rf package/node_modules -package/lib/test/output.ts: package/node_modules package/lib/test/makeOutput.ts package/scripts/oldSpecToBuilder.ts - cd package && npm run buildOutput - bundle: baseDist dist | test fmt touch dist diff --git a/sdk/base/lib/actions/input/builder/inputSpec.ts b/sdk/base/lib/actions/input/builder/inputSpec.ts index 8e76ff99b..944581af5 100644 --- a/sdk/base/lib/actions/input/builder/inputSpec.ts +++ b/sdk/base/lib/actions/input/builder/inputSpec.ts @@ -2,7 +2,7 @@ import { ValueSpec } from '../inputSpecTypes' import { Value } from './value' import { _ } from '../../../util' import { Effects } from '../../../Effects' -import { Parser, object } from 'ts-matches' +import { z } from 'zod' import { DeepPartial } from '../../../types' import { InputSpecTools, createInputSpecTools } from './inputSpecTools' @@ -96,7 +96,7 @@ export class InputSpec< private readonly spec: { [K in keyof Type]: Value }, - public readonly validator: Parser, + public readonly validator: z.ZodType, ) {} public _TYPE: Type = null as any as Type public _PARTIAL: DeepPartial = null as any as DeepPartial @@ -104,13 +104,13 @@ export class InputSpec< spec: { [K in keyof Type]: ValueSpec } - validator: Parser + validator: z.ZodType }> { const answer = {} as { [K in keyof Type]: ValueSpec } const validator = {} as { - [K in keyof Type]: Parser + [K in keyof Type]: z.ZodType } for (const k in this.spec) { const built = await this.spec[k].build(options as any) @@ -119,7 +119,7 @@ export class InputSpec< } return { spec: answer, - validator: object(validator) as any, + validator: z.object(validator) as any, } } @@ -135,7 +135,7 @@ export class InputSpec< const value = build instanceof Function ? build(createInputSpecTools()) : build const newSpec = { ...this.spec, [key]: value } as any - const newValidator = object( + const newValidator = z.object( Object.fromEntries( Object.entries(newSpec).map(([k, v]) => [ k, @@ -163,7 +163,7 @@ export class InputSpec< const addedValues = build instanceof Function ? build(createInputSpecTools()) : build const newSpec = { ...this.spec, ...addedValues } as any - const newValidator = object( + const newValidator = z.object( Object.fromEntries( Object.entries(newSpec).map(([k, v]) => [ k, @@ -175,7 +175,7 @@ export class InputSpec< } static of>>(spec: Spec) { - const validator = object( + const validator = z.object( Object.fromEntries( Object.entries(spec).map(([k, v]) => [k, v.validator]), ), diff --git a/sdk/base/lib/actions/input/builder/inputSpecTools.ts b/sdk/base/lib/actions/input/builder/inputSpecTools.ts index d3eed62e9..04dc26a28 100644 --- a/sdk/base/lib/actions/input/builder/inputSpecTools.ts +++ b/sdk/base/lib/actions/input/builder/inputSpecTools.ts @@ -9,7 +9,7 @@ import { ValueSpecText, } from '../inputSpecTypes' import { DefaultString } from '../inputSpecTypes' -import { Parser } from 'ts-matches' +import { z } from 'zod' import { ListValueSpecText } from '../inputSpecTypes' export interface InputSpecTools { @@ -224,7 +224,7 @@ export interface BoundValue { }, OuterType >, - validator: Parser>, + validator: z.ZodType>, ): Value< UnionRes, UnionResStaticValidatedAs, @@ -232,7 +232,7 @@ export interface BoundValue { > dynamicHidden( - getParser: LazyBuild, OuterType>, + getParser: LazyBuild, OuterType>, ): Value } diff --git a/sdk/base/lib/actions/input/builder/list.ts b/sdk/base/lib/actions/input/builder/list.ts index 89663b011..61ca2f29b 100644 --- a/sdk/base/lib/actions/input/builder/list.ts +++ b/sdk/base/lib/actions/input/builder/list.ts @@ -7,7 +7,7 @@ import { ValueSpecList, ValueSpecListOf, } from '../inputSpecTypes' -import { Parser, arrayOf, string } from 'ts-matches' +import { z } from 'zod' export class List< Type extends StaticValidatedAs, @@ -18,11 +18,11 @@ export class List< public build: LazyBuild< { spec: ValueSpecList - validator: Parser + validator: z.ZodType }, OuterType >, - public readonly validator: Parser, + public readonly validator: z.ZodType, ) {} readonly _TYPE: Type = null as any @@ -69,7 +69,7 @@ export class List< generate?: null | RandomString }, ) { - const validator = arrayOf(string) + const validator = z.array(z.string()) return new List(() => { const spec = { type: 'text' as const, @@ -120,7 +120,7 @@ export class List< OuterType >, ) { - const validator = arrayOf(string) + const validator = z.array(z.string()) return new List(async (options) => { const { spec: aSpec, ...a } = await getA(options) const spec = { @@ -193,8 +193,8 @@ export class List< disabled: false, ...value, }, - validator: arrayOf(built.validator), + validator: z.array(built.validator), } - }, arrayOf(aSpec.spec.validator)) + }, z.array(aSpec.spec.validator)) } } diff --git a/sdk/base/lib/actions/input/builder/value.ts b/sdk/base/lib/actions/input/builder/value.ts index 927173c56..c6b54dec1 100644 --- a/sdk/base/lib/actions/input/builder/value.ts +++ b/sdk/base/lib/actions/input/builder/value.ts @@ -12,37 +12,27 @@ import { } from '../inputSpecTypes' import { DefaultString } from '../inputSpecTypes' import { _, once } from '../../../util' -import { - Parser, - any, - anyOf, - arrayOf, - boolean, - literal, - literals, - number, - object, - string, -} from 'ts-matches' +import { z } from 'zod' import { DeepPartial } from '../../../types' -export const fileInfoParser = object({ - path: string, - commitment: object({ hash: string, size: number }), +export const fileInfoParser = z.object({ + path: z.string(), + commitment: z.object({ hash: z.string(), size: z.number() }), }) -export type FileInfo = typeof fileInfoParser._TYPE +export type FileInfo = z.infer export type AsRequired = Required extends true ? T : T | null const testForAsRequiredParser = once( - () => object({ required: literal(true) }).test, + () => (v: unknown) => + z.object({ required: z.literal(true) }).safeParse(v).success, ) function asRequiredParser( - parser: Parser, + parser: z.ZodType, input: Input, -): Parser> { +): z.ZodType> { if (testForAsRequiredParser()(input)) return parser as any return parser.nullable() as any } @@ -56,11 +46,11 @@ export class Value< public build: LazyBuild< { spec: ValueSpec - validator: Parser + validator: z.ZodType }, OuterType >, - public readonly validator: Parser, + public readonly validator: z.ZodType, ) {} public _TYPE: Type = null as any as Type public _PARTIAL: DeepPartial = null as any as DeepPartial @@ -93,7 +83,7 @@ export class Value< */ immutable?: boolean }) { - const validator = boolean + const validator = z.boolean() return new Value( async () => ({ spec: { @@ -121,7 +111,7 @@ export class Value< OuterType >, ) { - const validator = boolean + const validator = z.boolean() return new Value( async (options) => ({ spec: { @@ -212,7 +202,7 @@ export class Value< */ generate?: RandomString | null }) { - const validator = asRequiredParser(string, a) + const validator = asRequiredParser(z.string(), a) return new Value>( async () => ({ spec: { @@ -274,10 +264,10 @@ export class Value< generate: a.generate ?? null, ...a, }, - validator: asRequiredParser(string, a), + validator: asRequiredParser(z.string(), a), } }, - string.nullable(), + z.string().nullable(), ) } /** @@ -336,7 +326,7 @@ export class Value< */ immutable?: boolean }) { - const validator = asRequiredParser(string, a) + const validator = asRequiredParser(z.string(), a) return new Value>(async () => { const built: ValueSpecTextarea = { description: null, @@ -392,10 +382,10 @@ export class Value< immutable: false, ...a, }, - validator: asRequiredParser(string, a), + validator: asRequiredParser(z.string(), a), } }, - string.nullable(), + z.string().nullable(), ) } /** @@ -456,7 +446,7 @@ export class Value< */ immutable?: boolean }) { - const validator = asRequiredParser(number, a) + const validator = asRequiredParser(z.number(), a) return new Value>( () => ({ spec: { @@ -513,10 +503,10 @@ export class Value< immutable: false, ...a, }, - validator: asRequiredParser(number, a), + validator: asRequiredParser(z.number(), a), } }, - number.nullable(), + z.number().nullable(), ) } /** @@ -555,7 +545,7 @@ export class Value< */ immutable?: boolean }) { - const validator = asRequiredParser(string, a) + const validator = asRequiredParser(z.string(), a) return new Value>( () => ({ spec: { @@ -597,10 +587,10 @@ export class Value< immutable: false, ...a, }, - validator: asRequiredParser(string, a), + validator: asRequiredParser(z.string(), a), } }, - string.nullable(), + z.string().nullable(), ) } /** @@ -649,7 +639,7 @@ export class Value< */ immutable?: boolean }) { - const validator = asRequiredParser(string, a) + const validator = asRequiredParser(z.string(), a) return new Value>( () => ({ spec: { @@ -700,10 +690,10 @@ export class Value< immutable: false, ...a, }, - validator: asRequiredParser(string, a), + validator: asRequiredParser(z.string(), a), } }, - string.nullable(), + z.string().nullable(), ) } /** @@ -757,8 +747,12 @@ export class Value< */ immutable?: boolean }) { - const validator = anyOf( - ...Object.keys(a.values).map((x: keyof Values & string) => literal(x)), + const validator = z.union( + Object.keys(a.values).map((x: keyof Values & string) => z.literal(x)) as [ + z.ZodLiteral, + z.ZodLiteral, + ...z.ZodLiteral[], + ], ) return new Value( () => ({ @@ -803,14 +797,18 @@ export class Value< immutable: false, ...a, }, - validator: anyOf( - ...Object.keys(a.values).map((x: keyof Values & string) => - literal(x), - ), + validator: z.union( + Object.keys(a.values).map((x: keyof Values & string) => + z.literal(x), + ) as [ + z.ZodLiteral, + z.ZodLiteral, + ...z.ZodLiteral[], + ], ), } }, - string, + z.string(), ) } /** @@ -865,8 +863,14 @@ export class Value< */ immutable?: boolean }) { - const validator = arrayOf( - literals(...(Object.keys(a.values) as any as [keyof Values & string])), + const validator = z.array( + z.union( + Object.keys(a.values).map((x) => z.literal(x)) as [ + z.ZodLiteral, + z.ZodLiteral, + ...z.ZodLiteral[], + ], + ), ) return new Value<(keyof Values & string)[]>( () => ({ @@ -920,13 +924,17 @@ export class Value< immutable: false, ...a, }, - validator: arrayOf( - literals( - ...(Object.keys(a.values) as any as [keyof Values & string]), + validator: z.array( + z.union( + Object.keys(a.values).map((x) => z.literal(x)) as [ + z.ZodLiteral, + z.ZodLiteral, + ...z.ZodLiteral[], + ], ), ), } - }, arrayOf(string)) + }, z.array(z.string())) } /** * @description Display a collapsable grouping of additional fields, a "sub form". The second value is the inputSpec spec for the sub form. @@ -1077,7 +1085,7 @@ export class Value< }) { return new Value< typeof a.variants._TYPE, - typeof a.variants.validator._TYPE + typeof a.variants.validator._output >(async (options) => { const built = await a.variants.build(options as any) return { @@ -1136,7 +1144,7 @@ export class Value< }, OuterType >, - validator: Parser>, + validator: z.ZodType>, ): Value< UnionRes, UnionResStaticValidatedAs, @@ -1162,11 +1170,11 @@ export class Value< }, OuterType >, - validator: Parser = any, + validator: z.ZodType = z.any(), ) { return new Value< UnionRes, - typeof validator._TYPE, + z.infer, OuterType >(async (options) => { const newValues = await getA(options) @@ -1259,9 +1267,9 @@ export class Value< * ``` */ static hidden(): Value - static hidden(parser: Parser): Value - static hidden(parser: Parser = any) { - return new Value(async () => { + static hidden(parser: z.ZodType): Value + static hidden(parser: z.ZodType = z.any()) { + return new Value>(async () => { return { spec: { type: 'hidden' as const, @@ -1279,7 +1287,7 @@ export class Value< * ``` */ static dynamicHidden( - getParser: LazyBuild, OuterType>, + getParser: LazyBuild, OuterType>, ) { return new Value(async (options) => { const validator = await getParser(options) @@ -1289,7 +1297,7 @@ export class Value< } as ValueSpecHidden, validator, } - }, any) + }, z.any()) } map(fn: (value: StaticValidatedAs) => U): Value { @@ -1297,8 +1305,8 @@ export class Value< const built = await this.build(options) return { spec: built.spec, - validator: built.validator.map(fn), + validator: built.validator.transform(fn), } - }, this.validator.map(fn)) + }, this.validator.transform(fn)) } } diff --git a/sdk/base/lib/actions/input/builder/variants.ts b/sdk/base/lib/actions/input/builder/variants.ts index 3f9be0ec4..4ccfabf55 100644 --- a/sdk/base/lib/actions/input/builder/variants.ts +++ b/sdk/base/lib/actions/input/builder/variants.ts @@ -6,7 +6,7 @@ import { ExtractInputSpecType, ExtractInputSpecStaticValidatedAs, } from './inputSpec' -import { Parser, any, anyOf, literal, object } from 'ts-matches' +import { z } from 'zod' export type UnionRes< VariantValues extends { @@ -109,12 +109,11 @@ export class Variants< public build: LazyBuild< { spec: ValueSpecUnion['variants'] - validator: Parser> + validator: z.ZodType> }, OuterType >, - public readonly validator: Parser< - unknown, + public readonly validator: z.ZodType< UnionResStaticValidatedAs >, ) {} @@ -128,8 +127,7 @@ export class Variants< }, >(a: VariantValues) { const staticValidators = {} as { - [K in keyof VariantValues]: Parser< - unknown, + [K in keyof VariantValues]: z.ZodType< ExtractInputSpecStaticValidatedAs > } @@ -137,16 +135,20 @@ export class Variants< const value = a[key] staticValidators[key] = value.spec.validator } - const other = object( - Object.fromEntries( - Object.entries(staticValidators).map(([k, v]) => [k, any.optional()]), - ), - ).optional() + const other = z + .object( + Object.fromEntries( + Object.entries(staticValidators).map(([k, v]) => [ + k, + z.any().optional(), + ]), + ), + ) + .optional() return new Variants( async (options) => { const validators = {} as { - [K in keyof VariantValues]: Parser< - unknown, + [K in keyof VariantValues]: z.ZodType< ExtractInputSpecType > } @@ -165,32 +167,37 @@ export class Variants< } validators[key] = built.validator } - const other = object( - Object.fromEntries( - Object.entries(validators).map(([k, v]) => [k, any.optional()]), - ), - ).optional() + const other = z + .object( + Object.fromEntries( + Object.entries(validators).map(([k, v]) => [ + k, + z.any().optional(), + ]), + ), + ) + .optional() return { spec: variants, - validator: anyOf( - ...Object.entries(validators).map(([k, v]) => - object({ - selection: literal(k), + validator: z.union( + Object.entries(validators).map(([k, v]) => + z.object({ + selection: z.literal(k), value: v, other, }), - ), + ) as [z.ZodObject, z.ZodObject, ...z.ZodObject[]], ) as any, } }, - anyOf( - ...Object.entries(staticValidators).map(([k, v]) => - object({ - selection: literal(k), + z.union( + Object.entries(staticValidators).map(([k, v]) => + z.object({ + selection: z.literal(k), value: v, other, }), - ), + ) as [z.ZodObject, z.ZodObject, ...z.ZodObject[]], ) as any, ) } diff --git a/sdk/base/lib/actions/setupActions.ts b/sdk/base/lib/actions/setupActions.ts index e648e6322..8c05a7e71 100644 --- a/sdk/base/lib/actions/setupActions.ts +++ b/sdk/base/lib/actions/setupActions.ts @@ -3,7 +3,7 @@ import { ExtractInputSpecType } from './input/builder/inputSpec' import * as T from '../types' import { once } from '../util' import { InitScript } from '../inits' -import { Parser } from 'ts-matches' +import { z } from 'zod' type MaybeInputSpec = {} extends Type ? null : InputSpec export type Run> = (options: { @@ -52,7 +52,7 @@ export class Action> readonly _INPUT: Type = null as any as Type private prevInputSpec: Record< string, - { spec: T.inputSpecTypes.InputSpec; validator: Parser } + { spec: T.inputSpecTypes.InputSpec; validator: z.ZodType } > = {} private constructor( readonly id: Id, @@ -137,7 +137,7 @@ export class Action> `getActionInput has not been called for EventID ${options.effects.eventId}`, ) } - options.input = prevInputSpec.validator.unsafeCast(options.input) + options.input = prevInputSpec.validator.parse(options.input) spec = prevInputSpec.spec } return ( diff --git a/sdk/base/lib/index.ts b/sdk/base/lib/index.ts index 85d00a794..bf2cc1e4b 100644 --- a/sdk/base/lib/index.ts +++ b/sdk/base/lib/index.ts @@ -8,6 +8,6 @@ export * as types from './types' export * as T from './types' export * as yaml from 'yaml' export * as inits from './inits' -export * as matches from 'ts-matches' +export { z } from 'zod' export * as utils from './util' diff --git a/sdk/base/lib/interfaces/Host.ts b/sdk/base/lib/interfaces/Host.ts index 0ad4424ba..8842caf77 100644 --- a/sdk/base/lib/interfaces/Host.ts +++ b/sdk/base/lib/interfaces/Host.ts @@ -1,4 +1,4 @@ -import { object, string } from 'ts-matches' +import { z } from 'zod' import { Effects } from '../Effects' import { Origin } from './Origin' import { AddSslOptions, BindParams } from '../osBindings' @@ -69,9 +69,8 @@ export type BindOptionsByProtocol = | BindOptionsByKnownProtocol | (BindOptions & { protocol: null }) -const hasStringProtocol = object({ - protocol: string, -}).test +const hasStringProtocol = (v: unknown): v is { protocol: string } => + z.object({ protocol: z.string() }).safeParse(v).success export class MultiHost { constructor( diff --git a/sdk/base/lib/util/deepEqual.ts b/sdk/base/lib/util/deepEqual.ts index e64c23429..cee9c67ff 100644 --- a/sdk/base/lib/util/deepEqual.ts +++ b/sdk/base/lib/util/deepEqual.ts @@ -1,7 +1,7 @@ -import { object } from 'ts-matches' - export function deepEqual(...args: unknown[]) { - const objects = args.filter(object.test) + const objects = args.filter( + (x): x is object => typeof x === 'object' && x !== null, + ) if (objects.length === 0) { for (const x of args) if (x !== args[0]) return false return true diff --git a/sdk/base/lib/util/graph.ts b/sdk/base/lib/util/graph.ts index f2ba5ff09..5f6de64fd 100644 --- a/sdk/base/lib/util/graph.ts +++ b/sdk/base/lib/util/graph.ts @@ -1,4 +1,3 @@ -import { boolean } from 'ts-matches' import { ExtendedVersion } from '../exver' export type Vertex = { diff --git a/sdk/base/lib/util/splitCommand.ts b/sdk/base/lib/util/splitCommand.ts index 8a9144b53..8e17d8fe5 100644 --- a/sdk/base/lib/util/splitCommand.ts +++ b/sdk/base/lib/util/splitCommand.ts @@ -1,8 +1,6 @@ -import { arrayOf, string } from 'ts-matches' - export const splitCommand = ( command: string | [string, ...string[]], ): string[] => { - if (arrayOf(string).test(command)) return command + if (Array.isArray(command)) return command return ['sh', '-c', command] } diff --git a/sdk/base/package-lock.json b/sdk/base/package-lock.json index f6949c84f..d20579b22 100644 --- a/sdk/base/package-lock.json +++ b/sdk/base/package-lock.json @@ -13,8 +13,8 @@ "deep-equality-data-structures": "^1.5.0", "isomorphic-fetch": "^3.0.0", "mime": "^4.0.7", - "ts-matches": "^6.3.2", - "yaml": "^2.7.1" + "yaml": "^2.7.1", + "zod": "^4.3.6" }, "devDependencies": { "@types/jest": "^29.4.0", @@ -4626,12 +4626,6 @@ "node": ">=10" } }, - "node_modules/ts-matches": { - "version": "6.3.2", - "resolved": "https://registry.npmjs.org/ts-matches/-/ts-matches-6.3.2.tgz", - "integrity": "sha512-UhSgJymF8cLd4y0vV29qlKVCkQpUtekAaujXbQVc729FezS8HwqzepqvtjzQ3HboatIqN/Idor85O2RMwT7lIQ==", - "license": "MIT" - }, "node_modules/ts-morph": { "version": "18.0.0", "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-18.0.0.tgz", @@ -5006,6 +5000,15 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zod": { + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz", + "integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/sdk/base/package.json b/sdk/base/package.json index 1eda223bf..dce2ca2db 100644 --- a/sdk/base/package.json +++ b/sdk/base/package.json @@ -24,11 +24,11 @@ "@iarna/toml": "^3.0.0", "@noble/curves": "^1.8.2", "@noble/hashes": "^1.7.2", + "deep-equality-data-structures": "^1.5.0", "isomorphic-fetch": "^3.0.0", "mime": "^4.0.7", - "ts-matches": "^6.3.2", "yaml": "^2.7.1", - "deep-equality-data-structures": "^1.5.0" + "zod": "^4.3.6" }, "prettier": { "trailingComma": "all", diff --git a/sdk/package/lib/health/HealthCheck.ts b/sdk/package/lib/health/HealthCheck.ts index 9f1f1088a..191c322ff 100644 --- a/sdk/package/lib/health/HealthCheck.ts +++ b/sdk/package/lib/health/HealthCheck.ts @@ -4,7 +4,6 @@ import { Trigger } from '../trigger' import { TriggerInput } from '../trigger/TriggerInput' import { defaultTrigger } from '../trigger/defaultTrigger' import { once, asError, Drop } from '../util' -import { object, unknown } from 'ts-matches' export type HealthCheckParams = { id: HealthCheckId @@ -109,7 +108,8 @@ export class HealthCheck extends Drop { } function asMessage(e: unknown) { - if (object({ message: unknown }).test(e)) return String(e.message) + if (typeof e === 'object' && e !== null && 'message' in e) + return String((e as any).message) const value = String(e) if (value.length == null) return null return value diff --git a/sdk/package/lib/index.ts b/sdk/package/lib/index.ts index 3526e1c05..13427fe21 100644 --- a/sdk/package/lib/index.ts +++ b/sdk/package/lib/index.ts @@ -7,7 +7,7 @@ import { ISB, IST, types, - matches, + z, utils, } from '../../base/lib' @@ -20,7 +20,7 @@ export { ISB, IST, types, - matches, + z, utils, } export { setupI18n } from './i18n' diff --git a/sdk/package/lib/test/inputSpecBuilder.test.ts b/sdk/package/lib/test/inputSpecBuilder.test.ts index 5bf6f01cb..d9b2dd4c0 100644 --- a/sdk/package/lib/test/inputSpecBuilder.test.ts +++ b/sdk/package/lib/test/inputSpecBuilder.test.ts @@ -1,4 +1,3 @@ -import { testOutput } from './output.test' import { InputSpec } from '../../../base/lib/actions/input/builder/inputSpec' import { List } from '../../../base/lib/actions/input/builder/list' import { Value } from '../../../base/lib/actions/input/builder/value' @@ -7,6 +6,12 @@ import { ValueSpec } from '../../../base/lib/actions/input/inputSpecTypes' import { setupManifest } from '../manifest/setupManifest' import { StartSdk } from '../StartSdk' +export type IfEquals = + (() => G extends T ? 1 : 2) extends () => G extends U ? 1 : 2 ? Y : N +export function testOutput(): (c: IfEquals) => null { + return () => null +} + describe('builder tests', () => { test('text', async () => { const bitcoinPropertiesBuilt: { @@ -50,8 +55,8 @@ describe('values', () => { default: false, }).build({} as any) const validator = value.validator - validator.unsafeCast(false) - testOutput()(null) + validator.parse(false) + testOutput()(null) }) test('text', async () => { const value = await Value.text({ @@ -61,9 +66,9 @@ describe('values', () => { }).build({} as any) const validator = value.validator const rawIs = value.spec - validator.unsafeCast('test text') - expect(() => validator.unsafeCast(null)).toThrowError() - testOutput()(null) + validator.parse('test text') + expect(() => validator.parse(null)).toThrowError() + testOutput()(null) }) test('text with default', async () => { const value = await Value.text({ @@ -73,9 +78,9 @@ describe('values', () => { }).build({} as any) const validator = value.validator const rawIs = value.spec - validator.unsafeCast('test text') - expect(() => validator.unsafeCast(null)).toThrowError() - testOutput()(null) + validator.parse('test text') + expect(() => validator.parse(null)).toThrowError() + testOutput()(null) }) test('optional text', async () => { const value = await Value.text({ @@ -85,9 +90,9 @@ describe('values', () => { }).build({} as any) const validator = value.validator const rawIs = value.spec - validator.unsafeCast('test text') - validator.unsafeCast(null) - testOutput()(null) + validator.parse('test text') + validator.parse(null) + testOutput()(null) }) test('color', async () => { const value = await Value.color({ @@ -98,8 +103,8 @@ describe('values', () => { warning: null, }).build({} as any) const validator = value.validator - validator.unsafeCast('#000000') - testOutput()(null) + validator.parse('#000000') + testOutput()(null) }) test('datetime', async () => { const value = await Value.datetime({ @@ -113,8 +118,8 @@ describe('values', () => { max: null, }).build({} as any) const validator = value.validator - validator.unsafeCast('2021-01-01') - testOutput()(null) + validator.parse('2021-01-01') + testOutput()(null) }) test('optional datetime', async () => { const value = await Value.datetime({ @@ -128,8 +133,8 @@ describe('values', () => { max: null, }).build({} as any) const validator = value.validator - validator.unsafeCast('2021-01-01') - testOutput()(null) + validator.parse('2021-01-01') + testOutput()(null) }) test('textarea', async () => { const value = await Value.textarea({ @@ -145,8 +150,8 @@ describe('values', () => { placeholder: null, }).build({} as any) const validator = value.validator - validator.unsafeCast('test text') - testOutput()(null) + validator.parse('test text') + testOutput()(null) }) test('number', async () => { const value = await Value.number({ @@ -163,8 +168,8 @@ describe('values', () => { placeholder: null, }).build({} as any) const validator = value.validator - validator.unsafeCast(2) - testOutput()(null) + validator.parse(2) + testOutput()(null) }) test('optional number', async () => { const value = await Value.number({ @@ -181,8 +186,8 @@ describe('values', () => { placeholder: null, }).build({} as any) const validator = value.validator - validator.unsafeCast(2) - testOutput()(null) + validator.parse(2) + testOutput()(null) }) test('select', async () => { const value = await Value.select({ @@ -196,10 +201,10 @@ describe('values', () => { warning: null, }).build({} as any) const validator = value.validator - validator.unsafeCast('a') - validator.unsafeCast('b') - expect(() => validator.unsafeCast('c')).toThrowError() - testOutput()(null) + validator.parse('a') + validator.parse('b') + expect(() => validator.parse('c')).toThrowError() + testOutput()(null) }) test('nullable select', async () => { const value = await Value.select({ @@ -213,9 +218,9 @@ describe('values', () => { warning: null, }).build({} as any) const validator = value.validator - validator.unsafeCast('a') - validator.unsafeCast('b') - testOutput()(null) + validator.parse('a') + validator.parse('b') + testOutput()(null) }) test('multiselect', async () => { const value = await Value.multiselect({ @@ -231,12 +236,12 @@ describe('values', () => { maxLength: null, }).build({} as any) const validator = value.validator - validator.unsafeCast([]) - validator.unsafeCast(['a', 'b']) + validator.parse([]) + validator.parse(['a', 'b']) - expect(() => validator.unsafeCast(['e'])).toThrowError() - expect(() => validator.unsafeCast([4])).toThrowError() - testOutput>()(null) + expect(() => validator.parse(['e'])).toThrowError() + expect(() => validator.parse([4])).toThrowError() + testOutput>()(null) }) test('object', async () => { const value = await Value.object( @@ -254,8 +259,8 @@ describe('values', () => { }), ).build({} as any) const validator = value.validator - validator.unsafeCast({ a: true }) - testOutput()(null) + validator.parse({ a: true }) + testOutput()(null) }) test('union', async () => { const value = await Value.union({ @@ -278,8 +283,8 @@ describe('values', () => { }), }).build({} as any) const validator = value.validator - validator.unsafeCast({ selection: 'a', value: { b: false } }) - type Test = typeof validator._TYPE + validator.parse({ selection: 'a', value: { b: false } }) + type Test = typeof validator._output testOutput< Test, { @@ -306,9 +311,9 @@ describe('values', () => { default: false, })).build({} as any) const validator = value.validator - validator.unsafeCast(false) - expect(() => validator.unsafeCast(null)).toThrowError() - testOutput()(null) + validator.parse(false) + expect(() => validator.parse(null)).toThrowError() + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', description: null, @@ -324,9 +329,9 @@ describe('values', () => { })).build({} as any) const validator = value.validator const rawIs = value.spec - validator.unsafeCast('test text') - validator.unsafeCast(null) - testOutput()(null) + validator.parse('test text') + validator.parse(null) + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -340,9 +345,9 @@ describe('values', () => { default: 'this is a default value', })).build({} as any) const validator = value.validator - validator.unsafeCast('test text') - validator.unsafeCast(null) - testOutput()(null) + validator.parse('test text') + validator.parse(null) + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -357,9 +362,9 @@ describe('values', () => { })).build({} as any) const validator = value.validator const rawIs = value.spec - validator.unsafeCast('test text') - validator.unsafeCast(null) - testOutput()(null) + validator.parse('test text') + validator.parse(null) + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -375,9 +380,9 @@ describe('values', () => { warning: null, })).build({} as any) const validator = value.validator - validator.unsafeCast('#000000') - validator.unsafeCast(null) - testOutput()(null) + validator.parse('#000000') + validator.parse(null) + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -432,9 +437,9 @@ describe('values', () => { } }).build({} as any) const validator = value.validator - validator.unsafeCast('2021-01-01') - validator.unsafeCast(null) - testOutput()(null) + validator.parse('2021-01-01') + validator.parse(null) + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -458,8 +463,8 @@ describe('values', () => { placeholder: null, })).build({} as any) const validator = value.validator - validator.unsafeCast('test text') - testOutput()(null) + validator.parse('test text') + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -480,10 +485,10 @@ describe('values', () => { placeholder: null, })).build({} as any) const validator = value.validator - validator.unsafeCast(2) - validator.unsafeCast(null) - expect(() => validator.unsafeCast('null')).toThrowError() - testOutput()(null) + validator.parse(2) + validator.parse(null) + expect(() => validator.parse('null')).toThrowError() + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', required: false, @@ -501,9 +506,9 @@ describe('values', () => { warning: null, })).build({} as any) const validator = value.validator - validator.unsafeCast('a') - validator.unsafeCast('b') - testOutput()(null) + validator.parse('a') + validator.parse('b') + testOutput()(null) expect(value.spec).toMatchObject({ name: 'Testing', }) @@ -522,12 +527,12 @@ describe('values', () => { maxLength: null, })).build({} as any) const validator = value.validator - validator.unsafeCast([]) - validator.unsafeCast(['a', 'b']) + validator.parse([]) + validator.parse(['a', 'b']) - expect(() => validator.unsafeCast([4])).toThrowError() - expect(() => validator.unsafeCast(null)).toThrowError() - testOutput>()(null) + expect(() => validator.parse([4])).toThrowError() + expect(() => validator.parse(null)).toThrowError() + testOutput>()(null) expect(value.spec).toMatchObject({ name: 'Testing', default: [], @@ -568,8 +573,8 @@ describe('values', () => { }), })).build({} as any) const validator = value.validator - validator.unsafeCast({ selection: 'a', value: { b: false } }) - type Test = typeof validator._TYPE + validator.parse({ selection: 'a', value: { b: false } }) + type Test = typeof validator._output testOutput< Test, | { @@ -653,8 +658,8 @@ describe('values', () => { }), })).build({} as any) const validator = value.validator - validator.unsafeCast({ selection: 'a', value: { b: false } }) - type Test = typeof validator._TYPE + validator.parse({ selection: 'a', value: { b: false } }) + type Test = typeof validator._output testOutput< Test, | { @@ -726,8 +731,8 @@ describe('Builder List', () => { ), ).build({} as any) const validator = value.validator - validator.unsafeCast([{ test: true }]) - testOutput()(null) + validator.parse([{ test: true }]) + testOutput()(null) }) test('text', async () => { const value = await Value.list( @@ -741,8 +746,8 @@ describe('Builder List', () => { ), ).build({} as any) const validator = value.validator - validator.unsafeCast(['test', 'text']) - testOutput()(null) + validator.parse(['test', 'text']) + testOutput()(null) }) describe('dynamic', () => { test('text', async () => { @@ -753,10 +758,10 @@ describe('Builder List', () => { })), ).build({} as any) const validator = value.validator - validator.unsafeCast(['test', 'text']) - expect(() => validator.unsafeCast([3, 4])).toThrowError() - expect(() => validator.unsafeCast(null)).toThrowError() - testOutput()(null) + validator.parse(['test', 'text']) + expect(() => validator.parse([3, 4])).toThrowError() + expect(() => validator.parse(null)).toThrowError() + testOutput()(null) expect(value.spec).toMatchObject({ name: 'test', spec: { patterns: [] }, @@ -777,10 +782,10 @@ describe('Nested nullable values', () => { }), }).build({} as any) const validator = value.validator - validator.unsafeCast({ a: null }) - validator.unsafeCast({ a: 'test' }) - expect(() => validator.unsafeCast({ a: 4 })).toThrowError() - testOutput()(null) + validator.parse({ a: null }) + validator.parse({ a: 'test' }) + expect(() => validator.parse({ a: 4 })).toThrowError() + testOutput()(null) }) test('Testing number', async () => { const value = await InputSpec.of({ @@ -800,10 +805,10 @@ describe('Nested nullable values', () => { }), }).build({} as any) const validator = value.validator - validator.unsafeCast({ a: null }) - validator.unsafeCast({ a: 5 }) - expect(() => validator.unsafeCast({ a: '4' })).toThrowError() - testOutput()(null) + validator.parse({ a: null }) + validator.parse({ a: 5 }) + expect(() => validator.parse({ a: '4' })).toThrowError() + testOutput()(null) }) test('Testing color', async () => { const value = await InputSpec.of({ @@ -817,10 +822,10 @@ describe('Nested nullable values', () => { }), }).build({} as any) const validator = value.validator - validator.unsafeCast({ a: null }) - validator.unsafeCast({ a: '5' }) - expect(() => validator.unsafeCast({ a: 4 })).toThrowError() - testOutput()(null) + validator.parse({ a: null }) + validator.parse({ a: '5' }) + expect(() => validator.parse({ a: 4 })).toThrowError() + testOutput()(null) }) test('Testing select', async () => { const value = await InputSpec.of({ @@ -847,9 +852,9 @@ describe('Nested nullable values', () => { }).build({} as any) const validator = value.validator - validator.unsafeCast({ a: 'a' }) - expect(() => validator.unsafeCast({ a: '4' })).toThrowError() - testOutput()(null) + validator.parse({ a: 'a' }) + expect(() => validator.parse({ a: '4' })).toThrowError() + testOutput()(null) }) test('Testing multiselect', async () => { const value = await InputSpec.of({ @@ -868,10 +873,10 @@ describe('Nested nullable values', () => { }), }).build({} as any) const validator = value.validator - validator.unsafeCast({ a: [] }) - validator.unsafeCast({ a: ['a'] }) - expect(() => validator.unsafeCast({ a: ['4'] })).toThrowError() - expect(() => validator.unsafeCast({ a: '4' })).toThrowError() - testOutput()(null) + validator.parse({ a: [] }) + validator.parse({ a: ['a'] }) + expect(() => validator.parse({ a: ['4'] })).toThrowError() + expect(() => validator.parse({ a: '4' })).toThrowError() + testOutput()(null) }) }) diff --git a/sdk/package/lib/test/makeOutput.ts b/sdk/package/lib/test/makeOutput.ts deleted file mode 100644 index b526aeca3..000000000 --- a/sdk/package/lib/test/makeOutput.ts +++ /dev/null @@ -1,428 +0,0 @@ -import { oldSpecToBuilder } from '../../scripts/oldSpecToBuilder' - -oldSpecToBuilder( - // Make the location - './lib/test/output.ts', - // Put the inputSpec here - { - mediasources: { - type: 'list', - subtype: 'enum', - name: 'Media Sources', - description: 'List of Media Sources to use with Jellyfin', - range: '[1,*)', - default: ['nextcloud'], - spec: { - values: ['nextcloud', 'filebrowser'], - 'value-names': { - nextcloud: 'NextCloud', - filebrowser: 'File Browser', - }, - }, - }, - testListUnion: { - type: 'list', - subtype: 'union', - name: 'Lightning Nodes', - description: 'List of Lightning Network node instances to manage', - range: '[1,*)', - default: ['lnd'], - spec: { - type: 'string', - 'display-as': '{{name}}', - 'unique-by': 'name', - name: 'Node Implementation', - tag: { - id: 'type', - name: 'Type', - description: - '- LND: Lightning Network Daemon from Lightning Labs\n- CLN: Core Lightning from Blockstream\n', - 'variant-names': { - lnd: 'Lightning Network Daemon (LND)', - 'c-lightning': 'Core Lightning (CLN)', - }, - }, - default: 'lnd', - variants: { - lnd: { - name: { - type: 'string', - name: 'Node Name', - description: 'Name of this node in the list', - default: 'LND Wrapper', - nullable: false, - }, - }, - }, - }, - }, - rpc: { - type: 'object', - name: 'RPC Settings', - description: 'RPC configuration options.', - spec: { - enable: { - type: 'boolean', - name: 'Enable', - description: 'Allow remote RPC requests.', - default: true, - }, - username: { - type: 'string', - nullable: false, - name: 'Username', - description: 'The username for connecting to Bitcoin over RPC.', - default: 'bitcoin', - masked: true, - pattern: '^[a-zA-Z0-9_]+$', - 'pattern-description': - 'Must be alphanumeric (can contain underscore).', - }, - password: { - type: 'string', - nullable: false, - name: 'RPC Password', - description: 'The password for connecting to Bitcoin over RPC.', - default: { - charset: 'a-z,2-7', - len: 20, - }, - pattern: '^[^\\n"]*$', - 'pattern-description': - 'Must not contain newline or quote characters.', - copyable: true, - masked: true, - }, - bio: { - type: 'string', - nullable: false, - name: 'Username', - description: 'The username for connecting to Bitcoin over RPC.', - default: 'bitcoin', - masked: true, - pattern: '^[a-zA-Z0-9_]+$', - 'pattern-description': - 'Must be alphanumeric (can contain underscore).', - textarea: true, - }, - advanced: { - type: 'object', - name: 'Advanced', - description: 'Advanced RPC Settings', - spec: { - auth: { - name: 'Authorization', - description: - 'Username and hashed password for JSON-RPC connections. RPC clients connect using the usual http basic authentication.', - type: 'list', - subtype: 'string', - default: [], - spec: { - pattern: - '^[a-zA-Z0-9_-]+:([0-9a-fA-F]{2})+\\$([0-9a-fA-F]{2})+$', - 'pattern-description': - 'Each item must be of the form ":$".', - masked: false, - }, - range: '[0,*)', - }, - serialversion: { - name: 'Serialization Version', - description: - 'Return raw transaction or block hex with Segwit or non-SegWit serialization.', - type: 'enum', - values: ['non-segwit', 'segwit'], - 'value-names': {}, - default: 'segwit', - }, - servertimeout: { - name: 'Rpc Server Timeout', - description: - 'Number of seconds after which an uncompleted RPC call will time out.', - type: 'number', - nullable: false, - range: '[5,300]', - integral: true, - units: 'seconds', - default: 30, - }, - threads: { - name: 'Threads', - description: - 'Set the number of threads for handling RPC calls. You may wish to increase this if you are making lots of calls via an integration.', - type: 'number', - nullable: false, - default: 16, - range: '[1,64]', - integral: true, - }, - workqueue: { - name: 'Work Queue', - description: - 'Set the depth of the work queue to service RPC calls. Determines how long the backlog of RPC requests can get before it just rejects new ones.', - type: 'number', - nullable: false, - default: 128, - range: '[8,256]', - integral: true, - units: 'requests', - }, - }, - }, - }, - }, - 'zmq-enabled': { - type: 'boolean', - name: 'ZeroMQ Enabled', - description: 'Enable the ZeroMQ interface', - default: true, - }, - txindex: { - type: 'boolean', - name: 'Transaction Index', - description: 'Enable the Transaction Index (txindex)', - default: true, - }, - wallet: { - type: 'object', - name: 'Wallet', - description: 'Wallet Settings', - spec: { - enable: { - name: 'Enable Wallet', - description: 'Load the wallet and enable wallet RPC calls.', - type: 'boolean', - default: true, - }, - avoidpartialspends: { - name: 'Avoid Partial Spends', - description: - 'Group outputs by address, selecting all or none, instead of selecting on a per-output basis. This improves privacy at the expense of higher transaction fees.', - type: 'boolean', - default: true, - }, - discardfee: { - name: 'Discard Change Tolerance', - description: - 'The fee rate (in BTC/kB) that indicates your tolerance for discarding change by adding it to the fee.', - type: 'number', - nullable: false, - default: 0.0001, - range: '[0,.01]', - integral: false, - units: 'BTC/kB', - }, - }, - }, - advanced: { - type: 'object', - name: 'Advanced', - description: 'Advanced Settings', - spec: { - mempool: { - type: 'object', - name: 'Mempool', - description: 'Mempool Settings', - spec: { - mempoolfullrbf: { - name: 'Enable Full RBF', - description: - 'Policy for your node to use for relaying and mining unconfirmed transactions. For details, see https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-24.0.md#notice-of-new-option-for-transaction-replacement-policies', - type: 'boolean', - default: false, - }, - persistmempool: { - type: 'boolean', - name: 'Persist Mempool', - description: 'Save the mempool on shutdown and load on restart.', - default: true, - }, - maxmempool: { - type: 'number', - nullable: false, - name: 'Max Mempool Size', - description: - 'Keep the transaction memory pool below megabytes.', - range: '[1,*)', - integral: true, - units: 'MiB', - default: 300, - }, - mempoolexpiry: { - type: 'number', - nullable: false, - name: 'Mempool Expiration', - description: - 'Do not keep transactions in the mempool longer than hours.', - range: '[1,*)', - integral: true, - units: 'Hr', - default: 336, - }, - }, - }, - peers: { - type: 'object', - name: 'Peers', - description: 'Peer Connection Settings', - spec: { - listen: { - type: 'boolean', - name: 'Make Public', - description: - 'Allow other nodes to find your server on the network.', - default: true, - }, - onlyconnect: { - type: 'boolean', - name: 'Disable Peer Discovery', - description: 'Only connect to specified peers.', - default: false, - }, - onlyonion: { - type: 'boolean', - name: 'Disable Clearnet', - description: 'Only connect to peers over Tor.', - default: false, - }, - addnode: { - name: 'Add Nodes', - description: 'Add addresses of nodes to connect to.', - type: 'list', - subtype: 'object', - range: '[0,*)', - default: [], - spec: { - 'unique-by': null, - spec: { - hostname: { - type: 'string', - nullable: true, - name: 'Hostname', - description: 'Domain or IP address of bitcoin peer', - pattern: - '(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)|((^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$)|(^[a-z2-7]{16}\\.onion$)|(^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$))', - 'pattern-description': - "Must be either a domain name, or an IPv4 or IPv6 address. Do not include protocol scheme (eg 'http://') or port.", - masked: false, - }, - port: { - type: 'number', - nullable: true, - name: 'Port', - description: - 'Port that peer is listening on for inbound p2p connections', - range: '[0,65535]', - integral: true, - }, - }, - }, - }, - }, - }, - dbcache: { - type: 'number', - nullable: true, - name: 'Database Cache', - description: - "How much RAM to allocate for caching the TXO set. Higher values improve syncing performance, but increase your chance of using up all your system's memory or corrupting your database in the event of an ungraceful shutdown. Set this high but comfortably below your system's total RAM during IBD, then turn down to 450 (or leave blank) once the sync completes.", - warning: - 'WARNING: Increasing this value results in a higher chance of ungraceful shutdowns, which can leave your node unusable if it happens during the initial block download. Use this setting with caution. Be sure to set this back to the default (450 or leave blank) once your node is synced. DO NOT press the STOP button if your dbcache is large. Instead, set this number back to the default, hit save, and wait for bitcoind to restart on its own.', - range: '(0,*)', - integral: true, - units: 'MiB', - }, - pruning: { - type: 'union', - name: 'Pruning Settings', - description: - 'Blockchain Pruning Options\nReduce the blockchain size on disk\n', - warning: - 'If you set pruning to Manual and your disk is smaller than the total size of the blockchain, you MUST have something running that prunes these blocks or you may overfill your disk!\nDisabling pruning will convert your node into a full archival node. This requires a resync of the entire blockchain, a process that may take several days. Make sure you have enough free disk space or you may fill up your disk.\n', - tag: { - id: 'mode', - name: 'Pruning Mode', - description: - '- Disabled: Disable pruning\n- Automatic: Limit blockchain size on disk to a certain number of megabytes\n- Manual: Prune blockchain with the "pruneblockchain" RPC\n', - 'variant-names': { - disabled: 'Disabled', - automatic: 'Automatic', - manual: 'Manual', - }, - }, - variants: { - disabled: {}, - automatic: { - size: { - type: 'number', - nullable: false, - name: 'Max Chain Size', - description: 'Limit of blockchain size on disk.', - warning: - 'Increasing this value will require re-syncing your node.', - default: 550, - range: '[550,1000000)', - integral: true, - units: 'MiB', - }, - }, - manual: { - size: { - type: 'number', - nullable: false, - name: 'Failsafe Chain Size', - description: 'Prune blockchain if size expands beyond this.', - default: 65536, - range: '[550,1000000)', - integral: true, - units: 'MiB', - }, - }, - }, - default: 'disabled', - }, - blockfilters: { - type: 'object', - name: 'Block Filters', - description: 'Settings for storing and serving compact block filters', - spec: { - blockfilterindex: { - type: 'boolean', - name: 'Compute Compact Block Filters (BIP158)', - description: - "Generate Compact Block Filters during initial sync (IBD) to enable 'getblockfilter' RPC. This is useful if dependent services need block filters to efficiently scan for addresses/transactions etc.", - default: true, - }, - peerblockfilters: { - type: 'boolean', - name: 'Serve Compact Block Filters to Peers (BIP157)', - description: - "Serve Compact Block Filters as a peer service to other nodes on the network. This is useful if you wish to connect an SPV client to your node to make it efficient to scan transactions without having to download all block data. 'Compute Compact Block Filters (BIP158)' is required.", - default: false, - }, - }, - }, - bloomfilters: { - type: 'object', - name: 'Bloom Filters (BIP37)', - description: 'Setting for serving Bloom Filters', - spec: { - peerbloomfilters: { - type: 'boolean', - name: 'Serve Bloom Filters to Peers', - description: - 'Peers have the option of setting filters on each connection they make after the version handshake has completed. Bloom filters are for clients implementing SPV (Simplified Payment Verification) that want to check that block headers connect together correctly, without needing to verify the full blockchain. The client must trust that the transactions in the chain are in fact valid. It is highly recommended AGAINST using for anything except Bisq integration.', - warning: - 'This is ONLY for use with Bisq integration, please use Block Filters for all other applications.', - default: false, - }, - }, - }, - }, - }, - }, - { - // convert this to `start-sdk/lib` for conversions - StartSdk: './output.sdk', - }, -) diff --git a/sdk/package/lib/test/output.test.ts b/sdk/package/lib/test/output.test.ts deleted file mode 100644 index 146092075..000000000 --- a/sdk/package/lib/test/output.test.ts +++ /dev/null @@ -1,148 +0,0 @@ -import { inputSpecSpec, InputSpecSpec } from './output' -import * as _I from '../index' -import { camelCase } from '../../scripts/oldSpecToBuilder' -import { deepMerge } from '../../../base/lib/util' - -export type IfEquals = - (() => G extends T ? 1 : 2) extends () => G extends U ? 1 : 2 ? Y : N -export function testOutput(): (c: IfEquals) => null { - return () => null -} - -/// Testing the types of the input spec -testOutput()(null) -testOutput()(null) -testOutput()(null) - -testOutput()(null) -testOutput< - InputSpecSpec['rpc']['advanced']['serialversion'], - 'segwit' | 'non-segwit' ->()(null) -testOutput()(null) -testOutput< - InputSpecSpec['advanced']['peers']['addnode'][0]['hostname'], - string | null ->()(null) -testOutput< - InputSpecSpec['testListUnion'][0]['union']['value']['name'], - string ->()(null) -testOutput()( - null, -) -testOutput>()( - null, -) - -// @ts-expect-error Because enable should be a boolean -testOutput()(null) -// prettier-ignore -// @ts-expect-error Expect that the string is the one above -testOutput()(null); - -/// Here we test the output of the matchInputSpecSpec function -describe('Inputs', () => { - const validInput: InputSpecSpec = { - mediasources: ['filebrowser'], - testListUnion: [ - { - union: { selection: 'lnd', value: { name: 'string' } }, - }, - ], - rpc: { - enable: true, - bio: 'This is a bio', - username: 'test', - password: 'test', - advanced: { - auth: ['test'], - serialversion: 'segwit', - servertimeout: 6, - threads: 3, - workqueue: 9, - }, - }, - 'zmq-enabled': false, - txindex: false, - wallet: { enable: false, avoidpartialspends: false, discardfee: 0.0001 }, - advanced: { - mempool: { - maxmempool: 1, - persistmempool: true, - mempoolexpiry: 23, - mempoolfullrbf: true, - }, - peers: { - listen: true, - onlyconnect: true, - onlyonion: true, - addnode: [ - { - hostname: 'test', - port: 1, - }, - ], - }, - dbcache: 5, - pruning: { - selection: 'disabled', - value: { disabled: {} }, - }, - blockfilters: { - blockfilterindex: false, - peerblockfilters: false, - }, - bloomfilters: { peerbloomfilters: false }, - }, - } - - test('test valid input', async () => { - const { validator } = await inputSpecSpec.build({} as any) - const output = validator.unsafeCast(validInput) - expect(output).toEqual(validInput) - }) - test('test no longer care about the conversion of min/max and validating', async () => { - const { validator } = await inputSpecSpec.build({} as any) - validator.unsafeCast( - deepMerge({}, validInput, { rpc: { advanced: { threads: 0 } } }), - ) - }) - test('test errors should throw for number in string', async () => { - const { validator } = await inputSpecSpec.build({} as any) - expect(() => - validator.unsafeCast(deepMerge({}, validInput, { rpc: { enable: 2 } })), - ).toThrowError() - }) - test('Test that we set serialversion to something not segwit or non-segwit', async () => { - const { validator } = await inputSpecSpec.build({} as any) - expect(() => - validator.unsafeCast( - deepMerge({}, validInput, { - rpc: { advanced: { serialversion: 'testing' } }, - }), - ), - ).toThrowError() - }) -}) - -describe('camelCase', () => { - test("'EquipmentClass name'", () => { - expect(camelCase('EquipmentClass name')).toEqual('equipmentClassName') - }) - test("'Equipment className'", () => { - expect(camelCase('Equipment className')).toEqual('equipmentClassName') - }) - test("'equipment class name'", () => { - expect(camelCase('equipment class name')).toEqual('equipmentClassName') - }) - test("'Equipment Class Name'", () => { - expect(camelCase('Equipment Class Name')).toEqual('equipmentClassName') - }) - test("'hyphen-name-format'", () => { - expect(camelCase('hyphen-name-format')).toEqual('hyphenNameFormat') - }) - test("'underscore_name_format'", () => { - expect(camelCase('underscore_name_format')).toEqual('underscoreNameFormat') - }) -}) diff --git a/sdk/package/lib/util/fileHelper.ts b/sdk/package/lib/util/fileHelper.ts index 33d3504ad..65f0e9f8f 100644 --- a/sdk/package/lib/util/fileHelper.ts +++ b/sdk/package/lib/util/fileHelper.ts @@ -1,4 +1,4 @@ -import * as matches from 'ts-matches' +import { z } from 'zod' import * as YAML from 'yaml' import * as TOML from '@iarna/toml' import * as INI from 'ini' @@ -97,7 +97,7 @@ function toPath(path: ToPath): string { return path.base.subpath(path.subpath) } -type Validator = matches.Validator | matches.Validator +type Validator<_T, U> = z.ZodType type ReadType = { once: () => Promise @@ -499,7 +499,7 @@ export class FileHelper { (inData) => inData, (inString) => inString, (data) => - (shape || (matches.string as Validator)).unsafeCast( + (shape || (z.string() as unknown as Validator)).parse( data, ), transformers, @@ -518,7 +518,7 @@ export class FileHelper { path, (inData) => JSON.stringify(inData, null, 2), (inString) => JSON.parse(inString), - (data) => shape.unsafeCast(data), + (data) => shape.parse(data), transformers, ) } @@ -544,7 +544,7 @@ export class FileHelper { path, (inData) => YAML.stringify(inData, null, 2), (inString) => YAML.parse(inString), - (data) => shape.unsafeCast(data), + (data) => shape.parse(data), transformers, ) } @@ -570,7 +570,7 @@ export class FileHelper { path, (inData) => TOML.stringify(inData as TOML.JsonMap), (inString) => TOML.parse(inString), - (data) => shape.unsafeCast(data), + (data) => shape.parse(data), transformers, ) } @@ -596,7 +596,7 @@ export class FileHelper { path, (inData) => INI.stringify(filterUndefined(inData), options), (inString) => INI.parse(inString, options), - (data) => shape.unsafeCast(data), + (data) => shape.parse(data), transformers, ) } @@ -632,7 +632,7 @@ export class FileHelper { return [line.slice(0, pos), line.slice(pos + 1)] }), ), - (data) => shape.unsafeCast(data), + (data) => shape.parse(data), transformers, ) } diff --git a/sdk/package/package-lock.json b/sdk/package/package-lock.json index 5024774c6..1fc5a8670 100644 --- a/sdk/package/package-lock.json +++ b/sdk/package/package-lock.json @@ -1,12 +1,12 @@ { "name": "@start9labs/start-sdk", - "version": "0.4.0-beta.50", + "version": "0.4.0-beta.51", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@start9labs/start-sdk", - "version": "0.4.0-beta.50", + "version": "0.4.0-beta.51", "license": "MIT", "dependencies": { "@iarna/toml": "^3.0.0", @@ -17,8 +17,8 @@ "ini": "^5.0.0", "isomorphic-fetch": "^3.0.0", "mime": "^4.0.7", - "ts-matches": "^6.3.2", - "yaml": "^2.7.1" + "yaml": "^2.7.1", + "zod": "^4.3.6" }, "devDependencies": { "@types/jest": "^29.4.0", @@ -4815,12 +4815,6 @@ "node": ">=10" } }, - "node_modules/ts-matches": { - "version": "6.3.2", - "resolved": "https://registry.npmjs.org/ts-matches/-/ts-matches-6.3.2.tgz", - "integrity": "sha512-UhSgJymF8cLd4y0vV29qlKVCkQpUtekAaujXbQVc729FezS8HwqzepqvtjzQ3HboatIqN/Idor85O2RMwT7lIQ==", - "license": "MIT" - }, "node_modules/ts-morph": { "version": "18.0.0", "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-18.0.0.tgz", @@ -5232,6 +5226,15 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zod": { + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz", + "integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/sdk/package/package.json b/sdk/package/package.json index 4583158b0..bd7de2e58 100644 --- a/sdk/package/package.json +++ b/sdk/package/package.json @@ -1,6 +1,6 @@ { "name": "@start9labs/start-sdk", - "version": "0.4.0-beta.50", + "version": "0.4.0-beta.51", "description": "Software development kit to facilitate packaging services for StartOS", "main": "./package/lib/index.js", "types": "./package/lib/index.d.ts", @@ -31,16 +31,16 @@ }, "homepage": "https://github.com/Start9Labs/start-os#readme", "dependencies": { - "isomorphic-fetch": "^3.0.0", - "mime": "^4.0.7", - "ts-matches": "^6.3.2", - "yaml": "^2.7.1", - "deep-equality-data-structures": "^2.0.0", - "ini": "^5.0.0", - "@types/ini": "^4.1.1", "@iarna/toml": "^3.0.0", "@noble/curves": "^1.8.2", - "@noble/hashes": "^1.7.2" + "@noble/hashes": "^1.7.2", + "@types/ini": "^4.1.1", + "deep-equality-data-structures": "^2.0.0", + "ini": "^5.0.0", + "isomorphic-fetch": "^3.0.0", + "mime": "^4.0.7", + "yaml": "^2.7.1", + "zod": "^4.3.6" }, "prettier": { "trailingComma": "all", diff --git a/sdk/package/scripts/oldSpecToBuilder.ts b/sdk/package/scripts/oldSpecToBuilder.ts deleted file mode 100644 index 22d377083..000000000 --- a/sdk/package/scripts/oldSpecToBuilder.ts +++ /dev/null @@ -1,384 +0,0 @@ -import * as fs from 'fs' - -// https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case -export function camelCase(value: string) { - return value - .replace(/([\(\)\[\]])/g, '') - .replace(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2, offset) { - if (p2) return p2.toUpperCase() - return p1.toLowerCase() - }) -} - -export async function oldSpecToBuilder( - file: string, - inputData: Promise | any, - options?: Parameters[1], -) { - await fs.writeFile( - file, - await makeFileContentFromOld(inputData, options), - (err) => console.error(err), - ) -} - -function isString(x: unknown): x is string { - return typeof x === 'string' -} - -export default async function makeFileContentFromOld( - inputData: Promise | any, - { StartSdk = 'start-sdk', nested = true } = {}, -) { - const outputLines: string[] = [] - outputLines.push(` -import { sdk } from "${StartSdk}" -const {InputSpec, List, Value, Variants} = sdk -`) - const data = await inputData - - const namedConsts = new Set(['InputSpec', 'Value', 'List']) - const inputSpecName = newConst('inputSpecSpec', convertInputSpec(data)) - outputLines.push(`export type InputSpecSpec = typeof ${inputSpecName}._TYPE;`) - - return outputLines.join('\n') - - function newConst(key: string, data: string, type?: string) { - const variableName = getNextConstName(camelCase(key)) - outputLines.push( - `export const ${variableName}${!type ? '' : `: ${type}`} = ${data};`, - ) - return variableName - } - function maybeNewConst(key: string, data: string) { - if (nested) return data - return newConst(key, data) - } - function convertInputSpecInner(data: any) { - let answer = '{' - for (const [key, value] of Object.entries(data)) { - const variableName = maybeNewConst(key, convertValueSpec(value)) - - answer += `${JSON.stringify(key)}: ${variableName},` - } - return `${answer}}` - } - - function convertInputSpec(data: any) { - return `InputSpec.of(${convertInputSpecInner(data)})` - } - function convertValueSpec(value: any): string { - switch (value.type) { - case 'string': { - if (value.textarea) { - return `${rangeToTodoComment( - value?.range, - )}Value.textarea(${JSON.stringify( - { - name: value.name || null, - description: value.description || null, - warning: value.warning || null, - required: !(value.nullable || false), - default: value.default, - placeholder: value.placeholder || null, - minLength: null, - maxLength: null, - minRows: 3, - maxRows: 6, - }, - null, - 2, - )})` - } - return `${rangeToTodoComment(value?.range)}Value.text(${JSON.stringify( - { - name: value.name || null, - default: value.default || null, - required: !value.nullable, - description: value.description || null, - warning: value.warning || null, - masked: value.masked || false, - placeholder: value.placeholder || null, - inputmode: 'text', - patterns: value.pattern - ? [ - { - regex: value.pattern, - description: value['pattern-description'], - }, - ] - : [], - minLength: null, - maxLength: null, - }, - null, - 2, - )})` - } - case 'number': { - return `${rangeToTodoComment( - value?.range, - )}Value.number(${JSON.stringify( - { - name: value.name || null, - description: value.description || null, - warning: value.warning || null, - default: value.default || null, - required: !value.nullable, - min: null, - max: null, - step: null, - integer: value.integral || false, - units: value.units || null, - placeholder: value.placeholder || null, - }, - null, - 2, - )})` - } - case 'boolean': { - return `Value.toggle(${JSON.stringify( - { - name: value.name || null, - default: value.default || false, - description: value.description || null, - warning: value.warning || null, - }, - null, - 2, - )})` - } - case 'enum': { - const allValueNames = new Set([ - ...(value?.['values'] || []), - ...Object.keys(value?.['value-names'] || {}), - ]) - const values = Object.fromEntries( - Array.from(allValueNames) - .filter(isString) - .map((key) => [key, value?.spec?.['value-names']?.[key] || key]), - ) - return `Value.select(${JSON.stringify( - { - name: value.name || null, - description: value.description || null, - warning: value.warning || null, - default: value.default, - values, - }, - null, - 2, - )} as const)` - } - case 'object': { - const specName = maybeNewConst( - value.name + '_spec', - convertInputSpec(value.spec), - ) - return `Value.object({ - name: ${JSON.stringify(value.name || null)}, - description: ${JSON.stringify(value.description || null)}, - }, ${specName})` - } - case 'union': { - const variants = maybeNewConst( - value.name + '_variants', - convertVariants(value.variants, value.tag['variant-names'] || {}), - ) - - return `Value.union({ - name: ${JSON.stringify(value.name || null)}, - description: ${JSON.stringify(value.tag.description || null)}, - warning: ${JSON.stringify(value.tag.warning || null)}, - default: ${JSON.stringify(value.default)}, - variants: ${variants}, - })` - } - case 'list': { - if (value.subtype === 'enum') { - const allValueNames = new Set([ - ...(value?.spec?.['values'] || []), - ...Object.keys(value?.spec?.['value-names'] || {}), - ]) - const values = Object.fromEntries( - Array.from(allValueNames) - .filter(isString) - .map((key: string) => [ - key, - value?.spec?.['value-names']?.[key] ?? key, - ]), - ) - return `Value.multiselect(${JSON.stringify( - { - name: value.name || null, - minLength: null, - maxLength: null, - default: value.default ?? null, - description: value.description || null, - warning: value.warning || null, - values, - }, - null, - 2, - )})` - } - const list = maybeNewConst(value.name + '_list', convertList(value)) - return `Value.list(${list})` - } - case 'pointer': { - return `/* TODO deal with point removed point "${value.name}" */null as any` - } - } - throw Error(`Unknown type "${value.type}"`) - } - - function convertList(value: any) { - switch (value.subtype) { - case 'string': { - return `${rangeToTodoComment(value?.range)}List.text(${JSON.stringify( - { - name: value.name || null, - minLength: null, - maxLength: null, - default: value.default || null, - description: value.description || null, - warning: value.warning || null, - }, - null, - 2, - )}, ${JSON.stringify({ - masked: value?.spec?.masked || false, - placeholder: value?.spec?.placeholder || null, - patterns: value?.spec?.pattern - ? [ - { - regex: value.spec.pattern, - description: value?.spec?.['pattern-description'], - }, - ] - : [], - minLength: null, - maxLength: null, - })})` - } - // case "number": { - // return `${rangeToTodoComment(value?.range)}List.number(${JSON.stringify( - // { - // name: value.name || null, - // minLength: null, - // maxLength: null, - // default: value.default || null, - // description: value.description || null, - // warning: value.warning || null, - // }, - // null, - // 2, - // )}, ${JSON.stringify({ - // integer: value?.spec?.integral || false, - // min: null, - // max: null, - // units: value?.spec?.units || null, - // placeholder: value?.spec?.placeholder || null, - // })})` - // } - case 'enum': { - return '/* error!! list.enum */' - } - case 'object': { - const specName = maybeNewConst( - value.name + '_spec', - convertInputSpec(value.spec.spec), - ) - return `${rangeToTodoComment(value?.range)}List.obj({ - name: ${JSON.stringify(value.name || null)}, - minLength: ${JSON.stringify(null)}, - maxLength: ${JSON.stringify(null)}, - default: ${JSON.stringify(value.default || null)}, - description: ${JSON.stringify(value.description || null)}, - }, { - spec: ${specName}, - displayAs: ${JSON.stringify(value?.spec?.['display-as'] || null)}, - uniqueBy: ${JSON.stringify(value?.spec?.['unique-by'] || null)}, - })` - } - case 'union': { - const variants = maybeNewConst( - value.name + '_variants', - convertVariants( - value.spec.variants, - value.spec['variant-names'] || {}, - ), - ) - const unionValueName = maybeNewConst( - value.name + '_union', - `${rangeToTodoComment(value?.range)} - Value.union({ - name: ${JSON.stringify(value?.spec?.tag?.name || null)}, - description: ${JSON.stringify( - value?.spec?.tag?.description || null, - )}, - warning: ${JSON.stringify(value?.spec?.tag?.warning || null)}, - default: ${JSON.stringify(value?.spec?.default || null)}, - variants: ${variants}, - }) - `, - ) - const listInputSpec = maybeNewConst( - value.name + '_list_inputSpec', - ` - InputSpec.of({ - "union": ${unionValueName} - }) - `, - ) - return `${rangeToTodoComment(value?.range)}List.obj({ - name:${JSON.stringify(value.name || null)}, - minLength:${JSON.stringify(null)}, - maxLength:${JSON.stringify(null)}, - default: [], - description: ${JSON.stringify(value.description || null)}, - warning: ${JSON.stringify(value.warning || null)}, - }, { - spec: ${listInputSpec}, - displayAs: ${JSON.stringify(value?.spec?.['display-as'] || null)}, - uniqueBy: ${JSON.stringify(value?.spec?.['unique-by'] || null)}, - })` - } - } - throw new Error(`Unknown subtype "${value.subtype}"`) - } - - function convertVariants( - variants: Record, - variantNames: Record, - ): string { - let answer = 'Variants.of({' - for (const [key, value] of Object.entries(variants)) { - const variantSpec = maybeNewConst(key, convertInputSpec(value)) - answer += `"${key}": {name: "${ - variantNames[key] || key - }", spec: ${variantSpec}},` - } - return `${answer}})` - } - - function getNextConstName(name: string, i = 0): string { - const newName = !i ? name : name + i - if (namedConsts.has(newName)) { - return getNextConstName(name, i + 1) - } - namedConsts.add(newName) - return newName - } -} - -function rangeToTodoComment(range: string | undefined) { - if (!range) return '' - return `/* TODO: Convert range for this value (${range})*/` -} - -// oldSpecToBuilder( -// "./inputSpec.ts", -// // Put inputSpec here -// {}, -// ) diff --git a/sdk/package/tsconfig.json b/sdk/package/tsconfig.json index cee6dad33..d178c8212 100644 --- a/sdk/package/tsconfig.json +++ b/sdk/package/tsconfig.json @@ -16,5 +16,5 @@ "resolveJsonModule": true }, "include": ["lib/**/*"], - "exclude": ["lib/**/*.spec.ts", "lib/**/*.gen.ts", "list", "node_modules"] + "exclude": ["lib/**/*.spec.ts", "lib/**/*.test.ts", "lib/**/*.gen.ts", "list", "node_modules"] } diff --git a/web/package-lock.json b/web/package-lock.json index cc14bb785..e32f3dea3 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -62,7 +62,6 @@ "pbkdf2": "^3.1.2", "rxjs": "^7.8.2", "tldts": "^7.0.11", - "ts-matches": "^6.3.2", "tslib": "^2.8.1", "uuid": "^8.3.2", "zone.js": "^0.15.0" @@ -126,8 +125,8 @@ "deep-equality-data-structures": "^1.5.0", "isomorphic-fetch": "^3.0.0", "mime": "^4.0.7", - "ts-matches": "^6.3.2", - "yaml": "^2.7.1" + "yaml": "^2.7.1", + "zod": "^4.3.6" }, "devDependencies": { "@types/jest": "^29.4.0", @@ -11393,12 +11392,6 @@ "node": ">=0.6" } }, - "node_modules/ts-matches": { - "version": "6.5.0", - "resolved": "https://registry.npmjs.org/ts-matches/-/ts-matches-6.5.0.tgz", - "integrity": "sha512-MhuobYhHYn6MlOTPAF/qk3tsRRioPac5ofYn68tc3rAJaGjsw1MsX1MOSep52DkvNJPgNV0F73zfgcQfYTVeyQ==", - "license": "MIT" - }, "node_modules/ts-morph": { "version": "23.0.0", "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-23.0.0.tgz", diff --git a/web/package.json b/web/package.json index 6d4e17883..9d5818382 100644 --- a/web/package.json +++ b/web/package.json @@ -86,7 +86,6 @@ "pbkdf2": "^3.1.2", "rxjs": "^7.8.2", "tldts": "^7.0.11", - "ts-matches": "^6.3.2", "tslib": "^2.8.1", "uuid": "^8.3.2", "zone.js": "^0.15.0"