@form-instant/react-resolver-zod 0.1.5 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
- var reactInputMapping = require('@form-instant/react-input-mapping');
5
4
  var react = require('react');
5
+ var reactInputMapping = require('@form-instant/react-input-mapping');
6
6
  var useContextSelector = require('use-context-selector');
7
7
  var zod = require('zod');
8
8
 
@@ -76,12 +76,12 @@ function inferFieldType(schema, fieldType) {
76
76
  return match || 'string'; // Default to string for unknown types
77
77
  }
78
78
 
79
- var _excluded$1 = ["fieldType"];
79
+ var _excluded = ["fieldType"];
80
80
  function parseField(key, schema) {
81
81
  var baseSchema = getBaseSchema(schema);
82
82
  var _getFieldConfigInZodS = getFieldConfigInZodStack(schema),
83
83
  fieldType = _getFieldConfigInZodS.fieldType,
84
- fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded$1);
84
+ fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded);
85
85
  var type = inferFieldType(baseSchema, fieldType);
86
86
  var defaultValue = getDefaultValueInZodStack(schema);
87
87
  // Enums
@@ -152,21 +152,15 @@ var useFields = function useFields(key) {
152
152
  return fields[key];
153
153
  };
154
154
 
155
- var _excluded = ["key"];
156
155
  var FormInstantElement = function FormInstantElement(_ref) {
157
156
  var name = _ref.name;
158
- var InputMapping = reactInputMapping.useInputMapping();
159
157
  var field = useFields(name);
160
158
  var inputs = Object.values(field.schema);
161
159
  var id = react.useId();
162
160
  return jsxRuntime.jsx(jsxRuntime.Fragment, {
163
161
  children: inputs.map(function (props) {
164
- var Element = InputMapping.get(props.type);
165
- var key = props.key,
166
- prop = _objectWithoutPropertiesLoose(props, _excluded);
167
- if (!Element) return null;
168
- return react.createElement(Element, _extends({}, prop, {
169
- key: key + "-" + id
162
+ return react.createElement(reactInputMapping.ElementMapping, _extends({}, props, {
163
+ key: id + "-" + props.key
170
164
  }));
171
165
  })
172
166
  });
@@ -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"],"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 { createElement, useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <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 if (!Element) return null;\n\n return createElement(Element, { ...prop, key: `${key}-${id}` });\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","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","FormInstantElement","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","Element","prop","createElement","_extends","FormInstantProvider","children","Provider"],"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,mBAAmB,gBAAGC,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAE9CC,IAA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A,CAACxE,kBAAkB,CAACgF,QAAQ,EAAA;AAACpC,IAAAA,KAAK,EAAEzC,MAAM;AAAA4E,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;;;;;"}
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"],"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 { ElementMapping } 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 FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\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 return <ElementMapping {...props} key={`${id}-${props.key}`} />;\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","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","FormInstantElement","name","inputs","id","useId","_jsx","props","_createElement","ElementMapping","_extends","FormInstantProvider","children","Provider"],"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;AACrC,GAAA;AAEA,EAAA,IAAIJ,MAAM,YAAYC,KAAC,CAACI,UAAU,EAAE;AAChC,IAAA,OAAON,yBAAyB,CAACC,MAAM,CAACM,SAAS,EAAE,CAAC,CAAA;AACxD,GAAA;AAEA,EAAA,OAAOC,SAAS,CAAA;AACpB;;ACXO,IAAMC,mBAAmB,gBAAGC,MA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CAAA;AAErB,EAAA,IAAAG,YAAA,GAAmBT,WAAW,CAACC,CAAC,CAAC;IAAzBE,MAAM,GAAAM,YAAA,CAANN,MAAM,CAAA;EAEd,OAAOA,MAAM,CAAC7B,GAAa,CAAC,CAAA;AAChC;;ICNaoC,kBAAkB,GAAG,SAArBA,kBAAkBA,CAAAjB,IAAA,EAAgE;AAAA,EAAA,IAA3BkB,IAAI,GAAAlB,IAAA,CAAJkB,IAAI,CAAA;AACpE,EAAA,IAAMjB,KAAK,GAAGc,SAAS,CAACG,IAAI,CAAC,CAAA;EAE7B,IAAMC,MAAM,GAAG1B,MAAM,CAACJ,MAAM,CAACY,KAAK,CAAC9C,MAAO,CAAC,CAAA;AAE3C,EAAA,IAAMiE,EAAE,GAAGC,WAAK,EAAE,CAAA;EAElB,OACIC;cACKH,MAAM,CAACxB,GAAG,CAAC,UAAC4B,KAAK,EAAI;AAClB,MAAA,OAAOC,mBAAC,CAAAC,gCAAc,EAAAC,QAAA,KAAKH,KAAK,EAAA;AAAE1C,QAAAA,GAAG,EAAKuC,EAAE,GAAIG,GAAAA,GAAAA,KAAK,CAAC1C,GAAAA;AAAK,OAAA,EAAI,CAAA;KAClE,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;ICnBa8C,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA3B,IAAA,EAG3B;AAAA,EAAA,IAFD4B,QAAQ,GAAA5B,IAAA,CAAR4B,QAAQ;IACRzE,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOmE,cAAA,CAACtE,kBAAkB,CAAC6E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAEzC,MAAM;AAAAyE,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;;;;;"}
@@ -1,2 +1,2 @@
1
- "use strict";var e=require("react/jsx-runtime"),n=require("@form-instant/react-input-mapping"),r=require("react"),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 l(e,n){var r=p(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 l(e[0],e[1])}))),r instanceof i.z.ZodArray&&(h=[l("0",r._def.type)]),{key:e,type:c,required:!n.isOptional(),default:m,fieldConfig:u,options:y,schema:h,description:r.description}}function p(e){return"innerType"in e._def?p(e._def.innerType):"schema"in e._def?p(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,l(n,e[1])]})))});return t.fields[e]},y=["key"];exports.FormInstantElement=function(t){var i=t.name,u=n.useInputMapping(),f=v(i),c=Object.values(f.schema),s=r.useId();return e.jsx(e.Fragment,{children:c.map((function(e){var n=u.get(e.type),t=e.key,i=o(e,y);return n?r.createElement(n,a({},i,{key:t+"-"+s})):null}))})},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[c]=e,n}},exports.useFields=v,exports.useSchema=m;
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)}var o=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 u=Symbol("GetFieldConfig");function c(e){var n=e;if("ZodEffects"===n._def.typeName){var r=n._def.effect.refinement;if(u in r)return r[u]}return"innerType"in n._def?c(n._def.innerType):"schema"in n._def?c(n._def.schema):{}}var s=["fieldType"];function d(e,n){var r=l(n),t=c(n),a=t.fieldType,o=function(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}(t,s),u=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),p=f(n),m=r._def.values,v=[];m&&(v=Array.isArray(m)?m.map((function(e){return[e,e]})):Object.entries(m));var y=[];return r instanceof i.z.ZodObject&&(y=Object.entries(r.shape).map((function(e){return d(e[0],e[1])}))),r instanceof i.z.ZodArray&&(y=[d("0",r._def.type)]),{key:e,type:u,required:!n.isOptional(),default:p,fieldConfig:o,options:v,schema:y,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 p=function(){return t.useContext(o)},m=function(e){var n,r,t=(r=(n=p())instanceof i.z.ZodEffects?n.innerType():n,{fields:Object.fromEntries(Object.entries(r.shape).map((function(e){var n=e[0];return[n,d(n,e[1])]})))});return t.fields[e]};exports.FormInstantElement=function(t){var i=m(t.name),o=Object.values(i.schema),f=n.useId();return e.jsx(e.Fragment,{children:o.map((function(e){return n.createElement(r.ElementMapping,a({},e,{key:f+"-"+e.key}))}))})},exports.FormInstantProvider=function(n){return e.jsx(o.Provider,{value:n.schema,children:n.children})},exports.createZodSchemaFieldConfig=function(){return function(e){var n=function(){};return n[u]=e,n}},exports.useFields=m,exports.useSchema=p;
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"],"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 { createElement, useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <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 if (!Element) return null;\n\n return createElement(Element, { ...prop, key: `${key}-${id}` });\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\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","_extends","jsx","Provider","children","config"],"mappings":"sgBAGO,IAAMA,EAAqBC,EAAaA,cAAmB,ICA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,EAACA,EAACI,WACbN,EAA0BC,EAAOM,kBA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A0C,aAOhD,OAFAA,EAAmBL,GAAuBgE,EAEnC3D,EACV"}
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"],"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 { ElementMapping } 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 FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\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 return <ElementMapping {...props} key={`${id}-${props.key}`} />;\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\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","field","name","inputs","id","useId","_jsx","props","_createElement","createElement","ElementMapping","_extends","jsx","Provider","children","config"],"mappings":"6XAGO,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,EAAYP,KACpBM,EAAyBC,EAAYP,KAAKG,WAEjD,WAAYI,EAAYP,KACjBM,EAA0BC,EAAYP,KAAaH,QAGvD,EACX,qBC/BA,SAASe,EAAWC,EAAahB,GAC7B,IAAMiB,EAAaC,EAAclB,GACjCmB,EAAsCV,EAAyBT,GAAvDoB,EAASD,EAATC,UAAcC,yIA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AO1D,CAAEG,OAJMjB,OAAOkB,YAClBlB,OAAOC,QAHGc,EAAaX,OAGDN,KAAI,SAAAqB,GAAA,IAAEtC,EAAGsC,EAAA,GAAO,MAAM,CAACtC,EAAKD,EAAWC,EAAvBsC,EAAA,IAAmD,OErD7F,OAFcH,EAANC,OAEMpC,EAClB,6BCNkC,SAAHwB,GAAgE,IACrFe,EAAQP,EADsDR,EAAJgB,MAG1DC,EAAStB,OAAON,OAAO0B,EAAMvD,QAE7B0D,EAAKC,EAAAA,QAEX,OACIC,EAAAA,yBACKH,EAAOxB,KAAI,SAAC4B,GACT,OAAOC,EAACC,cAAAC,EAAcA,eAAAC,KAAKJ,EAAK,CAAE7C,IAAQ0C,EAAMG,IAAAA,EAAM7C,WAItE,8BCnB0F,SAA1DwB,GAI5B,OAAOoB,EAAAM,IAACrE,EAAmBsE,SAAQ,CAACjC,MAF9BM,EAANxC,OAEiDoE,SAHzC5B,EAAR4B,UAIJ,qCLHI,WADmC,OAEnC,SAACC,GACG,IAAMzD,EAA0C,aAOhD,OAFAA,EAAmBL,GAAuB8D,EAEnCzD,EACV"}
@@ -1,6 +1,6 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import { useInputMapping } from '@form-instant/react-input-mapping';
3
2
  import { useId, createElement } from 'react';
3
+ import { ElementMapping } from '@form-instant/react-input-mapping';
4
4
  import { createContext, useContext } from 'use-context-selector';
5
5
  import { z } from 'zod';
6
6
 
@@ -74,12 +74,12 @@ function inferFieldType(schema, fieldType) {
74
74
  return match || 'string'; // Default to string for unknown types
75
75
  }
76
76
 
77
- var _excluded$1 = ["fieldType"];
77
+ var _excluded = ["fieldType"];
78
78
  function parseField(key, schema) {
79
79
  var baseSchema = getBaseSchema(schema);
80
80
  var _getFieldConfigInZodS = getFieldConfigInZodStack(schema),
81
81
  fieldType = _getFieldConfigInZodS.fieldType,
82
- fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded$1);
82
+ fieldConfig = _objectWithoutPropertiesLoose(_getFieldConfigInZodS, _excluded);
83
83
  var type = inferFieldType(baseSchema, fieldType);
84
84
  var defaultValue = getDefaultValueInZodStack(schema);
85
85
  // Enums
@@ -150,21 +150,15 @@ var useFields = function useFields(key) {
150
150
  return fields[key];
151
151
  };
152
152
 
153
- var _excluded = ["key"];
154
153
  var FormInstantElement = function FormInstantElement(_ref) {
155
154
  var name = _ref.name;
156
- var InputMapping = useInputMapping();
157
155
  var field = useFields(name);
158
156
  var inputs = Object.values(field.schema);
159
157
  var id = useId();
160
158
  return jsx(Fragment, {
161
159
  children: inputs.map(function (props) {
162
- var Element = InputMapping.get(props.type);
163
- var key = props.key,
164
- prop = _objectWithoutPropertiesLoose(props, _excluded);
165
- if (!Element) return null;
166
- return createElement(Element, _extends({}, prop, {
167
- key: key + "-" + id
160
+ return createElement(ElementMapping, _extends({}, props, {
161
+ key: id + "-" + props.key
168
162
  }));
169
163
  })
170
164
  });
@@ -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"],"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 { createElement, useId } from 'react';\nimport { useFields } from './useSchema';\n\nexport interface ElementProps<Schema extends Record<string, any>> {\n name: keyof Schema;\n}\n\nexport const FormInstantElement = <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 if (!Element) return null;\n\n return createElement(Element, { ...prop, key: `${key}-${id}` });\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","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","FormInstantElement","name","InputMapping","useInputMapping","inputs","id","useId","_jsx","props","Element","prop","createElement","_extends","FormInstantProvider","children","Provider"],"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,CAACI,UAAU,EAAE;AAChC,IAAA,OAAON,yBAAyB,CAACC,MAAM,CAACM,SAAS,EAAE,CAAC,CAAA;AACvD,GAAA;AAED,EAAA,OAAOC,SAAS,CAAA;AACpB;;ACXO,IAAMC,mBAAmB,gBAAGC,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAE9CC,IAAAA,0BAA0B,GACnC,SADSA,0BAA0BA,GAAA;EAAA,OAEnC,UAACC,MAAqD,EAAyB;AAC3E,IAAA,IAAMC,kBAAkB,GAAwB,SAA1CA,kBAAkBA,GAA6B;AACjD;KACH,CAAA;AAED;AACAA,IAAAA,kBAAkB,CAACJ,mBAAmB,CAAC,GAAGG,MAAM,CAAA;AAEhD,IAAA,OAAOC,kBAAkB,CAAA;GAC5B,CAAA;AAAA,EAAA;AAEC,SAAUC,wBAAwBA,CAACb,MAAoB,EAAA;EACzD,IAAMc,WAAW,GAAGd,MAA4D,CAAA;AAEhF,EAAA,IAAIc,WAAW,CAACX,IAAI,CAACY,QAAQ,KAAK,YAAY,EAAE;AAC5C,IAAA,IAAMC,MAAM,GAAGF,WAAW,CAACX,IAAI,CAACa,MAA+B,CAAA;AAC/D,IAAA,IAAMJ,kBAAkB,GAAGI,MAAM,CAACC,UAAU,CAAA;IAE5C,IAAIT,mBAAmB,IAAII,kBAAkB,EAAE;MAC3C,OAAOA,kBAAkB,CAACJ,mBAAmB,CAAgB,CAAA;AACh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GAAG,GAAc4C,KAAK,CAAtB5C,GAAG;AAAK8C,QAAAA,IAAI,GAAAzC,6BAAA,CAAKuC,KAAK,EAAAtC,SAAA,CAAA,CAAA;AAE9B,MAAA,IAAI,CAACuC,OAAO,EAAE,OAAO,IAAI,CAAA;AAEzB,MAAA,OAAOE,aAAa,CAACF,OAAO,EAAAG,QAAA,KAAOF,IAAI,EAAA;QAAE9C,GAAG,EAAKA,GAAG,GAAIyC,GAAAA,GAAAA,EAAAA;AAAI,OAAA,CAAE,CAAC,CAAA;KAClE,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;IC1BaQ,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA9B,IAAA,EAG3B;AAAA,EAAA,IAFD+B,QAAQ,GAAA/B,IAAA,CAAR+B,QAAQ;IACR5E,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOqE,GAAA,CAACxE,kBAAkB,CAACgF,QAAQ,EAAA;AAACpC,IAAAA,KAAK,EAAEzC,MAAM;AAAA4E,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;"}
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"],"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 { ElementMapping } 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 FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {\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 return <ElementMapping {...props} key={`${id}-${props.key}`} />;\n })}\n </>\n );\n};\n","import { FC, ReactNode } from 'react';\nimport { ZodResolverContext } from './context';\nimport { zodResolverProps } from './type';\nexport const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({\n children,\n schema,\n}) => {\n return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","createZodSchemaFieldConfig","config","refinementFunction","getFieldConfigInZodStack","typedSchema","typeName","effect","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","FormInstantElement","name","inputs","id","useId","_jsx","props","_createElement","ElementMapping","_extends","FormInstantProvider","children","Provider"],"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;AACrC,GAAA;AAEA,EAAA,IAAIJ,MAAM,YAAYC,CAAC,CAACI,UAAU,EAAE;AAChC,IAAA,OAAON,yBAAyB,CAACC,MAAM,CAACM,SAAS,EAAE,CAAC,CAAA;AACxD,GAAA;AAEA,EAAA,OAAOC,SAAS,CAAA;AACpB;;ACXO,IAAMC,mBAAmB,gBAAGC,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAE9CC,IAAAA,0BAA0B,GACnC,SADSA,0BAA0BA,GAAA;EAAA,OAEnC,UAACC,MAAqD,EAAyB;AAC3E,IAAA,IAAMC,kBAAkB,GAAwB,SAA1CA,kBAAkBA,GAA6B;AACjD;KACH,CAAA;AAED;AACAA,IAAAA,kBAAkB,CAACJ,mBAAmB,CAAC,GAAGG,MAAM,CAAA;AAEhD,IAAA,OAAOC,kBAAkB,CAAA;GAC5B,CAAA;AAAA,EAAA;AAEC,SAAUC,wBAAwBA,CAACb,MAAoB,EAAA;EACzD,IAAMc,WAAW,GAAGd,MAA4D,CAAA;AAEhF,EAAA,IAAIc,WAAW,CAACX,IAAI,CAACY,QAAQ,KAAK,YAAY,EAAE;AAC5C,IAAA,IAAMC,MAAM,GAAGF,WAAW,CAACX,IAAI,CAACa,MAA+B,CAAA;AAC/D,IAAA,IAAMJ,kBAAkB,GAAGI,MAAM,CAACC,UAAU,CAAA;IAE5C,IAAIT,mBAAmB,IAAII,kBAAkB,EAAE;MAC3C,OAAOA,kBAAkB,CAACJ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AA,KAAK,CAAC1C,GAAAA;AAAK,OAAA,EAAI,CAAA;KAClE,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;ICnBa8C,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA3B,IAAA,EAG3B;AAAA,EAAA,IAFD4B,QAAQ,GAAA5B,IAAA,CAAR4B,QAAQ;IACRzE,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOmE,GAAA,CAACtE,kBAAkB,CAAC6E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAEzC,MAAM;AAAAyE,IAAAA,QAAA,EAAGA,QAAAA;AAAQ,GAAA,CAA+B,CAAA;AAC/F;;;;"}
@@ -1,3 +1,3 @@
1
1
  import { ZodEffects, ZodObject } from 'zod';
2
- export declare const useSchema: () => ZodObject<never, never, import("zod").ZodTypeAny, never, never> | ZodEffects<never, never, never>;
2
+ export declare const useSchema: () => ZodObject<never, never> | ZodEffects<never, never>;
3
3
  export declare const useFields: <Sc extends Record<string, any>>(key: keyof Sc) => import("./funcs/types").ParsedField<null, string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@form-instant/react-resolver-zod",
3
- "version": "0.1.5",
3
+ "version": "0.1.11",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "leomerida15",
@@ -11,13 +11,8 @@
11
11
  "module": "dist/react-input-mapping.esm.js",
12
12
  "typings": "dist/index.d.ts",
13
13
  "files": [
14
- "dist",
15
- "src"
14
+ "dist"
16
15
  ],
17
- "private": false,
18
- "exports": {
19
- ".": "./src/index.ts"
20
- },
21
16
  "scripts": {
22
17
  "analyze": "size-limit --why",
23
18
  "build": "dts build",
@@ -53,7 +48,7 @@
53
48
  }
54
49
  ],
55
50
  "dependencies": {
56
- "@form-instant/react-input-mapping": "^0.0.0-alpha",
51
+ "@form-instant/react-input-mapping": "^0.1.9",
57
52
  "use-context-selector": "^2.0.0"
58
53
  },
59
54
  "devDependencies": {
@@ -78,7 +73,7 @@
78
73
  "homepage": "https://leomerida15.github.io/form-instant-react-mapping",
79
74
  "repository": {
80
75
  "type": "git",
81
- "url": "git+https://github.com/leomerida15/form-instant-react-mapping.git"
76
+ "url": "git+https://github.com/leomerida15/form-instant-react-resolver-zod.git"
82
77
  },
83
78
  "keywords": [
84
79
  "@form-instant",
package/src/context.tsx DELETED
@@ -1,4 +0,0 @@
1
- import { createContext } from 'use-context-selector';
2
- import { zodResolverProps } from './type';
3
-
4
- export const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);
package/src/element.tsx DELETED
@@ -1,30 +0,0 @@
1
- import { useInputMapping } from '@form-instant/react-input-mapping';
2
- import { createElement, useId } from 'react';
3
- import { useFields } from './useSchema';
4
-
5
- export interface ElementProps<Schema extends Record<string, any>> {
6
- name: keyof Schema;
7
- }
8
-
9
- export const FormInstantElement = <S extends Record<string, any>>({ name }: ElementProps<S>) => {
10
- const InputMapping = useInputMapping();
11
- const field = useFields(name);
12
-
13
- const inputs = Object.values(field.schema!);
14
-
15
- const id = useId();
16
-
17
- return (
18
- <>
19
- {inputs.map((props) => {
20
- const Element = InputMapping.get(props.type);
21
-
22
- const { key, ...prop } = props;
23
-
24
- if (!Element) return null;
25
-
26
- return createElement(Element, { ...prop, key: `${key}-${id}` });
27
- })}
28
- </>
29
- );
30
- };
@@ -1,30 +0,0 @@
1
- import { z } from 'zod';
2
- import { ZodObjectOrWrapped } from './types';
3
-
4
- export function getDefaultValueInZodStack(schema: z.ZodTypeAny): any {
5
- if (schema instanceof z.ZodDefault) {
6
- return schema._def.defaultValue();
7
- }
8
-
9
- if (schema instanceof z.ZodEffects) {
10
- return getDefaultValueInZodStack(schema.innerType());
11
- }
12
-
13
- return undefined;
14
- }
15
-
16
- export function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any> {
17
- const objectSchema = schema instanceof z.ZodEffects ? schema.innerType() : schema;
18
- const shape = objectSchema.shape;
19
-
20
- const defaultValues: Record<string, any> = {};
21
-
22
- for (const [key, field] of Object.entries(shape)) {
23
- const defaultValue = getDefaultValueInZodStack(field as z.ZodTypeAny);
24
- if (defaultValue !== undefined) {
25
- defaultValues[key] = defaultValue;
26
- }
27
- }
28
-
29
- return defaultValues;
30
- }
@@ -1,38 +0,0 @@
1
- import { RefinementEffect, z } from 'zod';
2
- import { FieldConfig, SuperRefineFunction } from './types';
3
- export const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');
4
-
5
- export const createZodSchemaFieldConfig =
6
- <AdditionalRenderable = null, FieldTypes = string>() =>
7
- (config: FieldConfig<AdditionalRenderable, FieldTypes>): SuperRefineFunction => {
8
- const refinementFunction: SuperRefineFunction = () => {
9
- // Do nothing.
10
- };
11
-
12
- // @ts-expect-error This is a symbol and not a real value.
13
- refinementFunction[FIELD_CONFIG_SYMBOL] = config;
14
-
15
- return refinementFunction;
16
- };
17
-
18
- export function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {
19
- const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;
20
-
21
- if (typedSchema._def.typeName === 'ZodEffects') {
22
- const effect = typedSchema._def.effect as RefinementEffect<any>;
23
- const refinementFunction = effect.refinement;
24
-
25
- if (FIELD_CONFIG_SYMBOL in refinementFunction) {
26
- return refinementFunction[FIELD_CONFIG_SYMBOL] as FieldConfig;
27
- }
28
- }
29
-
30
- if ('innerType' in typedSchema._def) {
31
- return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);
32
- }
33
- if ('schema' in typedSchema._def) {
34
- return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);
35
- }
36
-
37
- return {};
38
- }
@@ -1,25 +0,0 @@
1
- import { z } from 'zod';
2
- import { FieldConfig } from './types';
3
-
4
- export function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {
5
- if (fieldType) {
6
- return fieldType;
7
- }
8
-
9
- const type = schema._def.typeName;
10
-
11
- const zodToInputType = new Map([
12
- ['ZodObject', 'object'],
13
- ['ZodString', 'string'],
14
- ['ZodNumber', 'number'],
15
- ['ZodBoolean', 'boolean'],
16
- ['ZodDate', 'date'],
17
- ['ZodEnum', 'select'],
18
- ['ZodNativeEnum', 'select'],
19
- ['ZodArray', 'array'],
20
- ]);
21
-
22
- const match = zodToInputType.get(type);
23
-
24
- return match || 'string'; // Default to string for unknown types
25
- }
@@ -1,71 +0,0 @@
1
- import { z } from 'zod';
2
- import { getDefaultValueInZodStack } from './default-values';
3
- import { getFieldConfigInZodStack } from './field-config';
4
- import { inferFieldType } from './field-type-inference';
5
- import { ParsedField, ParsedSchema, ZodObjectOrWrapped } from './types';
6
-
7
- function parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {
8
- const baseSchema = getBaseSchema(schema);
9
- const { fieldType, ...fieldConfig } = getFieldConfigInZodStack(schema);
10
- const type = inferFieldType(baseSchema, fieldType);
11
- const defaultValue = getDefaultValueInZodStack(schema);
12
-
13
- // Enums
14
- const options = baseSchema._def.values;
15
- let optionValues: [string, string][] = [];
16
- if (options) {
17
- if (!Array.isArray(options)) {
18
- optionValues = Object.entries(options);
19
- } else {
20
- optionValues = options.map((value) => [value, value]);
21
- }
22
- }
23
-
24
- // Arrays and objects
25
- let subSchema: ParsedField<any>[] = [];
26
- if (baseSchema instanceof z.ZodObject) {
27
- subSchema = Object.entries(baseSchema.shape).map(([key, field]) =>
28
- parseField(key, field as z.ZodTypeAny),
29
- );
30
- }
31
- if (baseSchema instanceof z.ZodArray) {
32
- subSchema = [parseField('0', baseSchema._def.type)];
33
- }
34
-
35
- const resp = {
36
- key,
37
- type,
38
- required: !schema.isOptional(),
39
- default: defaultValue,
40
- fieldConfig,
41
- options: optionValues,
42
- schema: subSchema,
43
- description: baseSchema.description,
44
- };
45
-
46
- return resp;
47
- }
48
-
49
- function getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(
50
- schema: ChildType | z.ZodEffects<ChildType>,
51
- ): ChildType {
52
- if ('innerType' in schema._def) {
53
- return getBaseSchema(schema._def.innerType as ChildType);
54
- }
55
- if ('schema' in schema._def) {
56
- return getBaseSchema(schema._def.schema as ChildType);
57
- }
58
-
59
- return schema as ChildType;
60
- }
61
-
62
- export const parseSchema = (S: ZodObjectOrWrapped): ParsedSchema => {
63
- const objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;
64
- const shape = objectSchema.shape;
65
-
66
- const fields = Object.fromEntries(
67
- Object.entries(shape).map(([key, field]) => [key, parseField(key, field as z.ZodTypeAny)]),
68
- );
69
-
70
- return { fields };
71
- };
@@ -1,33 +0,0 @@
1
- import { ZodEffects, ZodObject } from 'zod';
2
-
3
- export type ZodObjectOrWrapped = ZodObject<any, any> | ZodEffects<ZodObject<any, any>>;
4
-
5
- export interface ParsedField<AdditionalRenderable, FieldTypes = string> {
6
- key: string;
7
- type: string;
8
- required: boolean;
9
- default?: any;
10
- fieldConfig?: FieldConfig<AdditionalRenderable, FieldTypes>;
11
-
12
- // Field-specific
13
- options?: [string, string][]; // [value, label] for enums
14
- schema?: ParsedField<AdditionalRenderable, FieldTypes>[]; // For objects and arrays
15
- }
16
-
17
- export interface ParsedSchema<AdditionalRenderable = null, FieldTypes = string> {
18
- fields: Record<string, ParsedField<AdditionalRenderable, FieldTypes>>;
19
- }
20
-
21
- export type SuperRefineFunction = () => unknown;
22
-
23
- export type Renderable<AdditionalRenderable = null> =
24
- | string
25
- | number
26
- | boolean
27
- | null
28
- | undefined
29
- | AdditionalRenderable;
30
-
31
- export type FieldConfig<AdditionalRenderable = object, FieldTypes = string> = {
32
- fieldType?: FieldTypes;
33
- } & AdditionalRenderable;
package/src/global.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { FC as FCN, ReactNode } from 'react';
2
-
3
- declare global {
4
- type FCCP<P extends object = object> = P & {
5
- children: ReactNode;
6
- };
7
-
8
- type FCCR = ReactNode;
9
-
10
- type FCC<P = object> = FCN<P & FCCP>;
11
-
12
- type FC<P = object> = FCN<P>;
13
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './element';
2
- export { createZodSchemaFieldConfig } from './funcs/field-config';
3
- export * from './provider';
4
- export * from './type';
5
- export * from './useSchema';
package/src/provider.tsx DELETED
@@ -1,9 +0,0 @@
1
- import { FC, ReactNode } from 'react';
2
- import { ZodResolverContext } from './context';
3
- import { zodResolverProps } from './type';
4
- export const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({
5
- children,
6
- schema,
7
- }) => {
8
- return <ZodResolverContext.Provider value={schema}>{children}</ZodResolverContext.Provider>;
9
- };
package/src/type.ts DELETED
@@ -1,6 +0,0 @@
1
- import { ZodDiscriminatedUnion, ZodEffects, ZodObject } from 'zod';
2
-
3
- export type zodResolverProps =
4
- | ZodObject<any, any>
5
- | ZodEffects<any, any>
6
- | ZodDiscriminatedUnion<any, any>;
package/src/useSchema.tsx DELETED
@@ -1,15 +0,0 @@
1
- import { useContext } from 'use-context-selector';
2
- import { ZodEffects, ZodObject } from 'zod';
3
- import { ZodResolverContext } from './context';
4
- import { parseSchema } from './funcs/schema-parser';
5
-
6
- export const useSchema = () =>
7
- useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;
8
-
9
- export const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {
10
- const S = useSchema();
11
-
12
- const { fields } = parseSchema(S);
13
-
14
- return fields[key as string];
15
- };