This commit is contained in:
81
store/src/routes/node/validations/inputs.ts
Normal file
81
store/src/routes/node/validations/inputs.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { z } from "@hono/zod-openapi";
|
||||
|
||||
const DefaultOptionsSchema = z.object({
|
||||
internal: z.boolean().optional(),
|
||||
external: z.boolean().optional(),
|
||||
setting: z.string().optional(),
|
||||
label: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
accepts: z.array(z.string()).optional(),
|
||||
hidden: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const NodeInputFloatSchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("float"),
|
||||
element: z.literal("slider").optional(),
|
||||
value: z.number().optional(),
|
||||
min: z.number().optional(),
|
||||
max: z.number().optional(),
|
||||
step: z.number().optional(),
|
||||
});
|
||||
|
||||
const NodeInputIntegerSchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("integer"),
|
||||
element: z.literal("slider").optional(),
|
||||
value: z.number().optional(),
|
||||
min: z.number().optional(),
|
||||
max: z.number().optional(),
|
||||
});
|
||||
|
||||
const NodeInputBooleanSchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("boolean"),
|
||||
value: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const NodeInputSelectSchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("select"),
|
||||
options: z.array(z.string()).optional(),
|
||||
value: z.number().optional(),
|
||||
});
|
||||
|
||||
const NodeInputSeedSchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("seed"),
|
||||
value: z.number().optional(),
|
||||
});
|
||||
|
||||
const NodeInputVec3Schema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("vec3"),
|
||||
value: z.array(z.number()).optional(),
|
||||
});
|
||||
|
||||
const NodeInputGeometrySchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("geometry"),
|
||||
});
|
||||
|
||||
const NodeInputPathSchema = z.object({
|
||||
...DefaultOptionsSchema.shape,
|
||||
type: z.literal("path"),
|
||||
});
|
||||
|
||||
export const NodeInputSchema = z
|
||||
.union([
|
||||
NodeInputSeedSchema,
|
||||
NodeInputBooleanSchema,
|
||||
NodeInputFloatSchema,
|
||||
NodeInputIntegerSchema,
|
||||
NodeInputSelectSchema,
|
||||
NodeInputSeedSchema,
|
||||
NodeInputVec3Schema,
|
||||
NodeInputGeometrySchema,
|
||||
NodeInputPathSchema,
|
||||
])
|
||||
.openapi("NodeInput");
|
||||
|
||||
export type NodeInput = z.infer<typeof NodeInputSchema>;
|
||||
31
store/src/routes/node/validations/types.ts
Normal file
31
store/src/routes/node/validations/types.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { z } from "zod";
|
||||
import { NodeInputSchema } from "./inputs.ts";
|
||||
|
||||
export type NodeId = `${string}/${string}/${string}`;
|
||||
|
||||
export const idRegex = /[a-z0-9-]+/i;
|
||||
|
||||
const idSchema = z
|
||||
.string()
|
||||
.regex(
|
||||
new RegExp(
|
||||
`^(${idRegex.source})/(${idRegex.source})/(${idRegex.source})$`,
|
||||
),
|
||||
"Invalid id format",
|
||||
);
|
||||
|
||||
export const NodeDefinitionSchema = z
|
||||
.object({
|
||||
id: idSchema,
|
||||
inputs: z.record(NodeInputSchema).optional(),
|
||||
outputs: z.array(z.string()).optional(),
|
||||
meta: z
|
||||
.object({
|
||||
description: z.string().optional(),
|
||||
title: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
.openapi("NodeDefinition");
|
||||
|
||||
export type NodeDefinition = z.infer<typeof NodeDefinitionSchema>;
|
||||
Reference in New Issue
Block a user