@form-instant/react-resolver-zod 0.0.0-alpha → 0.0.9-alpha
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/element.d.ts +1 -1
- package/dist/funcs/field-config.d.ts +1 -1
- package/dist/funcs/types.d.ts +3 -3
- package/dist/index.d.ts +7 -11
- package/dist/provider.d.ts +3 -1
- package/dist/react-resolver-zod.cjs.development.js +16 -21
- package/dist/react-resolver-zod.cjs.development.js.map +1 -1
- package/dist/react-resolver-zod.cjs.production.min.js +1 -1
- package/dist/react-resolver-zod.cjs.production.min.js.map +1 -1
- package/dist/react-resolver-zod.esm.js +12 -21
- package/dist/react-resolver-zod.esm.js.map +1 -1
- package/dist/useSchema.d.ts +2 -2
- package/package.json +74 -75
- package/src/element.tsx +18 -20
- package/src/funcs/field-config.ts +10 -10
- package/src/funcs/types.ts +4 -3
- package/src/index.ts +18 -17
- package/src/provider.tsx +5 -2
- package/src/useSchema.tsx +1 -1
package/dist/element.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
export interface ElementProps<Schema extends Record<string, any>> {
|
2
2
|
name: keyof Schema;
|
3
3
|
}
|
4
|
-
export declare const Element: <S extends Record<string, any>>({ name
|
4
|
+
export declare const Element: <S extends Record<string, any>>({ name }: ElementProps<S>) => import("react/jsx-runtime").JSX.Element;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { FieldConfig, SuperRefineFunction } from "./types";
|
3
3
|
export declare const FIELD_CONFIG_SYMBOL: unique symbol;
|
4
|
-
export declare
|
4
|
+
export declare const createZodSchemaFieldConfig: <AdditionalRenderable = null, FieldTypes = string>() => (config: FieldConfig<AdditionalRenderable, FieldTypes>) => SuperRefineFunction;
|
5
5
|
export declare function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig;
|
package/dist/funcs/types.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
export type ZodObjectOrWrapped =
|
1
|
+
import { ZodEffects, ZodObject } from "zod";
|
2
|
+
export type ZodObjectOrWrapped = ZodObject<any, any> | ZodEffects<ZodObject<any, any>>;
|
3
3
|
export interface ParsedField<AdditionalRenderable, FieldTypes = string> {
|
4
4
|
key: string;
|
5
5
|
type: string;
|
@@ -14,6 +14,6 @@ export interface ParsedSchema<AdditionalRenderable = null, FieldTypes = string>
|
|
14
14
|
}
|
15
15
|
export type SuperRefineFunction = () => unknown;
|
16
16
|
export type Renderable<AdditionalRenderable = null> = string | number | boolean | null | undefined | AdditionalRenderable;
|
17
|
-
export type FieldConfig<AdditionalRenderable =
|
17
|
+
export type FieldConfig<AdditionalRenderable = object, FieldTypes = string> = {
|
18
18
|
fieldType?: FieldTypes;
|
19
19
|
} & AdditionalRenderable;
|
package/dist/index.d.ts
CHANGED
@@ -1,11 +1,7 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
export
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
schema: import("./type").zodResolverProps;
|
9
|
-
}>;
|
10
|
-
FormInstantElement: <S extends Record<string, any>>({ name, }: import("./element").ElementProps<S>) => import("react/jsx-runtime").JSX.Element;
|
11
|
-
};
|
1
|
+
import { FieldConfig } from "@form-instant/react-input-mapping";
|
2
|
+
import { SuperRefineFunction } from "./funcs/types";
|
3
|
+
export { Element as FormInstantElement } from "./element";
|
4
|
+
export { Provider as FormInstantProvider } from "./provider";
|
5
|
+
export * from "./type";
|
6
|
+
export { useFields, useSchema } from "./useSchema";
|
7
|
+
export declare const createZodSchemaFieldConfig: <AdditionalRenderable = null, FieldTypes = string>() => (config: FieldConfig<AdditionalRenderable, FieldTypes>) => SuperRefineFunction;
|
package/dist/provider.d.ts
CHANGED
@@ -37,22 +37,14 @@ function getDefaultValueInZodStack(schema) {
|
|
37
37
|
return undefined;
|
38
38
|
}
|
39
39
|
|
40
|
-
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol("GetFieldConfig");
|
41
|
-
function fieldConfig(config) {
|
42
|
-
var refinementFunction = function refinementFunction() {
|
43
|
-
// Do nothing.
|
44
|
-
};
|
45
|
-
// @ts-expect-error This is a symbol and not a real value.
|
46
|
-
refinementFunction[FIELD_CONFIG_SYMBOL] = config;
|
47
|
-
return refinementFunction;
|
48
|
-
}
|
40
|
+
var FIELD_CONFIG_SYMBOL$1 = /*#__PURE__*/Symbol("GetFieldConfig");
|
49
41
|
function getFieldConfigInZodStack(schema) {
|
50
42
|
var typedSchema = schema;
|
51
43
|
if (typedSchema._def.typeName === "ZodEffects") {
|
52
44
|
var effect = typedSchema._def.effect;
|
53
45
|
var refinementFunction = effect.refinement;
|
54
|
-
if (FIELD_CONFIG_SYMBOL in refinementFunction) {
|
55
|
-
return refinementFunction[FIELD_CONFIG_SYMBOL];
|
46
|
+
if (FIELD_CONFIG_SYMBOL$1 in refinementFunction) {
|
47
|
+
return refinementFunction[FIELD_CONFIG_SYMBOL$1];
|
56
48
|
}
|
57
49
|
}
|
58
50
|
if ("innerType" in typedSchema._def) {
|
@@ -178,17 +170,20 @@ var Provider = function Provider(_ref) {
|
|
178
170
|
});
|
179
171
|
};
|
180
172
|
|
181
|
-
var
|
182
|
-
return {
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
FormInstantElement: Element
|
173
|
+
var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
174
|
+
return function (config) {
|
175
|
+
var refinementFunction = function refinementFunction() {
|
176
|
+
// Do nothing.
|
177
|
+
};
|
178
|
+
// @ts-expect-error This is a symbol and not a real value.
|
179
|
+
refinementFunction[FIELD_CONFIG_SYMBOL] = config;
|
180
|
+
return refinementFunction;
|
190
181
|
};
|
191
182
|
};
|
192
183
|
|
193
|
-
exports.
|
184
|
+
exports.FormInstantElement = Element;
|
185
|
+
exports.FormInstantProvider = Provider;
|
186
|
+
exports.createZodSchemaFieldConfig = createZodSchemaFieldConfig;
|
187
|
+
exports.useFields = useFields;
|
188
|
+
exports.useSchema = useSchema;
|
194
189
|
//# sourceMappingURL=react-resolver-zod.cjs.development.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react-resolver-zod.cjs.development.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx","../src/index.ts"],"sourcesContent":["import { createContext } from \"use-context-selector\";\nimport { zodResolverProps } from \"./type\";\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from \"zod\";\nimport { ZodObjectOrWrapped } from \"./types\";\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from \"zod\";\nimport { FieldConfig, SuperRefineFunction } from \"./types\";\nexport const FIELD_CONFIG_SYMBOL = Symbol(\"GetFieldConfig\");\n\nexport function fieldConfig<AdditionalRenderable = null, FieldTypes = string>(\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n): SuperRefineFunction {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n}\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === \"ZodEffects\") {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if (\"innerType\" in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if (\"schema\" in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from \"zod\";\nimport { FieldConfig } from \"./types\";\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[\"fieldType\"]): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n [\"ZodObject\", \"object\"],\n [\"ZodString\", \"string\"],\n [\"ZodNumber\", \"number\"],\n [\"ZodBoolean\", \"boolean\"],\n [\"ZodDate\", \"date\"],\n [\"ZodEnum\", \"select\"],\n [\"ZodNativeEnum\", \"select\"],\n [\"ZodArray\", \"array\"],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || \"string\"; // Default to string for unknown types\n}\n","import { z } from \"zod\";\nimport { getDefaultValueInZodStack } from \"./default-values\";\nimport { getFieldConfigInZodStack } from \"./field-config\";\nimport { inferFieldType } from \"./field-type-inference\";\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from \"./types\";\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField(\"0\", baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if (\"innerType\" in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if (\"schema\" in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from \"use-context-selector\";\nimport { ZodEffects, ZodObject } from \"zod\";\nimport { ZodResolverContext } from \"./context\";\nimport { parseSchema } from \"./funcs/schema-parser\";\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <S extends Record<string, any>>(key: keyof S) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from '@form-instant/react-input-mapping';\nimport { useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const Element = <S extends Record<string, any>>({\n name,\n}: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type)!;\n\n const { key, ...prop } = props;\n\n return <Element {...prop} key={`${key}-${id}`} />;\n })}\n </>\n );\n};\n","\"use client\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\n\nexport const Provider: FCC<{ schema: zodResolverProps }> = ({ children, schema }) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n","export * from './type';\nimport { Element } from './element';\nimport { fieldConfig } from './funcs/field-config';\nimport { FieldConfig } from './funcs/types';\nimport { Provider } from './provider';\nimport { useFields, useSchema } from './useSchema';\n\nexport const ZodSchemaResolver = <\n AdditionalRenderable = null,\n FieldTypes = string,\n>() => ({\n useSchema,\n useFields,\n fieldConfig: (config: FieldConfig<AdditionalRenderable, FieldTypes>) =>\n fieldConfig<AdditionalRenderable, FieldTypes>(config),\n FormInstantProvider: Provider,\n FormInstantElement: Element,\n});\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","fieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","Element","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","prop","_createElement","_extends","Provider","children","ZodSchemaResolver","FormInstantProvider","FormInstantElement"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC,gCAAa,CAAmB,EAAsB,CAAC;;ACAnF,SAAUC,yBAAyBA,CAACC,M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A,GAAG,GAAIwC,GAAAA,GAAAA,EAAAA;AAAI,OAAA,EAAI,CAAA;KAClD,CAAA;AACA,GAAA,CAAA,CAAA;AAEP,CAAC;;ACzBM,IAAMO,QAAQ,GAAsC,SAA9CA,QAAQA,CAAA7B,IAAA,EAA+D;AAAA,EAAA,IAAtB8B,QAAQ,GAAA9B,IAAA,CAAR8B,QAAQ;IAAE1E,MAAM,GAAA4C,IAAA,CAAN5C,MAAM,CAAA;AAC1E,EAAA,OAAOoE,cAAA,CAACvE,kBAAkB,CAAC4E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAExC,MAAM;AAAA0E,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F,CAAC;;ACCYC,IAAAA,iBAAiB,GAAG,SAApBA,iBAAiBA,GAAA;EAAA,OAGtB;AACNlB,IAAAA,SAAS,EAATA,SAAS;AACTE,IAAAA,SAAS,EAATA,SAAS;AACTjD,IAAAA,WAAW,EAAE,SAAbA,aAAWA,CAAGC,MAAqD,EAAA;MAAA,OACjED,WAAW,CAAmCC,MAAM,CAAC,CAAA;AAAA,KAAA;AACvDiE,IAAAA,mBAAmB,EAAEH,QAAQ;AAC7BI,IAAAA,kBAAkB,EAAEhB,OAAAA;GACrB,CAAA;AAAA;;;;"}
|
1
|
+
{"version":3,"file":"react-resolver-zod.cjs.development.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx","../src/index.ts"],"sourcesContent":["import { createContext } from \"use-context-selector\";\nimport { zodResolverProps } from \"./type\";\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from \"zod\";\nimport { ZodObjectOrWrapped } from \"./types\";\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from \"zod\";\nimport { FieldConfig, SuperRefineFunction } from \"./types\";\nexport const FIELD_CONFIG_SYMBOL = Symbol(\"GetFieldConfig\");\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === \"ZodEffects\") {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if (\"innerType\" in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if (\"schema\" in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from \"zod\";\nimport { FieldConfig } from \"./types\";\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[\"fieldType\"]): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n [\"ZodObject\", \"object\"],\n [\"ZodString\", \"string\"],\n [\"ZodNumber\", \"number\"],\n [\"ZodBoolean\", \"boolean\"],\n [\"ZodDate\", \"date\"],\n [\"ZodEnum\", \"select\"],\n [\"ZodNativeEnum\", \"select\"],\n [\"ZodArray\", \"array\"],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || \"string\"; // Default to string for unknown types\n}\n","import { z } from \"zod\";\nimport { getDefaultValueInZodStack } from \"./default-values\";\nimport { getFieldConfigInZodStack } from \"./field-config\";\nimport { inferFieldType } from \"./field-type-inference\";\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from \"./types\";\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField(\"0\", baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if (\"innerType\" in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if (\"schema\" in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from \"use-context-selector\";\nimport { ZodEffects, ZodObject } from \"zod\";\nimport { ZodResolverContext } from \"./context\";\nimport { parseSchema } from \"./funcs/schema-parser\";\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from \"@form-instant/react-input-mapping\";\nimport { useId } from \"react\";\nimport { useFields } from \"./useSchema\";\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const Element = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type)!;\n\n const { key, ...prop } = props;\n\n return <Element {...prop} key={`${key}-${id}`} />;\n })}\n </>\n );\n};\n","\"use client\";\nimport { FC, ReactNode } from \"react\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\nexport const Provider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n","import { FieldConfig } from \"@form-instant/react-input-mapping\";\nimport { SuperRefineFunction } from \"./funcs/types\";\n\nexport { Element as FormInstantElement } from \"./element\";\nexport { Provider as FormInstantProvider } from \"./provider\";\nexport * from \"./type\";\nexport { useFields, useSchema } from \"./useSchema\";\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","typedSchema","typeName","effect","refinementFunction","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","Element","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","prop","_createElement","_extends","Provider","children","createZodSchemaFieldConfig","config"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC,gCAAa,CAAmB,EAAsB,CAAC;;ACAnF,SAAUC,yBAAyBA,CAACC,MAAoB,EAAA;AAC1D,EAAA,IAAIA,MAAM,YAAYC,KAAC,CAACC,UAAU,EAAE;AAChC,IAAA,OAAOF,MAAM,CAACG,IAAI,CAACC,YAAY,EAAE,CAAA;AACpC,GAAA;AAED,EAAA,IAAIJ,MAAM,YAAYC,KAAC,CAACI,UAAU,EAAE;AAChC,IAAA,OAAON,yBAAyB,CAACC,MAAM,CAACM,SAAS,EAAE,CAAC,CAAA;AACvD,GAAA;AAED,EAAA,OAAOC,SAAS,CAAA;AACpB;;ACXO,IAAMC,qBAAmB,gBAAGC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,IAAAA,kBAAkB,CAACN,mBAAmB,CAAC,GAAGmE,MAAM,CAAA;AAEhD,IAAA,OAAO7D,kBAAkB,CAAA;GAC5B,CAAA;AAAA;;;;;;;;"}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
"use strict";var e=require("react/jsx-runtime"),n=require("react"),r=require("@form-instant/react-input-mapping"),t=require("use-context-selector"),i=require("zod");function a(){return a=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)({}).hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e},a.apply(null,arguments)}function o(e,n){if(null==e)return{};var r={};for(var t in e)if({}.hasOwnProperty.call(e,t)){if(n.includes(t))continue;r[t]=e[t]}return r}var u=t.createContext({});function f(e){return e instanceof i.z.ZodDefault?e._def.defaultValue():e instanceof i.z.ZodEffects?f(e.innerType()):void 0}var c=Symbol("GetFieldConfig");function s(e){var n=e;if("ZodEffects"===n._def.typeName){var r=n._def.effect.refinement;if(c in r)return r[c]}return"innerType"in n._def?s(n._def.innerType):"schema"in n._def?s(n._def.schema):{}}var d=["fieldType"];function
|
1
|
+
"use strict";var e=require("react/jsx-runtime"),n=require("react"),r=require("@form-instant/react-input-mapping"),t=require("use-context-selector"),i=require("zod");function a(){return a=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)({}).hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e},a.apply(null,arguments)}function o(e,n){if(null==e)return{};var r={};for(var t in e)if({}.hasOwnProperty.call(e,t)){if(n.includes(t))continue;r[t]=e[t]}return r}var u=t.createContext({});function f(e){return e instanceof i.z.ZodDefault?e._def.defaultValue():e instanceof i.z.ZodEffects?f(e.innerType()):void 0}var c=Symbol("GetFieldConfig");function s(e){var n=e;if("ZodEffects"===n._def.typeName){var r=n._def.effect.refinement;if(c in r)return r[c]}return"innerType"in n._def?s(n._def.innerType):"schema"in n._def?s(n._def.schema):{}}var d=["fieldType"];function p(e,n){var r=l(n),t=s(n),a=t.fieldType,u=o(t,d),c=function(e,n){if(n)return n;var r=e._def.typeName;return new Map([["ZodObject","object"],["ZodString","string"],["ZodNumber","number"],["ZodBoolean","boolean"],["ZodDate","date"],["ZodEnum","select"],["ZodNativeEnum","select"],["ZodArray","array"]]).get(r)||"string"}(r,a),m=f(n),v=r._def.values,y=[];v&&(y=Array.isArray(v)?v.map((function(e){return[e,e]})):Object.entries(v));var h=[];return r instanceof i.z.ZodObject&&(h=Object.entries(r.shape).map((function(e){return p(e[0],e[1])}))),r instanceof i.z.ZodArray&&(h=[p("0",r._def.type)]),{key:e,type:c,required:!n.isOptional(),default:m,fieldConfig:u,options:y,schema:h,description:r.description}}function l(e){return"innerType"in e._def?l(e._def.innerType):"schema"in e._def?l(e._def.schema):e}var m=function(){return t.useContext(u)},v=function(e){var n,r,t=(r=(n=m())instanceof i.z.ZodEffects?n.innerType():n,{fields:Object.fromEntries(Object.entries(r.shape).map((function(e){var n=e[0];return[n,p(n,e[1])]})))});return t.fields[e]},y=["key"];exports.FormInstantElement=function(t){var i=t.name,u=r.useInputMapping(),f=v(i),c=Object.values(f.schema),s=n.useId();return e.jsx(e.Fragment,{children:c.map((function(e){var r=u.get(e.type),t=e.key,i=o(e,y);return n.createElement(r,a({},i,{key:t+"-"+s}))}))})},exports.FormInstantProvider=function(n){return e.jsx(u.Provider,{value:n.schema,children:n.children})},exports.createZodSchemaFieldConfig=function(){return function(e){var n=function(){};return n[FIELD_CONFIG_SYMBOL]=e,n}},exports.useFields=v,exports.useSchema=m;
|
2
2
|
//# sourceMappingURL=react-resolver-zod.cjs.production.min.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react-resolver-zod.cjs.production.min.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/schema-parser.ts","../src/funcs/field-type-inference.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx","../src/index.ts"],"sourcesContent":["import { createContext } from \"use-context-selector\";\nimport { zodResolverProps } from \"./type\";\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from \"zod\";\nimport { ZodObjectOrWrapped } from \"./types\";\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from \"zod\";\nimport { FieldConfig, SuperRefineFunction } from \"./types\";\nexport const FIELD_CONFIG_SYMBOL = Symbol(\"GetFieldConfig\");\n\nexport function fieldConfig<AdditionalRenderable = null, FieldTypes = string>(\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n): SuperRefineFunction {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n}\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === \"ZodEffects\") {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if (\"innerType\" in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if (\"schema\" in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from \"zod\";\nimport { getDefaultValueInZodStack } from \"./default-values\";\nimport { getFieldConfigInZodStack } from \"./field-config\";\nimport { inferFieldType } from \"./field-type-inference\";\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from \"./types\";\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField(\"0\", baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if (\"innerType\" in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if (\"schema\" in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { z } from \"zod\";\nimport { FieldConfig } from \"./types\";\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[\"fieldType\"]): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n [\"ZodObject\", \"object\"],\n [\"ZodString\", \"string\"],\n [\"ZodNumber\", \"number\"],\n [\"ZodBoolean\", \"boolean\"],\n [\"ZodDate\", \"date\"],\n [\"ZodEnum\", \"select\"],\n [\"ZodNativeEnum\", \"select\"],\n [\"ZodArray\", \"array\"],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || \"string\"; // Default to string for unknown types\n}\n","import { useContext } from \"use-context-selector\";\nimport { ZodEffects, ZodObject } from \"zod\";\nimport { ZodResolverContext } from \"./context\";\nimport { parseSchema } from \"./funcs/schema-parser\";\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <S extends Record<string, any>>(key: keyof S) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from '@form-instant/react-input-mapping';\nimport { useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const Element = <S extends Record<string, any>>({\n name,\n}: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type)!;\n\n const { key, ...prop } = props;\n\n return <Element {...prop} key={`${key}-${id}`} />;\n })}\n </>\n );\n};\n","\"use client\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\n\nexport const Provider: FCC<{ schema: zodResolverProps }> = ({ children, schema }) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n","export * from './type';\nimport { Element } from './element';\nimport { fieldConfig } from './funcs/field-config';\nimport { FieldConfig } from './funcs/types';\nimport { Provider } from './provider';\nimport { useFields, useSchema } from './useSchema';\n\nexport const ZodSchemaResolver = <\n AdditionalRenderable = null,\n FieldTypes = string,\n>() => ({\n useSchema,\n useFields,\n fieldConfig: (config: FieldConfig<AdditionalRenderable, FieldTypes>) =>\n fieldConfig<AdditionalRenderable, FieldTypes>(config),\n FormInstantProvider: Provider,\n FormInstantElement: Element,\n});\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","typedSchema","typeName","refinementFunction","effect","refinement","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldType","fieldConfig","_objectWithoutPropertiesLoose","_excluded","type","Map","get","inferFieldType","options","values","optionValues","Array","isArray","map","value","Object","entries","subSchema","ZodObject","shape","_ref","ZodArray","required","isOptional","default","description","useSchema","useContext","useFields","S","objectSchema","_parseSchema","fields","fromEntries","_ref2","Element","name","InputMapping","useInputMapping","field","inputs","id","useId","_jsx","props","prop","_createElement","createElement","_extends","Provider","jsx","children","config","FormInstantProvider","FormInstantElement"],"mappings":"sgBAGO,IAAMA,EAAqBC,EAAaA,cAAmB,ICA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,EAACA,EAACI,WACbN,EAA0BC,EAAOM,kBAD5C,CAKJ,CCXO,IAAMC,EAAsBC,OAAO,kBAepC,SAAUC,EAAyBT,GACrC,IAAMU,EAAcV,EAEpB,GAAkC,eAA9BU,EAAYP,KAAKQ,SAA2B,CAC5C,IACMC,EADSF,EAAYP,KAAKU,OACEC,WAElC,GAAIP,KAAuBK,EACvB,OAAOA,EAAmBL,EAElC,CAEA,MAAI,cAAeG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x","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/schema-parser.ts","../src/funcs/field-type-inference.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx","../src/index.ts"],"sourcesContent":["import { createContext } from \"use-context-selector\";\nimport { zodResolverProps } from \"./type\";\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from \"zod\";\nimport { ZodObjectOrWrapped } from \"./types\";\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from \"zod\";\nimport { FieldConfig, SuperRefineFunction } from \"./types\";\nexport const FIELD_CONFIG_SYMBOL = Symbol(\"GetFieldConfig\");\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === \"ZodEffects\") {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if (\"innerType\" in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if (\"schema\" in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from \"zod\";\nimport { getDefaultValueInZodStack } from \"./default-values\";\nimport { getFieldConfigInZodStack } from \"./field-config\";\nimport { inferFieldType } from \"./field-type-inference\";\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from \"./types\";\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField(\"0\", baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if (\"innerType\" in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if (\"schema\" in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { z } from \"zod\";\nimport { FieldConfig } from \"./types\";\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[\"fieldType\"]): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n [\"ZodObject\", \"object\"],\n [\"ZodString\", \"string\"],\n [\"ZodNumber\", \"number\"],\n [\"ZodBoolean\", \"boolean\"],\n [\"ZodDate\", \"date\"],\n [\"ZodEnum\", \"select\"],\n [\"ZodNativeEnum\", \"select\"],\n [\"ZodArray\", \"array\"],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || \"string\"; // Default to string for unknown types\n}\n","import { useContext } from \"use-context-selector\";\nimport { ZodEffects, ZodObject } from \"zod\";\nimport { ZodResolverContext } from \"./context\";\nimport { parseSchema } from \"./funcs/schema-parser\";\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from \"@form-instant/react-input-mapping\";\nimport { useId } from \"react\";\nimport { useFields } from \"./useSchema\";\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const Element = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type)!;\n\n const { key, ...prop } = props;\n\n return <Element {...prop} key={`${key}-${id}`} />;\n })}\n </>\n );\n};\n","\"use client\";\nimport { FC, ReactNode } from \"react\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\nexport const Provider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n","import { FieldConfig } from \"@form-instant/react-input-mapping\";\nimport { SuperRefineFunction } from \"./funcs/types\";\n\nexport { Element as FormInstantElement } from \"./element\";\nexport { Provider as FormInstantProvider } from \"./provider\";\nexport * from \"./type\";\nexport { useFields, useSchema } from \"./useSchema\";\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","typedSchema","typeName","refinementFunction","effect","refinement","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldType","fieldConfig","_objectWithoutPropertiesLoose","_excluded","type","Map","get","inferFieldType","options","values","optionValues","Array","isArray","map","value","Object","entries","subSchema","ZodObject","shape","_ref","ZodArray","required","isOptional","default","description","useSchema","useContext","useFields","S","objectSchema","_parseSchema","fields","fromEntries","_ref2","name","InputMapping","useInputMapping","field","inputs","id","useId","_jsx","props","Element","prop","_createElement","createElement","_extends","jsx","Provider","children","config"],"mappings":"sgBAGO,IAAMA,EAAqBC,EAAaA,cAAmB,ICA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,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SAACC,GACG,IAAM5D,EAA0C,aAOhD,OAFAA,EAAmBL,qBAAuBiE,EAEnC5D,EACV"}
|
@@ -35,22 +35,14 @@ function getDefaultValueInZodStack(schema) {
|
|
35
35
|
return undefined;
|
36
36
|
}
|
37
37
|
|
38
|
-
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol("GetFieldConfig");
|
39
|
-
function fieldConfig(config) {
|
40
|
-
var refinementFunction = function refinementFunction() {
|
41
|
-
// Do nothing.
|
42
|
-
};
|
43
|
-
// @ts-expect-error This is a symbol and not a real value.
|
44
|
-
refinementFunction[FIELD_CONFIG_SYMBOL] = config;
|
45
|
-
return refinementFunction;
|
46
|
-
}
|
38
|
+
var FIELD_CONFIG_SYMBOL$1 = /*#__PURE__*/Symbol("GetFieldConfig");
|
47
39
|
function getFieldConfigInZodStack(schema) {
|
48
40
|
var typedSchema = schema;
|
49
41
|
if (typedSchema._def.typeName === "ZodEffects") {
|
50
42
|
var effect = typedSchema._def.effect;
|
51
43
|
var refinementFunction = effect.refinement;
|
52
|
-
if (FIELD_CONFIG_SYMBOL in refinementFunction) {
|
53
|
-
return refinementFunction[FIELD_CONFIG_SYMBOL];
|
44
|
+
if (FIELD_CONFIG_SYMBOL$1 in refinementFunction) {
|
45
|
+
return refinementFunction[FIELD_CONFIG_SYMBOL$1];
|
54
46
|
}
|
55
47
|
}
|
56
48
|
if ("innerType" in typedSchema._def) {
|
@@ -176,17 +168,16 @@ var Provider = function Provider(_ref) {
|
|
176
168
|
});
|
177
169
|
};
|
178
170
|
|
179
|
-
var
|
180
|
-
return {
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
FormInstantElement: Element
|
171
|
+
var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
172
|
+
return function (config) {
|
173
|
+
var refinementFunction = function refinementFunction() {
|
174
|
+
// Do nothing.
|
175
|
+
};
|
176
|
+
// @ts-expect-error This is a symbol and not a real value.
|
177
|
+
refinementFunction[FIELD_CONFIG_SYMBOL] = config;
|
178
|
+
return refinementFunction;
|
188
179
|
};
|
189
180
|
};
|
190
181
|
|
191
|
-
export {
|
182
|
+
export { Element as FormInstantElement, Provider as FormInstantProvider, createZodSchemaFieldConfig, useFields, useSchema };
|
192
183
|
//# sourceMappingURL=react-resolver-zod.esm.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"react-resolver-zod.esm.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx","../src/index.ts"],"sourcesContent":["import { createContext } from \"use-context-selector\";\nimport { zodResolverProps } from \"./type\";\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from \"zod\";\nimport { ZodObjectOrWrapped } from \"./types\";\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from \"zod\";\nimport { FieldConfig, SuperRefineFunction } from \"./types\";\nexport const FIELD_CONFIG_SYMBOL = Symbol(\"GetFieldConfig\");\n\nexport function fieldConfig<AdditionalRenderable = null, FieldTypes = string>(\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n): SuperRefineFunction {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n}\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === \"ZodEffects\") {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if (\"innerType\" in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if (\"schema\" in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from \"zod\";\nimport { FieldConfig } from \"./types\";\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[\"fieldType\"]): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n [\"ZodObject\", \"object\"],\n [\"ZodString\", \"string\"],\n [\"ZodNumber\", \"number\"],\n [\"ZodBoolean\", \"boolean\"],\n [\"ZodDate\", \"date\"],\n [\"ZodEnum\", \"select\"],\n [\"ZodNativeEnum\", \"select\"],\n [\"ZodArray\", \"array\"],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || \"string\"; // Default to string for unknown types\n}\n","import { z } from \"zod\";\nimport { getDefaultValueInZodStack } from \"./default-values\";\nimport { getFieldConfigInZodStack } from \"./field-config\";\nimport { inferFieldType } from \"./field-type-inference\";\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from \"./types\";\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField(\"0\", baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if (\"innerType\" in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if (\"schema\" in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from \"use-context-selector\";\nimport { ZodEffects, ZodObject } from \"zod\";\nimport { ZodResolverContext } from \"./context\";\nimport { parseSchema } from \"./funcs/schema-parser\";\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <S extends Record<string, any>>(key: keyof S) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from '@form-instant/react-input-mapping';\nimport { useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const Element = <S extends Record<string, any>>({\n name,\n}: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type)!;\n\n const { key, ...prop } = props;\n\n return <Element {...prop} key={`${key}-${id}`} />;\n })}\n </>\n );\n};\n","\"use client\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\n\nexport const Provider: FCC<{ schema: zodResolverProps }> = ({ children, schema }) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n","export * from './type';\nimport { Element } from './element';\nimport { fieldConfig } from './funcs/field-config';\nimport { FieldConfig } from './funcs/types';\nimport { Provider } from './provider';\nimport { useFields, useSchema } from './useSchema';\n\nexport const ZodSchemaResolver = <\n AdditionalRenderable = null,\n FieldTypes = string,\n>() => ({\n useSchema,\n useFields,\n fieldConfig: (config: FieldConfig<AdditionalRenderable, FieldTypes>) =>\n fieldConfig<AdditionalRenderable, FieldTypes>(config),\n FormInstantProvider: Provider,\n FormInstantElement: Element,\n});\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","fieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","Element","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","prop","_createElement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AAK4C,QAAAA,IAAI,GAAAxC,6BAAA,CAAKuC,KAAK,EAAAtC,SAAA,CAAA,CAAA;AAE9B,MAAA,OAAOwC,aAAC,CAAAV,OAAO,EAAAW,QAAA,KAAKF,IAAI,EAAA;QAAE5C,GAAG,EAAKA,GAAG,GAAIwC,GAAAA,GAAAA,EAAAA;AAAI,OAAA,EAAI,CAAA;KAClD,CAAA;AACA,GAAA,CAAA,CAAA;AAEP,CAAC;;ACzBM,IAAMO,QAAQ,GAAsC,SAA9CA,QAAQA,CAAA7B,IAAA,EAA+D;AAAA,EAAA,IAAtB8B,QAAQ,GAAA9B,IAAA,CAAR8B,QAAQ;IAAE1E,MAAM,GAAA4C,IAAA,CAAN5C,MAAM,CAAA;AAC1E,EAAA,OAAOoE,GAAA,CAACvE,kBAAkB,CAAC4E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAExC,MAAM;AAAA0E,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F,CAAC;;ACCYC,IAAAA,iBAAiB,GAAG,SAApBA,iBAAiBA,GAAA;EAAA,OAGtB;AACNlB,IAAAA,SAAS,EAATA,SAAS;AACTE,IAAAA,SAAS,EAATA,SAAS;AACTjD,IAAAA,WAAW,EAAE,SAAbA,aAAWA,CAAGC,MAAqD,EAAA;MAAA,OACjED,WAAW,CAAmCC,MAAM,CAAC,CAAA;AAAA,KAAA;AACvDiE,IAAAA,mBAAmB,EAAEH,QAAQ;AAC7BI,IAAAA,kBAAkB,EAAEhB,OAAAA;GACrB,CAAA;AAAA;;;;"}
|
1
|
+
{"version":3,"file":"react-resolver-zod.esm.js","sources":["../src/context.tsx","../src/funcs/default-values.ts","../src/funcs/field-config.ts","../src/funcs/field-type-inference.ts","../src/funcs/schema-parser.ts","../src/useSchema.tsx","../src/element.tsx","../src/provider.tsx","../src/index.ts"],"sourcesContent":["import { createContext } from \"use-context-selector\";\nimport { zodResolverProps } from \"./type\";\n\nexport const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);\n","import { z } from \"zod\";\nimport { ZodObjectOrWrapped } from \"./types\";\n\nexport function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {\n if (schema instanceof z.ZodDefault) {\n return schema._def.defaultValue();\n }\n\n if (schema instanceof z.ZodEffects) {\n return getDefaultValueInZodStack(schema.innerType());\n }\n\n return undefined;\n}\n\nexport function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {\n const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;\n const shape = objectSchema.shape;\n\n const defaultValues: Record<string, any> = {};\n\n for (const [key, field] of Object.entries(shape)) {\n const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);\n if (defaultValue !== undefined) {\n defaultValues[key] = defaultValue;\n }\n }\n\n return defaultValues;\n}\n","import { RefinementEffect, z } from \"zod\";\nimport { FieldConfig, SuperRefineFunction } from \"./types\";\nexport const FIELD_CONFIG_SYMBOL = Symbol(\"GetFieldConfig\");\n\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n\nexport function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {\n const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;\n\n if (typedSchema._def.typeName === \"ZodEffects\") {\n const effect = typedSchema._def.effect as RefinementEffect<any>;\n const refinementFunction = effect.refinement;\n\n if (FIELD_CONFIG_SYMBOL in refinementFunction) {\n return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;\n }\n }\n\n if (\"innerType\" in typedSchema._def) {\n return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);\n }\n if (\"schema\" in typedSchema._def) {\n return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);\n }\n\n return {};\n}\n","import { z } from \"zod\";\nimport { FieldConfig } from \"./types\";\n\nexport function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[\"fieldType\"]): string {\n if (fieldType) {\n return fieldType;\n }\n\n const type = schema._def.typeName;\n\n const zodToInputType = new Map([\n [\"ZodObject\", \"object\"],\n [\"ZodString\", \"string\"],\n [\"ZodNumber\", \"number\"],\n [\"ZodBoolean\", \"boolean\"],\n [\"ZodDate\", \"date\"],\n [\"ZodEnum\", \"select\"],\n [\"ZodNativeEnum\", \"select\"],\n [\"ZodArray\", \"array\"],\n ]);\n\n const match = zodToInputType.get(type);\n\n return match || \"string\"; // Default to string for unknown types\n}\n","import { z } from \"zod\";\nimport { getDefaultValueInZodStack } from \"./default-values\";\nimport { getFieldConfigInZodStack } from \"./field-config\";\nimport { inferFieldType } from \"./field-type-inference\";\nimport { ParsedField, ParsedSchema, ZodObjectOrWrapped } from \"./types\";\n\nfunction parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);\n const type = inferFieldType(baseSchema, fieldType);\n const defaultValue = getDefaultValueInZodStack(schema);\n\n // Enums\n const options = baseSchema._def.values;\n let optionValues: [string, string][] = [];\n if (options) {\n if (!Array.isArray(options)) {\n optionValues = Object.entries(options);\n } else {\n optionValues = options.map((value) => [value, value]);\n }\n }\n\n // Arrays and objects\n let subSchema: ParsedField<any>[] = [];\n if (baseSchema instanceof z.ZodObject) {\n subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny),\n );\n }\n if (baseSchema instanceof z.ZodArray) {\n subSchema = [parseField(\"0\", baseSchema._def.type)];\n }\n\n const resp = {\n key,\n type,\n required: !schema.isOptional(),\n default: defaultValue,\n fieldConfig,\n options: optionValues,\n schema: subSchema,\n description: baseSchema.description,\n };\n\n return resp;\n}\n\nfunction getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(\n schema: ChildType | z.ZodEffects<ChildType>,\n): ChildType {\n if (\"innerType\" in schema._def) {\n return getBaseSchema(schema._def.innerType as ChildType);\n }\n if (\"schema\" in schema._def) {\n return getBaseSchema(schema._def.schema as ChildType);\n }\n\n return schema as ChildType;\n}\n\nexport const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {\n const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;\n const shape = objectSchema.shape;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),\n );\n\n return { fields };\n};\n","import { useContext } from \"use-context-selector\";\nimport { ZodEffects, ZodObject } from \"zod\";\nimport { ZodResolverContext } from \"./context\";\nimport { parseSchema } from \"./funcs/schema-parser\";\n\nexport const useSchema = () =>\n useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchema();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { useInputMapping } from \"@form-instant/react-input-mapping\";\nimport { useId } from \"react\";\nimport { useFields } from \"./useSchema\";\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const Element = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\n const InputMapping = useInputMapping();\n const field = useFields(name);\n\n const inputs = Object.values(field.schema!);\n\n const id = useId();\n\n return (\n <>\n {inputs.map((props) => {\n const Element = InputMapping.get(props.type)!;\n\n const { key, ...prop } = props;\n\n return <Element {...prop} key={`${key}-${id}`} />;\n })}\n </>\n );\n};\n","\"use client\";\nimport { FC, ReactNode } from \"react\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\nexport const Provider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n","import { FieldConfig } from \"@form-instant/react-input-mapping\";\nimport { SuperRefineFunction } from \"./funcs/types\";\n\nexport { Element as FormInstantElement } from \"./element\";\nexport { Provider as FormInstantProvider } from \"./provider\";\nexport * from \"./type\";\nexport { useFields, useSchema } from \"./useSchema\";\nexport const createZodSchemaFieldConfig =\n <AdditionalRenderable = null, FieldTypes = string>() =>\n (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {\n const refinementFunction: SuperRefineFunction = () => {\n // Do nothing.\n };\n\n // @ts-expect-error This is a symbol and not a real value.\n refinementFunction[FIELD_CONFIG_SYMBOL] = config;\n\n return refinementFunction;\n };\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","typedSchema","typeName","effect","refinementFunction","refinement","inferFieldType","fieldType","type","zodToInputType","Map","match","get","parseField","key","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","ZodObject","shape","_ref","field","ZodArray","resp","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchema","useContext","useFields","_parseSchema","Element","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","prop","_createElement","_extends","Provider","children","createZodSchemaFieldConfig","config"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAMA,kBAAkB,gBAAGC,aAAa,CAAmB,EAAsB,CAAC;;ACAnF,SAAUC,yBAAyBA,CAACC,MAAoB,EAAA;AAC1D,EAAA,IAAIA,MAAM,YAAYC,CAAC,CAACC,UAAU,EAAE;AAChC,IAAA,OAAOF,MAAM,CAACG,IAAI,CAACC,YAAY,EAAE,CAAA;AACpC,GAAA;AAED,EAAA,IAAIJ,MAAM,YAAYC,CAAC,CA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0BAA0B,GACnC,SADSA,0BAA0BA,GAAA;EAAA,OAEnC,UAACC,MAAqD,EAAyB;AAC3E,IAAA,IAAM7D,kBAAkB,GAAwB,SAA1CA,kBAAkBA,GAA6B;AACjD;KACH,CAAA;AAED;AACAA,IAAAA,kBAAkB,CAACN,mBAAmB,CAAC,GAAGmE,MAAM,CAAA;AAEhD,IAAA,OAAO7D,kBAAkB,CAAA;GAC5B,CAAA;AAAA;;;;"}
|
package/dist/useSchema.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
import { ZodEffects, ZodObject } from "zod";
|
2
|
-
export declare const useSchema: () => ZodObject<never, never> | ZodEffects<never, never>;
|
3
|
-
export declare const useFields: <
|
2
|
+
export declare const useSchema: () => ZodObject<never, never, import("zod").ZodTypeAny, never, never> | ZodEffects<never, never, never>;
|
3
|
+
export declare const useFields: <Sc extends Record<string, any>>(key: keyof Sc) => import("./funcs/types").ParsedField<null, string>;
|
package/package.json
CHANGED
@@ -1,79 +1,78 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
"
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
2
|
+
"name": "@form-instant/react-resolver-zod",
|
3
|
+
"version": "0.0.9-alpha",
|
4
|
+
"license": "MIT",
|
5
|
+
"author": {
|
6
|
+
"name": "leomerida15",
|
7
|
+
"email": "dimasmerida15@gmail.com",
|
8
|
+
"url": "https://github.com/leomerida15"
|
9
|
+
},
|
10
|
+
"main": "dist/index.js",
|
11
|
+
"module": "dist/react-input-mapping.esm.js",
|
12
|
+
"typings": "dist/index.d.ts",
|
13
|
+
"files": [
|
14
|
+
"dist",
|
15
|
+
"src"
|
16
|
+
],
|
17
|
+
"private": false,
|
18
|
+
"exports": {
|
19
|
+
".": "./src/index.ts"
|
20
|
+
},
|
21
|
+
"scripts": {
|
22
|
+
"analyze": "size-limit --why",
|
23
|
+
"build": "dts build",
|
24
|
+
"lint": "dts lint",
|
25
|
+
"prepare": "dts build",
|
26
|
+
"size": "size-limit",
|
27
|
+
"start": "dts watch",
|
28
|
+
"test": "dts test --passWithNoTests",
|
29
|
+
"Up": "npm publish --access public"
|
30
|
+
},
|
31
|
+
"husky": {
|
32
|
+
"hooks": {
|
33
|
+
"pre-commit": "dts lint"
|
34
|
+
}
|
35
|
+
},
|
36
|
+
"jest": {
|
37
|
+
"testEnvironment": "jsdom"
|
38
|
+
},
|
39
|
+
"peerDependencies": {
|
40
|
+
"react": "^18.3.1"
|
41
|
+
},
|
42
|
+
"engines": {
|
43
|
+
"node": ">=12"
|
44
|
+
},
|
45
|
+
"size-limit": [
|
46
|
+
{
|
47
|
+
"path": "dist/react-input-mapping.cjs.production.min.js",
|
48
|
+
"limit": "10 KB"
|
49
|
+
},
|
50
|
+
{
|
51
|
+
"path": "dist/react-input-mapping.esm.js",
|
52
|
+
"limit": "10 KB"
|
53
|
+
}
|
54
|
+
],
|
55
|
+
"dependencies": {
|
56
|
+
"@form-instant/react-input-mapping": "^0.0.0-alpha",
|
57
|
+
"use-context-selector": "^2.0.0"
|
55
58
|
},
|
56
|
-
{
|
57
|
-
|
58
|
-
|
59
|
+
"devDependencies": {
|
60
|
+
"@size-limit/preset-small-lib": "^11.1.6",
|
61
|
+
"@tsconfig/recommended": "^1.0.7",
|
62
|
+
"@tsconfig/vite-react": "^3.0.2",
|
63
|
+
"@types/bun": "^1.1.11",
|
64
|
+
"@types/react": "^18.3.11",
|
65
|
+
"@typescript-eslint/eslint-plugin": "^8.10.0",
|
66
|
+
"@typescript-eslint/parser": "^8.10.0",
|
67
|
+
"dts-cli": "^2.0.5",
|
68
|
+
"eslint": "^9.13.0",
|
69
|
+
"eslint-plugin-prettier": "^5.2.1",
|
70
|
+
"husky": "^9.1.6",
|
71
|
+
"nodemon": "^3.1.7",
|
72
|
+
"prettier": "^3.3.3",
|
73
|
+
"size-limit": "^11.1.6",
|
74
|
+
"tslib": "^2.8.0",
|
75
|
+
"typescript": "~5.0.4",
|
76
|
+
"zod": "^3.23.8"
|
59
77
|
}
|
60
|
-
],
|
61
|
-
"dependencies": {
|
62
|
-
"@form-instant/react-input-mapping": "^0.0.0-alpha",
|
63
|
-
"use-context-selector": "^2.0.0"
|
64
|
-
},
|
65
|
-
"devDependencies": {
|
66
|
-
"@size-limit/preset-small-lib": "^11.1.4",
|
67
|
-
"@tsconfig/recommended": "^1.0.7",
|
68
|
-
"@tsconfig/vite-react": "^3.0.2",
|
69
|
-
"@types/bun": "^1.1.8",
|
70
|
-
"dts-cli": "^2.0.5",
|
71
|
-
"husky": "^9.1.5",
|
72
|
-
"nodemon": "^3.1.4",
|
73
|
-
"prettier": "^3.3.3",
|
74
|
-
"size-limit": "^11.1.4",
|
75
|
-
"tslib": "^2.7.0",
|
76
|
-
"typescript": "^5.5.4",
|
77
|
-
"zod": "^3.23.8"
|
78
|
-
}
|
79
78
|
}
|
package/src/element.tsx
CHANGED
@@ -1,30 +1,28 @@
|
|
1
|
-
import { useInputMapping } from
|
2
|
-
import { useId } from
|
3
|
-
import { useFields } from
|
1
|
+
import { useInputMapping } from "@form-instant/react-input-mapping";
|
2
|
+
import { useId } from "react";
|
3
|
+
import { useFields } from "./useSchema";
|
4
4
|
|
5
5
|
export interface ElementProps<Schema extends Record<string, any>> {
|
6
|
-
|
6
|
+
name: keyof Schema;
|
7
7
|
}
|
8
8
|
|
9
|
-
export const Element = <S extends Record<string, any>>({
|
10
|
-
|
11
|
-
|
12
|
-
const InputMapping = useInputMapping();
|
13
|
-
const field = useFields(name);
|
9
|
+
export const Element = <S extends Record<string, any>>({ name }: ElementProps<S>) => {
|
10
|
+
const InputMapping = useInputMapping();
|
11
|
+
const field = useFields(name);
|
14
12
|
|
15
|
-
|
13
|
+
const inputs = Object.values(field.schema!);
|
16
14
|
|
17
|
-
|
15
|
+
const id = useId();
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
return (
|
18
|
+
<>
|
19
|
+
{inputs.map((props) => {
|
20
|
+
const Element = InputMapping.get(props.type)!;
|
23
21
|
|
24
|
-
|
22
|
+
const { key, ...prop } = props;
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
return <Element {...prop} key={`${key}-${id}`} />;
|
25
|
+
})}
|
26
|
+
</>
|
27
|
+
);
|
30
28
|
};
|
@@ -2,18 +2,18 @@ import { RefinementEffect, z } from "zod";
|
|
2
2
|
import { FieldConfig, SuperRefineFunction } from "./types";
|
3
3
|
export const FIELD_CONFIG_SYMBOL = Symbol("GetFieldConfig");
|
4
4
|
|
5
|
-
export
|
6
|
-
|
7
|
-
): SuperRefineFunction {
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
export const createZodSchemaFieldConfig =
|
6
|
+
<AdditionalRenderable = null, FieldTypes = string>() =>
|
7
|
+
(config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {
|
8
|
+
const refinementFunction: SuperRefineFunction = () => {
|
9
|
+
// Do nothing.
|
10
|
+
};
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
// @ts-expect-error This is a symbol and not a real value.
|
13
|
+
refinementFunction[FIELD_CONFIG_SYMBOL] = config;
|
14
14
|
|
15
|
-
|
16
|
-
}
|
15
|
+
return refinementFunction;
|
16
|
+
};
|
17
17
|
|
18
18
|
export function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {
|
19
19
|
const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;
|
package/src/funcs/types.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
import {
|
2
|
-
|
1
|
+
import { ZodEffects, ZodObject } from "zod";
|
2
|
+
|
3
|
+
export type ZodObjectOrWrapped = ZodObject<any, any> | ZodEffects<ZodObject<any, any>>;
|
3
4
|
|
4
5
|
export interface ParsedField<AdditionalRenderable, FieldTypes = string> {
|
5
6
|
key: string;
|
@@ -27,6 +28,6 @@ export type Renderable<AdditionalRenderable = null> =
|
|
27
28
|
| undefined
|
28
29
|
| AdditionalRenderable;
|
29
30
|
|
30
|
-
export type FieldConfig<AdditionalRenderable =
|
31
|
+
export type FieldConfig<AdditionalRenderable = object, FieldTypes = string> = {
|
31
32
|
fieldType?: FieldTypes;
|
32
33
|
} & AdditionalRenderable;
|
package/src/index.ts
CHANGED
@@ -1,18 +1,19 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
import { fieldConfig } from './funcs/field-config';
|
4
|
-
import { FieldConfig } from './funcs/types';
|
5
|
-
import { Provider } from './provider';
|
6
|
-
import { useFields, useSchema } from './useSchema';
|
1
|
+
import { FieldConfig } from "@form-instant/react-input-mapping";
|
2
|
+
import { SuperRefineFunction } from "./funcs/types";
|
7
3
|
|
8
|
-
export
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
4
|
+
export { Element as FormInstantElement } from "./element";
|
5
|
+
export { Provider as FormInstantProvider } from "./provider";
|
6
|
+
export * from "./type";
|
7
|
+
export { useFields, useSchema } from "./useSchema";
|
8
|
+
export const createZodSchemaFieldConfig =
|
9
|
+
<AdditionalRenderable = null, FieldTypes = string>() =>
|
10
|
+
(config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {
|
11
|
+
const refinementFunction: SuperRefineFunction = () => {
|
12
|
+
// Do nothing.
|
13
|
+
};
|
14
|
+
|
15
|
+
// @ts-expect-error This is a symbol and not a real value.
|
16
|
+
refinementFunction[FIELD_CONFIG_SYMBOL] = config;
|
17
|
+
|
18
|
+
return refinementFunction;
|
19
|
+
};
|
package/src/provider.tsx
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
"use client";
|
2
|
+
import { FC, ReactNode } from "react";
|
2
3
|
import { ZodResolverContext } from "./context";
|
3
4
|
import { zodResolverProps } from "./type";
|
4
|
-
|
5
|
-
|
5
|
+
export const Provider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({
|
6
|
+
children,
|
7
|
+
schema,
|
8
|
+
}) => {
|
6
9
|
return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;
|
7
10
|
};
|
package/src/useSchema.tsx
CHANGED
@@ -6,7 +6,7 @@ import { parseSchema } from "./funcs/schema-parser";
|
|
6
6
|
export const useSchema = () =>
|
7
7
|
useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;
|
8
8
|
|
9
|
-
export const useFields = <
|
9
|
+
export const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {
|
10
10
|
const S = useSchema();
|
11
11
|
|
12
12
|
const { fields } = parseSchema(S);
|