mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 04:01:58 +00:00
feat: add zod-deep-partial, partialValidator on InputSpec, and z.deepPartial re-export
This commit is contained in:
5
container-runtime/package-lock.json
generated
5
container-runtime/package-lock.json
generated
@@ -37,7 +37,7 @@
|
|||||||
},
|
},
|
||||||
"../sdk/dist": {
|
"../sdk/dist": {
|
||||||
"name": "@start9labs/start-sdk",
|
"name": "@start9labs/start-sdk",
|
||||||
"version": "0.4.0-beta.51",
|
"version": "0.4.0-beta.52",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@iarna/toml": "^3.0.0",
|
"@iarna/toml": "^3.0.0",
|
||||||
@@ -49,7 +49,8 @@
|
|||||||
"isomorphic-fetch": "^3.0.0",
|
"isomorphic-fetch": "^3.0.0",
|
||||||
"mime": "^4.0.7",
|
"mime": "^4.0.7",
|
||||||
"yaml": "^2.7.1",
|
"yaml": "^2.7.1",
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6",
|
||||||
|
"zod-deep-partial": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.4.0",
|
"@types/jest": "^29.4.0",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Value } from './value'
|
|||||||
import { _ } from '../../../util'
|
import { _ } from '../../../util'
|
||||||
import { Effects } from '../../../Effects'
|
import { Effects } from '../../../Effects'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { zodDeepPartial } from 'zod-deep-partial'
|
||||||
import { DeepPartial } from '../../../types'
|
import { DeepPartial } from '../../../types'
|
||||||
import { InputSpecTools, createInputSpecTools } from './inputSpecTools'
|
import { InputSpecTools, createInputSpecTools } from './inputSpecTools'
|
||||||
|
|
||||||
@@ -21,6 +22,41 @@ export type LazyBuild<ExpectedOut, Type> = (
|
|||||||
options: LazyBuildOptions<Type>,
|
options: LazyBuildOptions<Type>,
|
||||||
) => Promise<ExpectedOut> | ExpectedOut
|
) => Promise<ExpectedOut> | ExpectedOut
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines which keys to keep when filtering an InputSpec.
|
||||||
|
* Use `true` to keep a field as-is, or a nested object to filter sub-fields of an object-typed field.
|
||||||
|
*/
|
||||||
|
export type FilterKeys<T> = {
|
||||||
|
[K in keyof T]?: T[K] extends Record<string, any>
|
||||||
|
? true | FilterKeys<T[K]>
|
||||||
|
: true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the resulting type after applying a {@link FilterKeys} shape to a type.
|
||||||
|
*/
|
||||||
|
export type ApplyFilter<T, F> = {
|
||||||
|
[K in Extract<keyof F, keyof T>]: F[K] extends true
|
||||||
|
? T[K]
|
||||||
|
: T[K] extends Record<string, any>
|
||||||
|
? F[K] extends FilterKeys<T[K]>
|
||||||
|
? ApplyFilter<T[K], F[K]>
|
||||||
|
: T[K]
|
||||||
|
: T[K]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the union of all valid key-path tuples through a nested type.
|
||||||
|
* Each tuple represents a path from root to a field, recursing into object-typed sub-fields.
|
||||||
|
*/
|
||||||
|
export type KeyPaths<T> = {
|
||||||
|
[K in keyof T & string]: T[K] extends any[]
|
||||||
|
? [K]
|
||||||
|
: T[K] extends Record<string, any>
|
||||||
|
? [K] | [K, ...KeyPaths<T[K]>]
|
||||||
|
: [K]
|
||||||
|
}[keyof T & string]
|
||||||
|
|
||||||
/** Extracts the runtime type from an {@link InputSpec}. */
|
/** Extracts the runtime type from an {@link InputSpec}. */
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
export type ExtractInputSpecType<A extends InputSpec<Record<string, any>, any>> =
|
export type ExtractInputSpecType<A extends InputSpec<Record<string, any>, any>> =
|
||||||
@@ -111,6 +147,8 @@ export class InputSpec<
|
|||||||
) {}
|
) {}
|
||||||
public _TYPE: Type = null as any as Type
|
public _TYPE: Type = null as any as Type
|
||||||
public _PARTIAL: DeepPartial<Type> = null as any as DeepPartial<Type>
|
public _PARTIAL: DeepPartial<Type> = null as any as DeepPartial<Type>
|
||||||
|
public readonly partialValidator: z.ZodType<DeepPartial<StaticValidatedAs>> =
|
||||||
|
zodDeepPartial(this.validator) as any
|
||||||
/**
|
/**
|
||||||
* Builds the runtime form specification and combined Zod validator from this InputSpec's fields.
|
* Builds the runtime form specification and combined Zod validator from this InputSpec's fields.
|
||||||
*
|
*
|
||||||
@@ -139,35 +177,6 @@ export class InputSpec<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a single named field to this spec, returning a new `InputSpec` with the extended type.
|
|
||||||
*
|
|
||||||
* @param key - The field key name
|
|
||||||
* @param build - A {@link Value} instance, or a function receiving typed tools that returns one
|
|
||||||
*/
|
|
||||||
addKey<Key extends string, V extends Value<any, any, any>>(
|
|
||||||
key: Key,
|
|
||||||
build: V | ((tools: InputSpecTools<Type>) => V),
|
|
||||||
): InputSpec<
|
|
||||||
Type & { [K in Key]: V extends Value<infer T, any, any> ? T : never },
|
|
||||||
StaticValidatedAs & {
|
|
||||||
[K in Key]: V extends Value<any, infer S, any> ? S : never
|
|
||||||
}
|
|
||||||
> {
|
|
||||||
const value =
|
|
||||||
build instanceof Function ? build(createInputSpecTools<Type>()) : build
|
|
||||||
const newSpec = { ...this.spec, [key]: value } as any
|
|
||||||
const newValidator = z.object(
|
|
||||||
Object.fromEntries(
|
|
||||||
Object.entries(newSpec).map(([k, v]) => [
|
|
||||||
k,
|
|
||||||
(v as Value<any>).validator,
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return new InputSpec(newSpec, newValidator as any)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds multiple fields to this spec at once, returning a new `InputSpec` with extended types.
|
* Adds multiple fields to this spec at once, returning a new `InputSpec` with extended types.
|
||||||
*
|
*
|
||||||
@@ -201,6 +210,241 @@ export class InputSpec<
|
|||||||
return new InputSpec(newSpec, newValidator as any)
|
return new InputSpec(newSpec, newValidator as any)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new InputSpec containing only the specified keys.
|
||||||
|
* Use `true` to keep a field as-is, or a nested object to filter sub-fields of object-typed fields.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const full = InputSpec.of({
|
||||||
|
* name: Value.text({ name: 'Name', required: true, default: null }),
|
||||||
|
* settings: Value.object({ name: 'Settings' }, InputSpec.of({
|
||||||
|
* debug: Value.toggle({ name: 'Debug', default: false }),
|
||||||
|
* port: Value.number({ name: 'Port', required: true, default: 8080, integer: true }),
|
||||||
|
* })),
|
||||||
|
* })
|
||||||
|
* const filtered = full.filter({ name: true, settings: { debug: true } })
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<F extends FilterKeys<Type>>(
|
||||||
|
keys: F,
|
||||||
|
): InputSpec<
|
||||||
|
ApplyFilter<Type, F> & ApplyFilter<StaticValidatedAs, F>,
|
||||||
|
ApplyFilter<StaticValidatedAs, F>
|
||||||
|
> {
|
||||||
|
const newSpec: Record<string, Value<any>> = {}
|
||||||
|
for (const k of Object.keys(keys)) {
|
||||||
|
const filterVal = (keys as any)[k]
|
||||||
|
const value = (this.spec as any)[k] as Value<any> | undefined
|
||||||
|
if (!value) continue
|
||||||
|
if (filterVal === true) {
|
||||||
|
newSpec[k] = value
|
||||||
|
} else if (typeof filterVal === 'object' && filterVal !== null) {
|
||||||
|
const objectMeta = value._objectSpec
|
||||||
|
if (objectMeta) {
|
||||||
|
const filteredInner = objectMeta.inputSpec.filter(filterVal)
|
||||||
|
newSpec[k] = Value.object(objectMeta.params, filteredInner)
|
||||||
|
} else {
|
||||||
|
newSpec[k] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const newValidator = z.object(
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(newSpec).map(([k, v]) => [k, v.validator]),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return new InputSpec(newSpec as any, newValidator as any) as any
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new InputSpec with the specified keys disabled.
|
||||||
|
* Use `true` to disable a field, or a nested object to disable sub-fields of object-typed fields.
|
||||||
|
* All fields remain in the spec — disabled fields simply cannot be edited by the user.
|
||||||
|
*
|
||||||
|
* @param keys - Which fields to disable, using the same shape as {@link FilterKeys}
|
||||||
|
* @param message - The reason the fields are disabled, displayed to the user
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const spec = InputSpec.of({
|
||||||
|
* name: Value.text({ name: 'Name', required: true, default: null }),
|
||||||
|
* settings: Value.object({ name: 'Settings' }, InputSpec.of({
|
||||||
|
* debug: Value.toggle({ name: 'Debug', default: false }),
|
||||||
|
* port: Value.number({ name: 'Port', required: true, default: 8080, integer: true }),
|
||||||
|
* })),
|
||||||
|
* })
|
||||||
|
* const disabled = spec.disable({ name: true, settings: { debug: true } }, 'Managed by the system')
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
disable(
|
||||||
|
keys: FilterKeys<Type>,
|
||||||
|
message: string,
|
||||||
|
): InputSpec<Type, StaticValidatedAs> {
|
||||||
|
const newSpec: Record<string, Value<any>> = {}
|
||||||
|
for (const k in this.spec) {
|
||||||
|
const filterVal = (keys as any)[k]
|
||||||
|
const value = (this.spec as any)[k] as Value<any>
|
||||||
|
if (!filterVal) {
|
||||||
|
newSpec[k] = value
|
||||||
|
} else if (filterVal === true) {
|
||||||
|
newSpec[k] = value.withDisabled(message)
|
||||||
|
} else if (typeof filterVal === 'object' && filterVal !== null) {
|
||||||
|
const objectMeta = value._objectSpec
|
||||||
|
if (objectMeta) {
|
||||||
|
const disabledInner = objectMeta.inputSpec.disable(filterVal, message)
|
||||||
|
newSpec[k] = Value.object(objectMeta.params, disabledInner)
|
||||||
|
} else {
|
||||||
|
newSpec[k] = value.withDisabled(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const newValidator = z.object(
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(newSpec).map(([k, v]) => [k, v.validator]),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return new InputSpec(newSpec as any, newValidator as any) as any
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a key path to its corresponding display name path.
|
||||||
|
* Each key is mapped to the `name` property of its built {@link ValueSpec}.
|
||||||
|
* Recurses into `Value.object` sub-specs for nested paths.
|
||||||
|
*
|
||||||
|
* @param path - Typed tuple of field keys (e.g. `["settings", "debug"]`)
|
||||||
|
* @param options - Build options providing effects and prefill data
|
||||||
|
* @returns Array of display names (e.g. `["Settings", "Debug"]`)
|
||||||
|
*/
|
||||||
|
async namePath<OuterType>(
|
||||||
|
path: KeyPaths<Type>,
|
||||||
|
options: LazyBuildOptions<OuterType>,
|
||||||
|
): Promise<string[]> {
|
||||||
|
if (path.length === 0) return []
|
||||||
|
const [key, ...rest] = path as [string, ...string[]]
|
||||||
|
const value = (this.spec as any)[key] as Value<any> | undefined
|
||||||
|
if (!value) return []
|
||||||
|
const built = await value.build(options as any)
|
||||||
|
const name =
|
||||||
|
'name' in built.spec ? (built.spec as { name: string }).name : key
|
||||||
|
if (rest.length === 0) return [name]
|
||||||
|
const objectMeta = value._objectSpec
|
||||||
|
if (objectMeta) {
|
||||||
|
const innerNames = await objectMeta.inputSpec.namePath(
|
||||||
|
rest as any,
|
||||||
|
options,
|
||||||
|
)
|
||||||
|
return [name, ...innerNames]
|
||||||
|
}
|
||||||
|
return [name]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a key path to the description of the target field.
|
||||||
|
* Recurses into `Value.object` sub-specs for nested paths.
|
||||||
|
*
|
||||||
|
* @param path - Typed tuple of field keys (e.g. `["settings", "debug"]`)
|
||||||
|
* @param options - Build options providing effects and prefill data
|
||||||
|
* @returns The description string, or `null` if the field has no description or was not found
|
||||||
|
*/
|
||||||
|
async description<OuterType>(
|
||||||
|
path: KeyPaths<Type>,
|
||||||
|
options: LazyBuildOptions<OuterType>,
|
||||||
|
): Promise<string | null> {
|
||||||
|
if (path.length === 0) return null
|
||||||
|
const [key, ...rest] = path as [string, ...string[]]
|
||||||
|
const value = (this.spec as any)[key] as Value<any> | undefined
|
||||||
|
if (!value) return null
|
||||||
|
if (rest.length === 0) {
|
||||||
|
const built = await value.build(options as any)
|
||||||
|
return 'description' in built.spec
|
||||||
|
? (built.spec as { description: string | null }).description
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
const objectMeta = value._objectSpec
|
||||||
|
if (objectMeta) {
|
||||||
|
return objectMeta.inputSpec.description(rest as any, options)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new InputSpec filtered to only include keys present in the given partial object.
|
||||||
|
* For nested `Value.object` fields, recurses into the partial value to filter sub-fields.
|
||||||
|
*
|
||||||
|
* @param partial - A deep-partial object whose defined keys determine which fields to keep
|
||||||
|
*/
|
||||||
|
filterFromPartial(
|
||||||
|
partial: DeepPartial<Type>,
|
||||||
|
): InputSpec<
|
||||||
|
DeepPartial<Type> & DeepPartial<StaticValidatedAs>,
|
||||||
|
DeepPartial<StaticValidatedAs>
|
||||||
|
> {
|
||||||
|
const newSpec: Record<string, Value<any>> = {}
|
||||||
|
for (const k of Object.keys(partial)) {
|
||||||
|
const value = (this.spec as any)[k] as Value<any> | undefined
|
||||||
|
if (!value) continue
|
||||||
|
const objectMeta = value._objectSpec
|
||||||
|
if (objectMeta) {
|
||||||
|
const partialVal = (partial as any)[k]
|
||||||
|
if (typeof partialVal === 'object' && partialVal !== null) {
|
||||||
|
const filteredInner =
|
||||||
|
objectMeta.inputSpec.filterFromPartial(partialVal)
|
||||||
|
newSpec[k] = Value.object(objectMeta.params, filteredInner)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newSpec[k] = value
|
||||||
|
}
|
||||||
|
const newValidator = z.object(
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(newSpec).map(([k, v]) => [k, v.validator]),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return new InputSpec(newSpec as any, newValidator as any) as any
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new InputSpec with fields disabled based on which keys are present in the given partial object.
|
||||||
|
* For nested `Value.object` fields, recurses into the partial value to disable sub-fields.
|
||||||
|
* All fields remain in the spec — disabled fields simply cannot be edited by the user.
|
||||||
|
*
|
||||||
|
* @param partial - A deep-partial object whose defined keys determine which fields to disable
|
||||||
|
* @param message - The reason the fields are disabled, displayed to the user
|
||||||
|
*/
|
||||||
|
disableFromPartial(
|
||||||
|
partial: DeepPartial<Type>,
|
||||||
|
message: string,
|
||||||
|
): InputSpec<Type, StaticValidatedAs> {
|
||||||
|
const newSpec: Record<string, Value<any>> = {}
|
||||||
|
for (const k in this.spec) {
|
||||||
|
const value = (this.spec as any)[k] as Value<any>
|
||||||
|
if (!(k in (partial as any))) {
|
||||||
|
newSpec[k] = value
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const objectMeta = value._objectSpec
|
||||||
|
if (objectMeta) {
|
||||||
|
const partialVal = (partial as any)[k]
|
||||||
|
if (typeof partialVal === 'object' && partialVal !== null) {
|
||||||
|
const disabledInner = objectMeta.inputSpec.disableFromPartial(
|
||||||
|
partialVal,
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
newSpec[k] = Value.object(objectMeta.params, disabledInner)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newSpec[k] = value.withDisabled(message)
|
||||||
|
}
|
||||||
|
const newValidator = z.object(
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(newSpec).map(([k, v]) => [k, v.validator]),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return new InputSpec(newSpec as any, newValidator as any) as any
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an `InputSpec` from a plain record of {@link Value} entries.
|
* Creates an `InputSpec` from a plain record of {@link Value} entries.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -70,6 +70,11 @@ export class Value<
|
|||||||
) {}
|
) {}
|
||||||
public _TYPE: Type = null as any as Type
|
public _TYPE: Type = null as any as Type
|
||||||
public _PARTIAL: DeepPartial<Type> = null as any as DeepPartial<Type>
|
public _PARTIAL: DeepPartial<Type> = null as any as DeepPartial<Type>
|
||||||
|
/** @internal Used by {@link InputSpec.filter} to support nested filtering of object-typed fields. */
|
||||||
|
_objectSpec?: {
|
||||||
|
inputSpec: InputSpec<any, any>
|
||||||
|
params: { name: string; description?: string | null }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Displays a boolean toggle to enable/disable
|
* @description Displays a boolean toggle to enable/disable
|
||||||
@@ -987,7 +992,7 @@ export class Value<
|
|||||||
},
|
},
|
||||||
spec: InputSpec<Type, StaticValidatedAs>,
|
spec: InputSpec<Type, StaticValidatedAs>,
|
||||||
) {
|
) {
|
||||||
return new Value<Type, StaticValidatedAs>(async (options) => {
|
const value = new Value<Type, StaticValidatedAs>(async (options) => {
|
||||||
const built = await spec.build(options as any)
|
const built = await spec.build(options as any)
|
||||||
return {
|
return {
|
||||||
spec: {
|
spec: {
|
||||||
@@ -1000,6 +1005,8 @@ export class Value<
|
|||||||
validator: built.validator,
|
validator: built.validator,
|
||||||
}
|
}
|
||||||
}, spec.validator)
|
}, spec.validator)
|
||||||
|
value._objectSpec = { inputSpec: spec, params: a }
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Displays a file upload input field.
|
* Displays a file upload input field.
|
||||||
@@ -1333,6 +1340,25 @@ export class Value<
|
|||||||
}, z.any())
|
}, z.any())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new Value that produces the same field spec but with `disabled` set to the given message.
|
||||||
|
* The field remains in the form but cannot be edited by the user.
|
||||||
|
*
|
||||||
|
* @param message - The reason the field is disabled, displayed to the user
|
||||||
|
*/
|
||||||
|
withDisabled(message: string): Value<Type, StaticValidatedAs, OuterType> {
|
||||||
|
const original = this
|
||||||
|
const v = new Value<Type, StaticValidatedAs, OuterType>(async (options) => {
|
||||||
|
const built = await original.build(options)
|
||||||
|
return {
|
||||||
|
spec: { ...built.spec, disabled: message } as ValueSpec,
|
||||||
|
validator: built.validator,
|
||||||
|
}
|
||||||
|
}, this.validator)
|
||||||
|
v._objectSpec = this._objectSpec
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms the validated output value using a mapping function.
|
* Transforms the validated output value using a mapping function.
|
||||||
* The form field itself remains unchanged, but the value is transformed after validation.
|
* The form field itself remains unchanged, but the value is transformed after validation.
|
||||||
|
|||||||
@@ -16,10 +16,12 @@ export type GetInput<A extends Record<string, any>> = (options: {
|
|||||||
prefill: T.DeepPartial<A> | null
|
prefill: T.DeepPartial<A> | null
|
||||||
}) => Promise<null | void | undefined | T.DeepPartial<A>>
|
}) => Promise<null | void | undefined | T.DeepPartial<A>>
|
||||||
|
|
||||||
export type MaybeFn<T> = T | ((options: { effects: T.Effects }) => Promise<T>)
|
export type MaybeFn<T, Opts = { effects: T.Effects }> =
|
||||||
function callMaybeFn<T>(
|
| T
|
||||||
maybeFn: MaybeFn<T>,
|
| ((options: Opts) => Promise<T>)
|
||||||
options: { effects: T.Effects },
|
function callMaybeFn<T, Opts = { effects: T.Effects }>(
|
||||||
|
maybeFn: MaybeFn<T, Opts>,
|
||||||
|
options: Opts,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
if (maybeFn instanceof Function) {
|
if (maybeFn instanceof Function) {
|
||||||
return maybeFn(options)
|
return maybeFn(options)
|
||||||
@@ -57,7 +59,13 @@ export class Action<Id extends T.ActionId, Type extends Record<string, any>>
|
|||||||
private constructor(
|
private constructor(
|
||||||
readonly id: Id,
|
readonly id: Id,
|
||||||
private readonly metadataFn: MaybeFn<T.ActionMetadata>,
|
private readonly metadataFn: MaybeFn<T.ActionMetadata>,
|
||||||
private readonly inputSpec: MaybeInputSpec<Type>,
|
private readonly inputSpec: MaybeFn<
|
||||||
|
MaybeInputSpec<Type>,
|
||||||
|
{
|
||||||
|
effects: T.Effects
|
||||||
|
prefill: unknown | null
|
||||||
|
}
|
||||||
|
>,
|
||||||
private readonly getInputFn: GetInput<Type>,
|
private readonly getInputFn: GetInput<Type>,
|
||||||
private readonly runFn: Run<Type>,
|
private readonly runFn: Run<Type>,
|
||||||
) {}
|
) {}
|
||||||
@@ -67,7 +75,13 @@ export class Action<Id extends T.ActionId, Type extends Record<string, any>>
|
|||||||
>(
|
>(
|
||||||
id: Id,
|
id: Id,
|
||||||
metadata: MaybeFn<Omit<T.ActionMetadata, 'hasInput'>>,
|
metadata: MaybeFn<Omit<T.ActionMetadata, 'hasInput'>>,
|
||||||
inputSpec: InputSpecType,
|
inputSpec: MaybeFn<
|
||||||
|
InputSpecType,
|
||||||
|
{
|
||||||
|
effects: T.Effects
|
||||||
|
prefill: unknown | null
|
||||||
|
}
|
||||||
|
>,
|
||||||
getInput: GetInput<ExtractInputSpecType<InputSpecType>>,
|
getInput: GetInput<ExtractInputSpecType<InputSpecType>>,
|
||||||
run: Run<ExtractInputSpecType<InputSpecType>>,
|
run: Run<ExtractInputSpecType<InputSpecType>>,
|
||||||
): Action<Id, ExtractInputSpecType<InputSpecType>> {
|
): Action<Id, ExtractInputSpecType<InputSpecType>> {
|
||||||
@@ -111,9 +125,12 @@ export class Action<Id extends T.ActionId, Type extends Record<string, any>>
|
|||||||
}): Promise<T.ActionInput> {
|
}): Promise<T.ActionInput> {
|
||||||
let spec = {}
|
let spec = {}
|
||||||
if (this.inputSpec) {
|
if (this.inputSpec) {
|
||||||
const built = await this.inputSpec.build(options)
|
const inputSpec = await callMaybeFn(this.inputSpec, options)
|
||||||
this.prevInputSpec[options.effects.eventId!] = built
|
const built = await inputSpec?.build(options)
|
||||||
spec = built.spec
|
if (built) {
|
||||||
|
this.prevInputSpec[options.effects.eventId!] = built
|
||||||
|
spec = built.spec
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
eventId: options.effects.eventId!,
|
eventId: options.effects.eventId!,
|
||||||
|
|||||||
@@ -8,6 +8,20 @@ export * as types from './types'
|
|||||||
export * as T from './types'
|
export * as T from './types'
|
||||||
export * as yaml from 'yaml'
|
export * as yaml from 'yaml'
|
||||||
export * as inits from './inits'
|
export * as inits from './inits'
|
||||||
export { z } from 'zod'
|
import { z as _z } from 'zod'
|
||||||
|
import { zodDeepPartial } from 'zod-deep-partial'
|
||||||
|
import { DeepPartial } from './types'
|
||||||
|
|
||||||
|
type ZodDeepPartial = <T>(a: _z.ZodType<T>) => _z.ZodType<DeepPartial<T>>
|
||||||
|
|
||||||
|
export const z: typeof _z & {
|
||||||
|
deepPartial: ZodDeepPartial
|
||||||
|
} = Object.assign(_z, { deepPartial: zodDeepPartial as ZodDeepPartial })
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
export namespace z {
|
||||||
|
export type infer<T extends { _zod: { output: any } }> = T['_zod']['output']
|
||||||
|
export type input<T extends { _zod: { input: any } }> = T['_zod']['input']
|
||||||
|
export type output<T extends { _zod: { output: any } }> = T['_zod']['output']
|
||||||
|
}
|
||||||
|
|
||||||
export * as utils from './util'
|
export * as utils from './util'
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export * as inputSpecTypes from './actions/input/inputSpecTypes'
|
export * as inputSpecTypes from './actions/input/inputSpecTypes'
|
||||||
|
import { InputSpec as InputSpecClass } from './actions/input/builder/inputSpec'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DependencyRequirement,
|
DependencyRequirement,
|
||||||
@@ -267,3 +268,8 @@ export type AllowReadonly<T> =
|
|||||||
| {
|
| {
|
||||||
readonly [P in keyof T]: AllowReadonly<T[P]>
|
readonly [P in keyof T]: AllowReadonly<T[P]>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type InputSpec<
|
||||||
|
Type extends StaticValidatedAs,
|
||||||
|
StaticValidatedAs extends Record<string, unknown> = Type,
|
||||||
|
> = InputSpecClass<Type, StaticValidatedAs>
|
||||||
|
|||||||
13
sdk/base/package-lock.json
generated
13
sdk/base/package-lock.json
generated
@@ -14,7 +14,8 @@
|
|||||||
"isomorphic-fetch": "^3.0.0",
|
"isomorphic-fetch": "^3.0.0",
|
||||||
"mime": "^4.0.7",
|
"mime": "^4.0.7",
|
||||||
"yaml": "^2.7.1",
|
"yaml": "^2.7.1",
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6",
|
||||||
|
"zod-deep-partial": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.4.0",
|
"@types/jest": "^29.4.0",
|
||||||
@@ -5006,9 +5007,19 @@
|
|||||||
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
|
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
|
||||||
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
|
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"funding": {
|
"funding": {
|
||||||
"url": "https://github.com/sponsors/colinhacks"
|
"url": "https://github.com/sponsors/colinhacks"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/zod-deep-partial": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/zod-deep-partial/-/zod-deep-partial-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-dXfte+/YN0aFYs0kMGz6xfPQWEYNaKz/LsbfxrbwL+oY3l/aR9HOBTyWCpHZ5AJXMGWKSq+0X0oVPpRliUFcjQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"zod": "^4.1.13"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,8 @@
|
|||||||
"isomorphic-fetch": "^3.0.0",
|
"isomorphic-fetch": "^3.0.0",
|
||||||
"mime": "^4.0.7",
|
"mime": "^4.0.7",
|
||||||
"yaml": "^2.7.1",
|
"yaml": "^2.7.1",
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6",
|
||||||
|
"zod-deep-partial": "^1.2.0"
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"trailingComma": "all",
|
"trailingComma": "all",
|
||||||
|
|||||||
@@ -91,11 +91,15 @@ function filterUndefined<A>(a: A): A {
|
|||||||
* @typeParam Raw - The native type the file format parses to (e.g. `Record<string, unknown>` for JSON)
|
* @typeParam Raw - The native type the file format parses to (e.g. `Record<string, unknown>` for JSON)
|
||||||
* @typeParam Transformed - The application-level type after transformation
|
* @typeParam Transformed - The application-level type after transformation
|
||||||
*/
|
*/
|
||||||
export type Transformers<Raw = unknown, Transformed = unknown> = {
|
export type Transformers<
|
||||||
|
Raw = unknown,
|
||||||
|
Transformed = unknown,
|
||||||
|
Validated extends Transformed = Transformed,
|
||||||
|
> = {
|
||||||
/** Transform raw parsed data into the application type */
|
/** Transform raw parsed data into the application type */
|
||||||
onRead: (value: Raw) => Transformed
|
onRead: (value: Raw) => Transformed
|
||||||
/** Transform application data back into the raw format for writing */
|
/** Transform application data back into the raw format for writing */
|
||||||
onWrite: (value: Transformed) => Raw
|
onWrite: (value: Validated) => Raw
|
||||||
}
|
}
|
||||||
|
|
||||||
type ToPath = string | { base: PathBase; subpath: string }
|
type ToPath = string | { base: PathBase; subpath: string }
|
||||||
@@ -483,7 +487,7 @@ export class FileHelper<A> {
|
|||||||
toFile: (dataIn: Raw) => string,
|
toFile: (dataIn: Raw) => string,
|
||||||
fromFile: (rawData: string) => Raw,
|
fromFile: (rawData: string) => Raw,
|
||||||
validate: (data: Transformed) => A,
|
validate: (data: Transformed) => A,
|
||||||
transformers: Transformers<Raw, Transformed> | undefined,
|
transformers: Transformers<Raw, Transformed, A> | undefined,
|
||||||
) {
|
) {
|
||||||
return FileHelper.raw<A>(
|
return FileHelper.raw<A>(
|
||||||
path,
|
path,
|
||||||
@@ -493,7 +497,12 @@ export class FileHelper<A> {
|
|||||||
}
|
}
|
||||||
return toFile(inData as any as Raw)
|
return toFile(inData as any as Raw)
|
||||||
},
|
},
|
||||||
fromFile,
|
(fileData) => {
|
||||||
|
if (transformers) {
|
||||||
|
return transformers.onRead(fromFile(fileData))
|
||||||
|
}
|
||||||
|
return fromFile(fileData)
|
||||||
|
},
|
||||||
validate as (a: unknown) => A,
|
validate as (a: unknown) => A,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -509,12 +518,12 @@ export class FileHelper<A> {
|
|||||||
static string<A extends Transformed, Transformed = string>(
|
static string<A extends Transformed, Transformed = string>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers: Transformers<string, Transformed>,
|
transformers: Transformers<string, Transformed, A>,
|
||||||
): FileHelper<A>
|
): FileHelper<A>
|
||||||
static string<A extends Transformed, Transformed = string>(
|
static string<A extends Transformed, Transformed = string>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape?: Validator<Transformed, A>,
|
shape?: Validator<Transformed, A>,
|
||||||
transformers?: Transformers<string, Transformed>,
|
transformers?: Transformers<string, Transformed, A>,
|
||||||
) {
|
) {
|
||||||
return FileHelper.rawTransformed<A, string, Transformed>(
|
return FileHelper.rawTransformed<A, string, Transformed>(
|
||||||
path,
|
path,
|
||||||
@@ -531,10 +540,16 @@ export class FileHelper<A> {
|
|||||||
/**
|
/**
|
||||||
* Create a File Helper for a .json file.
|
* Create a File Helper for a .json file.
|
||||||
*/
|
*/
|
||||||
static json<A>(
|
static json<A>(path: ToPath, shape: Validator<unknown, A>): FileHelper<A>
|
||||||
|
static json<A extends Transformed, Transformed = unknown>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<unknown, A>,
|
shape: Validator<unknown, A>,
|
||||||
transformers?: Transformers,
|
transformers: Transformers<unknown, Transformed, A>,
|
||||||
|
): FileHelper<A>
|
||||||
|
static json<A extends Transformed, Transformed = unknown>(
|
||||||
|
path: ToPath,
|
||||||
|
shape: Validator<unknown, A>,
|
||||||
|
transformers?: Transformers<unknown, Transformed, A>,
|
||||||
) {
|
) {
|
||||||
return FileHelper.rawTransformed(
|
return FileHelper.rawTransformed(
|
||||||
path,
|
path,
|
||||||
@@ -555,12 +570,12 @@ export class FileHelper<A> {
|
|||||||
static yaml<A extends Transformed, Transformed = Record<string, unknown>>(
|
static yaml<A extends Transformed, Transformed = Record<string, unknown>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers: Transformers<Record<string, unknown>, Transformed>,
|
transformers: Transformers<Record<string, unknown>, Transformed, A>,
|
||||||
): FileHelper<A>
|
): FileHelper<A>
|
||||||
static yaml<A extends Transformed, Transformed = Record<string, unknown>>(
|
static yaml<A extends Transformed, Transformed = Record<string, unknown>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers?: Transformers<Record<string, unknown>, Transformed>,
|
transformers?: Transformers<Record<string, unknown>, Transformed, A>,
|
||||||
) {
|
) {
|
||||||
return FileHelper.rawTransformed<A, Record<string, unknown>, Transformed>(
|
return FileHelper.rawTransformed<A, Record<string, unknown>, Transformed>(
|
||||||
path,
|
path,
|
||||||
@@ -581,12 +596,12 @@ export class FileHelper<A> {
|
|||||||
static toml<A extends Transformed, Transformed = Record<string, unknown>>(
|
static toml<A extends Transformed, Transformed = Record<string, unknown>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers: Transformers<Record<string, unknown>, Transformed>,
|
transformers: Transformers<Record<string, unknown>, Transformed, A>,
|
||||||
): FileHelper<A>
|
): FileHelper<A>
|
||||||
static toml<A extends Transformed, Transformed = Record<string, unknown>>(
|
static toml<A extends Transformed, Transformed = Record<string, unknown>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers?: Transformers<Record<string, unknown>, Transformed>,
|
transformers?: Transformers<Record<string, unknown>, Transformed, A>,
|
||||||
) {
|
) {
|
||||||
return FileHelper.rawTransformed<A, Record<string, unknown>, Transformed>(
|
return FileHelper.rawTransformed<A, Record<string, unknown>, Transformed>(
|
||||||
path,
|
path,
|
||||||
@@ -611,13 +626,13 @@ export class FileHelper<A> {
|
|||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
options: INI.EncodeOptions & INI.DecodeOptions,
|
options: INI.EncodeOptions & INI.DecodeOptions,
|
||||||
transformers: Transformers<Record<string, unknown>, Transformed>,
|
transformers: Transformers<Record<string, unknown>, Transformed, A>,
|
||||||
): FileHelper<A>
|
): FileHelper<A>
|
||||||
static ini<A extends Transformed, Transformed = Record<string, unknown>>(
|
static ini<A extends Transformed, Transformed = Record<string, unknown>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
options?: INI.EncodeOptions & INI.DecodeOptions,
|
options?: INI.EncodeOptions & INI.DecodeOptions,
|
||||||
transformers?: Transformers<Record<string, unknown>, Transformed>,
|
transformers?: Transformers<Record<string, unknown>, Transformed, A>,
|
||||||
): FileHelper<A> {
|
): FileHelper<A> {
|
||||||
return FileHelper.rawTransformed<A, Record<string, unknown>, Transformed>(
|
return FileHelper.rawTransformed<A, Record<string, unknown>, Transformed>(
|
||||||
path,
|
path,
|
||||||
@@ -640,12 +655,12 @@ export class FileHelper<A> {
|
|||||||
static env<A extends Transformed, Transformed = Record<string, string>>(
|
static env<A extends Transformed, Transformed = Record<string, string>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers: Transformers<Record<string, string>, Transformed>,
|
transformers: Transformers<Record<string, string>, Transformed, A>,
|
||||||
): FileHelper<A>
|
): FileHelper<A>
|
||||||
static env<A extends Transformed, Transformed = Record<string, string>>(
|
static env<A extends Transformed, Transformed = Record<string, string>>(
|
||||||
path: ToPath,
|
path: ToPath,
|
||||||
shape: Validator<Transformed, A>,
|
shape: Validator<Transformed, A>,
|
||||||
transformers?: Transformers<Record<string, string>, Transformed>,
|
transformers?: Transformers<Record<string, string>, Transformed, A>,
|
||||||
) {
|
) {
|
||||||
return FileHelper.rawTransformed<A, Record<string, string>, Transformed>(
|
return FileHelper.rawTransformed<A, Record<string, string>, Transformed>(
|
||||||
path,
|
path,
|
||||||
|
|||||||
13
sdk/package/package-lock.json
generated
13
sdk/package/package-lock.json
generated
@@ -18,7 +18,8 @@
|
|||||||
"isomorphic-fetch": "^3.0.0",
|
"isomorphic-fetch": "^3.0.0",
|
||||||
"mime": "^4.0.7",
|
"mime": "^4.0.7",
|
||||||
"yaml": "^2.7.1",
|
"yaml": "^2.7.1",
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6",
|
||||||
|
"zod-deep-partial": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.4.0",
|
"@types/jest": "^29.4.0",
|
||||||
@@ -5232,9 +5233,19 @@
|
|||||||
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
|
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
|
||||||
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
|
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"peer": true,
|
||||||
"funding": {
|
"funding": {
|
||||||
"url": "https://github.com/sponsors/colinhacks"
|
"url": "https://github.com/sponsors/colinhacks"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/zod-deep-partial": {
|
||||||
|
"version": "1.4.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/zod-deep-partial/-/zod-deep-partial-1.4.4.tgz",
|
||||||
|
"integrity": "sha512-aWkPl7hVStgE01WzbbSxCgX4O+sSpgt8JOjvFUtMTF75VgL6MhWQbiZi+AWGN85SfSTtI9gsOtL1vInoqfDVaA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"zod": "^4.1.13"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,8 @@
|
|||||||
"isomorphic-fetch": "^3.0.0",
|
"isomorphic-fetch": "^3.0.0",
|
||||||
"mime": "^4.0.7",
|
"mime": "^4.0.7",
|
||||||
"yaml": "^2.7.1",
|
"yaml": "^2.7.1",
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6",
|
||||||
|
"zod-deep-partial": "^1.2.0"
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"trailingComma": "all",
|
"trailingComma": "all",
|
||||||
|
|||||||
3
web/package-lock.json
generated
3
web/package-lock.json
generated
@@ -126,7 +126,8 @@
|
|||||||
"isomorphic-fetch": "^3.0.0",
|
"isomorphic-fetch": "^3.0.0",
|
||||||
"mime": "^4.0.7",
|
"mime": "^4.0.7",
|
||||||
"yaml": "^2.7.1",
|
"yaml": "^2.7.1",
|
||||||
"zod": "^4.3.6"
|
"zod": "^4.3.6",
|
||||||
|
"zod-deep-partial": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.4.0",
|
"@types/jest": "^29.4.0",
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ export class StateService {
|
|||||||
|
|
||||||
await this.api.execute({
|
await this.api.execute({
|
||||||
guid: this.dataDriveGuid,
|
guid: this.dataDriveGuid,
|
||||||
// @ts-expect-error TODO: backend should make password optional for restore/transfer
|
|
||||||
password: password ? await this.api.encrypt(password) : null,
|
password: password ? await this.api.encrypt(password) : null,
|
||||||
name,
|
name,
|
||||||
hostname,
|
hostname,
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { ISB } from '@start9labs/start-sdk'
|
import { ISB } from '@start9labs/start-sdk'
|
||||||
|
|
||||||
export async function configBuilderToSpec(
|
export async function configBuilderToSpec(builder: ISB.InputSpec<any>) {
|
||||||
builder: ISB.InputSpec<Record<string, unknown>>,
|
|
||||||
) {
|
|
||||||
return builder.build({} as any).then(a => a.spec)
|
return builder.build({} as any).then(a => a.spec)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user