diff --git a/apps/frontend/src/lib/components/blocks/field-control/json-control.svelte b/apps/frontend/src/lib/components/blocks/field-control/json-control.svelte index 6f8837c5a..9d212962e 100644 --- a/apps/frontend/src/lib/components/blocks/field-control/json-control.svelte +++ b/apps/frontend/src/lib/components/blocks/field-control/json-control.svelte @@ -7,7 +7,7 @@ export let onValueChange: (value: Json) => void let content: Content = { - text: "{}", + text: JSON.stringify(value ?? {}), json: value ?? {}, } diff --git a/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/json-cell.svelte b/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/json-cell.svelte index 950f905fd..13180e952 100644 --- a/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/json-cell.svelte +++ b/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/json-cell.svelte @@ -14,7 +14,7 @@ export let isSelected: boolean export let field: JsonField export let recordId: string - export let onValueChange: (value: JsonValue) => void + export let onValueChange: (value: JsonValue | undefined) => void const updateCell = createMutation({ mutationKey: ["record", tableId, field.id.value, recordId], @@ -25,7 +25,7 @@ }) let content: Content = { - text: undefined, + text: JSON.stringify(value ?? {}), json: value ?? {}, } diff --git a/packages/table/src/modules/schema/fields/variants/json-field/json-field-constraint.vo.ts b/packages/table/src/modules/schema/fields/variants/json-field/json-field-constraint.vo.ts index bfd5be6ee..8b98f4885 100644 --- a/packages/table/src/modules/schema/fields/variants/json-field/json-field-constraint.vo.ts +++ b/packages/table/src/modules/schema/fields/variants/json-field/json-field-constraint.vo.ts @@ -2,6 +2,7 @@ import { Some } from "@undb/domain" import { z } from "@undb/zod" import type { FormFieldVO } from "../../../../forms/form/form-field.vo" import { FieldConstraintVO, baseFieldConstraint } from "../../field-constraint.vo" +import { jsonSchemaValue } from "./json-field-value.vo" export const jsonFieldConstraint = z .object({ @@ -20,8 +21,7 @@ export class JsonFieldConstraint extends FieldConstraintVO }) } override get schema() { - const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]) - let base: z.ZodTypeAny = z.lazy(() => z.union([literalSchema, z.array(base), z.record(base)])) + let base: z.ZodTypeAny = jsonSchemaValue if (!this.props.required) { base = base.optional().nullable() diff --git a/packages/table/src/modules/schema/fields/variants/json-field/json-field-value.vo.ts b/packages/table/src/modules/schema/fields/variants/json-field/json-field-value.vo.ts index 8c64abb36..2bbf7a213 100644 --- a/packages/table/src/modules/schema/fields/variants/json-field/json-field-value.vo.ts +++ b/packages/table/src/modules/schema/fields/variants/json-field/json-field-value.vo.ts @@ -5,10 +5,15 @@ import { FieldValueObject } from "../../field-value" export type { JsonValue } from "type-fest" -const baseSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]) -const jsonSchemaValue: z.ZodTypeAny = z.lazy(() => - z.union([baseSchema, z.array(jsonSchemaValue), z.record(jsonSchemaValue)]), -) +const baseSchema = z.union([ + z.string(), + z.number(), + z.boolean(), + z.null(), + z.array(z.any()), + z.record(z.any(), z.any()), +]) +export const jsonSchemaValue = baseSchema export const mutateJsonFieldValueSchema = jsonSchemaValue