import * as t from "io-ts" import type { ZodMiniType } from "zod/mini" // Common import { AssetSchema as rawAssetSchema } from "./common/asset" import { EmbedSchema as rawEmbedSchema } from "./common/embed" import { HexaColorCodeSchema as rawHexaColorCodeSchema } from "./common/hexaColorCode" import { NonEmptyStringSchema as rawNonEmptyStringSchema } from "./common/nonEmptyString" import { WidgetKeySchema as rawWidgetKeySchema } from "./common/widgetKey" // Content import { BooleanContentSchema as rawBooleanContentSchema } from "./content/boolean" import { BooleanLegacyCodec } from "./content/codec/boolean" import { DocumentLegacyCodec } from "./content/codec/document" import { EmbedLegacyCodec } from "./content/codec/embed" import { EmptyLegacyCodec } from "./content/codec/empty" import { ColorLegacyCodec, DateLegacyCodec, NumberLegacyCodec, RangeLegacyCodec, SelectLegacyCodec, TextLegacyCodec, TimestampLegacyCodec, } from "./content/codec/field" import { GeoPointLegacyCodec } from "./content/codec/geopoint" import { GroupItemLegacyCodec, GroupLegacyCodec } from "./content/codec/group" import { ImageLegacyCodec } from "./content/codec/image" import { IntegrationFieldLegacyCodec } from "./content/codec/integrationField" import type { LegacyCodec } from "./content/codec/legacyContentCtx" import { LinkLegacyCodec } from "./content/codec/link" import { NestableLegacyCodec } from "./content/codec/nestable" import { RepeatableLegacyCodec } from "./content/codec/repeatable" import { RichTextLegacyCodec } from "./content/codec/richText" import { SeparatorLegacyCodec } from "./content/codec/separator" import { CompositeSliceLegacyCodec, SharedSliceLegacyCodec, LegacySliceLegacyCodec, SliceLegacyCodec, } from "./content/codec/slice" import { SliceItemLegacyCodec, SlicesLegacyCodec } from "./content/codec/slices" import { TableLegacyCodec } from "./content/codec/table" import { UIDLegacyCodec } from "./content/codec/uid" import { WidgetLegacyCodec } from "./content/codec/widget" import { DocumentContentSchema as rawDocumentContentSchema } from "./content/document" import { EmbedContentSchema as rawEmbedContentSchema } from "./content/embed" import { EmptyContentSchema as rawEmptyContentSchema } from "./content/empty" import { ColorContentSchema as rawColorContentSchema, DateContentSchema as rawDateContentSchema, NumberContentSchema as rawNumberContentSchema, RangeContentSchema as rawRangeContentSchema, SelectContentSchema as rawSelectContentSchema, TextContentSchema as rawTextContentSchema, TimestampContentSchema as rawTimestampContentSchema, FieldContentSchema as rawFieldContentSchema, } from "./content/field" import { GeoPointContentSchema as rawGeoPointContentSchema } from "./content/geopoint" import { GroupContentSchema as rawGroupContentSchema, GroupItemContentSchema as rawGroupItemContentSchema, } from "./content/group" import { ImageContentSchema as rawImageContentSchema, ImageContentViewSchema as rawImageContentViewSchema, } from "./content/image" import { IntegrationFieldContentSchema as rawIntegrationFieldContentSchema } from "./content/integrationField" import { EmptyLinkContentSchema as rawEmptyLinkContentSchema, FilledLinkContentSchema as rawFilledLinkContentSchema, LinkContentSchema as rawLinkContentSchema, } from "./content/link" import { NestableContentSchema as rawNestableContentSchema } from "./content/nestable" import { RepeatableContentSchema as rawRepeatableContentSchema } from "./content/repeatable" import { RichTextContentSchema as rawRichTextContentSchema, RichTextContentBlockSchema as rawRichTextContentBlockSchema, RichTextContentSpanSchema as rawRichTextContentSpanSchema, } from "./content/richText" import { SeparatorContentSchema as rawSeparatorContentSchema } from "./content/separator" import { LegacySliceContentSchema as rawLegacySliceContentSchema, CompositeSliceContentSchema as rawCompositeSliceContentSchema, SharedSliceContentSchema as rawSharedSliceContentSchema, SliceContentSchema as rawSliceContentSchema, } from "./content/slice" import { LegacySliceItemContentSchema as rawLegacySliceItemContentSchema, CompositeSliceItemContentSchema as rawCompositeSliceItemContentSchema, SharedSliceItemContentSchema as rawSharedSliceItemContentSchema, SliceItemContentSchema as rawSliceItemContentSchema, SlicesContentSchema as rawSlicesContentSchema, } from "./content/slices" import { TableContentSchema as rawTableContentSchema } from "./content/table" import { UIDContentSchema as rawUIDContentSchema } from "./content/uid" import { WidgetContentSchema as rawWidgetContentSchema } from "./content/widget" // Model import { BooleanModelSchema as rawBooleanModelSchema } from "./model/boolean" import { ColorModelSchema as rawColorModelSchema } from "./model/color" import { StaticCustomTypeModelSchema as rawStaticCustomTypeModelSchema, DynamicCustomTypeModelSchema as rawDynamicCustomTypeModelSchema, StaticCustomTypeModelTabSchema as rawStaticCustomTypeModelTabSchema, DynamicCustomTypeModelTabSchema as rawDynamicCustomTypeModelTabSchema, } from "./model/customType" import { DateModelSchema as rawDateModelSchema } from "./model/date" import { EmbedModelSchema as rawEmbedModelSchema } from "./model/embed" import { GeoPointModelSchema as rawGeoPointModelSchema } from "./model/geopoint" import { GroupModelSchema as rawGroupModelSchema, NestedGroupModelSchema as rawNestedGroupModelSchema, } from "./model/group" import { ImageModelSchema as rawImageModelSchema } from "./model/image" import { IntegrationFieldModelSchema as rawIntegrationFieldModelSchema } from "./model/integrationField" import { LinkModelSchema as rawLinkModelSchema } from "./model/link" import { NestableModelSchema as rawNestableModelSchema } from "./model/nestable" import { NumberModelSchema as rawNumberModelSchema } from "./model/number" import { RangeModelSchema as rawRangeModelSchema } from "./model/range" import { RichTextModelSchema as rawRichTextModelSchema } from "./model/richText" import { SelectModelSchema as rawSelectModelSchema } from "./model/select" import { SeparatorModelSchema as rawSeparatorModelSchema } from "./model/separator" import { LegacySliceModelSchema as rawLegacySliceModelSchema, CompositeSliceModelSchema as rawCompositeSliceModelSchema, SharedSliceModelSchema as rawSharedSliceModelSchema, SharedSliceModelVariationSchema as rawSharedSliceModelVariationSchema, SharedSliceRefModelSchema as rawSharedSliceRefModelSchema, StaticSliceModelSchema as rawStaticSliceModelSchema, DynamicSliceModelSchema as rawDynamicSliceModelSchema, SliceContentModelSchema as rawSliceContentModelSchema, } from "./model/slice" import { DynamicSlicesModelSchema as rawDynamicSlicesModelSchema, StaticSlicesModelSchema as rawStaticSlicesModelSchema, } from "./model/slices" import { TableModelSchema as rawTableModelSchema } from "./model/table" import { TextModelSchema as rawTextModelSchema } from "./model/text" import { TimestampModelSchema as rawTimestampModelSchema } from "./model/timestamp" import { UIDModelSchema as rawUIDModelSchema } from "./model/uid" import { StaticWidgetModelSchema as rawStaticWidgetModelSchema, DynamicWidgetModelSchema as rawDynamicWidgetModelSchema, } from "./model/widget" // Common export const AssetSchema = toIOTS(rawAssetSchema) export const EmbedSchema = toIOTS(rawEmbedSchema) export const HexaColorCodeSchema = toIOTS(rawHexaColorCodeSchema) export const NonEmptyStringSchema = toIOTS(rawNonEmptyStringSchema) export const WidgetKeySchema = toIOTS(rawWidgetKeySchema) // Content export const BooleanContentSchema = toIOTS(rawBooleanContentSchema) export const ColorContentSchema = toIOTS(rawColorContentSchema) export const DateContentSchema = toIOTS(rawDateContentSchema) export const EmptyContentSchema = toIOTS(rawEmptyContentSchema) export const GeoPointContentSchema = toIOTS(rawGeoPointContentSchema) export const FilledLinkContentSchema = toIOTS(rawFilledLinkContentSchema) export const EmptyLinkContentSchema = toIOTS(rawEmptyLinkContentSchema) export const LinkContentSchema = toIOTS(rawLinkContentSchema) export const NumberContentSchema = toIOTS(rawNumberContentSchema) export const RangeContentSchema = toIOTS(rawRangeContentSchema) export const SelectContentSchema = toIOTS(rawSelectContentSchema) export const SeparatorContentSchema = toIOTS(rawSeparatorContentSchema) export const SharedSliceContentSchema = toIOTS(rawSharedSliceContentSchema) export const TextContentSchema = toIOTS(rawTextContentSchema) export const TimestampContentSchema = toIOTS(rawTimestampContentSchema) export const FieldContentSchema = toIOTS(rawFieldContentSchema) export const UIDContentSchema = toIOTS(rawUIDContentSchema) export const IntegrationFieldContentSchema = toIOTS(rawIntegrationFieldContentSchema) export const EmbedContentSchema = toIOTS(rawEmbedContentSchema) export const ImageContentViewSchema = toIOTS(rawImageContentViewSchema) export const ImageContentSchema = toIOTS(rawImageContentSchema) export const RichTextContentSchema = toIOTS(rawRichTextContentSchema) export const RichTextContentBlockSchema = toIOTS(rawRichTextContentBlockSchema) export const RichTextContentSpanSchema = toIOTS(rawRichTextContentSpanSchema) export const TableContentSchema = toIOTS(rawTableContentSchema) export const RepeatableContentSchema = toIOTS(rawRepeatableContentSchema) export const NestableContentSchema = toIOTS(rawNestableContentSchema) export const GroupContentSchema = toIOTS(rawGroupContentSchema) export const GroupItemContentSchema = toIOTS(rawGroupItemContentSchema) export const CompositeSliceContentSchema = toIOTS(rawCompositeSliceContentSchema) export const LegacySliceContentSchema = toIOTS(rawLegacySliceContentSchema) export const SliceContentSchema = toIOTS(rawSliceContentSchema) export const LegacySliceItemContentSchema = toIOTS(rawLegacySliceItemContentSchema) export const CompositeSliceItemContentSchema = toIOTS(rawCompositeSliceItemContentSchema) export const SharedSliceItemContentSchema = toIOTS(rawSharedSliceItemContentSchema) export const SliceItemContentSchema = toIOTS(rawSliceItemContentSchema) export const SlicesContentSchema = toIOTS(rawSlicesContentSchema) export const WidgetContentSchema = toIOTS(rawWidgetContentSchema) export const DocumentContentSchema = toIOTS(rawDocumentContentSchema) // Content legacy export const BooleanLegacy = toIOTSCodec(BooleanLegacyCodec) export const EmptyLegacy = toIOTSCodec(EmptyLegacyCodec) export const TextLegacy = toIOTSCodec(TextLegacyCodec) export const ColorLegacy = toIOTSCodec(ColorLegacyCodec) export const DateLegacy = toIOTSCodec(DateLegacyCodec) export const TimestampLegacy = toIOTSCodec(TimestampLegacyCodec) export const NumberLegacy = toIOTSCodec(NumberLegacyCodec) export const RangeLegacy = toIOTSCodec(RangeLegacyCodec) export const SelectLegacy = toIOTSCodec(SelectLegacyCodec) export const GeoPointLegacy = toIOTSCodec(GeoPointLegacyCodec) export const EmbedLegacy = toIOTSCodec(EmbedLegacyCodec) export const ImageLegacy = toIOTSCodec(ImageLegacyCodec) export const LinkLegacy = toIOTSCodec(LinkLegacyCodec) export const RepeatableLegacy = toIOTSCodec(RepeatableLegacyCodec) export const RichTextLegacy = toIOTSCodec(RichTextLegacyCodec) export const SeparatorLegacy = toIOTSCodec(SeparatorLegacyCodec) export const IntegrationFieldLegacy = toIOTSCodec(IntegrationFieldLegacyCodec) export const TableLegacy = toIOTSCodec(TableLegacyCodec) export const NestableLegacy = toIOTSCodec(NestableLegacyCodec) export const UIDLegacy = toIOTSCodec(UIDLegacyCodec) export const GroupItemLegacy = toIOTSCodec(GroupItemLegacyCodec) export const GroupLegacy = toIOTSCodec(GroupLegacyCodec) export const LegacySliceLegacy = toIOTSCodec(LegacySliceLegacyCodec) export const CompositeSliceLegacy = toIOTSCodec(CompositeSliceLegacyCodec) export const SharedSliceLegacy = toIOTSCodec(SharedSliceLegacyCodec) export const SliceLegacy = toIOTSCodec(SliceLegacyCodec) export const SliceItemLegacy = toIOTSCodec(SliceItemLegacyCodec) export const SlicesLegacy = toIOTSCodec(SlicesLegacyCodec) export const WidgetLegacy = toIOTSCodec(WidgetLegacyCodec) export const DocumentLegacy = toIOTSCodec(DocumentLegacyCodec) // Model export const BooleanModelSchema = toIOTS(rawBooleanModelSchema) export const ColorModelSchema = toIOTS(rawColorModelSchema) export const DateModelSchema = toIOTS(rawDateModelSchema) export const EmbedModelSchema = toIOTS(rawEmbedModelSchema) export const GeoPointModelSchema = toIOTS(rawGeoPointModelSchema) export const ImageModelSchema = toIOTS(rawImageModelSchema) export const IntegrationFieldModelSchema = toIOTS(rawIntegrationFieldModelSchema) export const LinkModelSchema = toIOTS(rawLinkModelSchema) export const NestableModelSchema = toIOTS(rawNestableModelSchema) export const NumberModelSchema = toIOTS(rawNumberModelSchema) export const RangeModelSchema = toIOTS(rawRangeModelSchema) export const RichTextModelSchema = toIOTS(rawRichTextModelSchema) export const SelectModelSchema = toIOTS(rawSelectModelSchema) export const SeparatorModelSchema = toIOTS(rawSeparatorModelSchema) export const SharedSliceModelSchema = toIOTS(rawSharedSliceModelSchema) export const TableModelSchema = toIOTS(rawTableModelSchema) export const TextModelSchema = toIOTS(rawTextModelSchema) export const TimestampModelSchema = toIOTS(rawTimestampModelSchema) export const UIDModelSchema = toIOTS(rawUIDModelSchema) export const GroupModelSchema = toIOTS(rawGroupModelSchema) export const NestedGroupModelSchema = toIOTS(rawNestedGroupModelSchema) export const SharedSliceModelVariationSchema = toIOTS(rawSharedSliceModelVariationSchema) export const SharedSliceRefModelSchema = toIOTS(rawSharedSliceRefModelSchema) export const CompositeSliceModelSchema = toIOTS(rawCompositeSliceModelSchema) export const LegacySliceModelSchema = toIOTS(rawLegacySliceModelSchema) export const DynamicSliceModelSchema = toIOTS(rawDynamicSliceModelSchema) export const StaticSliceModelSchema = toIOTS(rawStaticSliceModelSchema) export const SliceContentModelSchema = toIOTS(rawSliceContentModelSchema) export const DynamicSlicesModelSchema = toIOTS(rawDynamicSlicesModelSchema) export const StaticSlicesModelSchema = toIOTS(rawStaticSlicesModelSchema) export const StaticWidgetModelSchema = toIOTS(rawStaticWidgetModelSchema) export const DynamicWidgetModelSchema = toIOTS(rawDynamicWidgetModelSchema) export const StaticCustomTypeModelTabSchema = toIOTS(rawStaticCustomTypeModelTabSchema) export const DynamicCustomTypeModelTabSchema = toIOTS(rawDynamicCustomTypeModelTabSchema) export const StaticCustomTypeModelSchema = toIOTS(rawStaticCustomTypeModelSchema) export const DynamicCustomTypeModelSchema = toIOTS(rawDynamicCustomTypeModelSchema) /** @deprecated Use DynamicCustomTypeModelSchema instead */ export const CustomTypeModelSchema = DynamicCustomTypeModelSchema function toIOTS(type: ZodMiniType): t.Type { return new t.Type( "Type", (input): input is T => type.safeParse(input).success, (input, context) => { const result = type.safeParse(input) return result.success ? t.success(result.data) : t.failure( input, context, result.error.issues.map((issue) => formatIssue(issue, input)).join("\n"), ) }, t.identity, ) } function toIOTSCodec( codecFactory: (...args: TArgs) => LegacyCodec, ): ( ...args: TArgs ) => t.Type["fromContent"]>, unknown> { return (...args: TArgs) => { const codec = codecFactory(...args) return new t.Type< A, ReturnType["fromContent"]>, unknown >( codec.name, codec.is, (input, context) => { const result = codec.toContent(input) return result.success ? t.success(result.data) : t.failure( input, context, result.error.issues.map((issue) => formatIssue(issue, input)).join("\n"), ) }, codec.fromContent, ) } } interface ZodIssue { code: string message: string path?: PropertyKey[] expected?: string values?: unknown[] keys?: string[] } function resolveValue(input: unknown, path: PropertyKey[]): unknown { let current = input for (const key of path) { if (current == null || typeof current !== "object") return undefined current = (current as Record)[key] } return current } function formatValue(value: unknown): string { if (value === null) return "null" if (value === undefined) return "undefined" if (typeof value === "string") return JSON.stringify(value) return String(value) } function formatIssue(issue: ZodIssue, input: unknown): string { if (issue.code === "custom" && issue.message !== "Invalid input") { return issue.message } const value = resolveValue(input, issue.path ?? []) const pathStr = issue.path?.length ? issue.path.join(".") : undefined if (issue.code === "invalid_value" && issue.values) { const expected = issue.values.map(formatValue).join(", ") return pathStr ? `Invalid value ${formatValue(value)} at .${pathStr}: expected ${expected}` : `Invalid value ${formatValue(value)}: expected ${expected}` } if (issue.code === "invalid_type" && issue.expected) { return pathStr ? `Invalid value ${formatValue(value)} at .${pathStr}: expected ${issue.expected}` : `Invalid value ${formatValue(value)}: expected ${issue.expected}` } if (issue.code === "unrecognized_keys" && issue.keys) { return `Unrecognized key(s): ${issue.keys.join(", ")}` } return issue.message }