@form-instant/react-resolver-zod 0.2.5 → 1.0.0-rc.1
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/funcs/field-config.d.ts +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/provider.d.ts +2 -2
- package/dist/react-resolver-zod.cjs.development.js +94 -29
- 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 +93 -31
- package/dist/react-resolver-zod.esm.js.map +1 -1
- package/dist/useSchema.d.ts +2 -2
- package/package.json +1 -1
@@ -1,5 +1,11 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
import { FieldConfig, SuperRefineFunction } from './types';
|
3
3
|
export declare const FIELD_CONFIG_SYMBOL: unique symbol;
|
4
|
+
declare module 'zod' {
|
5
|
+
interface ZodType<Output = any, Def = any, Input = Output> {
|
6
|
+
fieldConfig<AdditionalRenderable = null, FieldTypes = string>(config: FieldConfig<AdditionalRenderable, FieldTypes>): this;
|
7
|
+
}
|
8
|
+
}
|
9
|
+
export declare const extendZodWithFieldConfig: <AdditionalRenderable = null, FieldTypes = string>(zod: typeof z) => void;
|
4
10
|
export declare const createZodSchemaFieldConfig: <AdditionalRenderable = null, FieldTypes = string>() => (config: FieldConfig<AdditionalRenderable, FieldTypes>) => SuperRefineFunction;
|
5
11
|
export declare function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig;
|
package/dist/index.d.ts
CHANGED
package/dist/provider.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { FC, ReactNode } from
|
2
|
-
import { zodResolverProps } from
|
1
|
+
import { FC, ReactNode } from "react";
|
2
|
+
import { zodResolverProps } from "./type";
|
3
3
|
export declare const FormInstantProvider: FC<{
|
4
4
|
schema: zodResolverProps;
|
5
5
|
children: ReactNode;
|
@@ -8,6 +8,15 @@ var zod = require('zod');
|
|
8
8
|
|
9
9
|
var ZodResolverContext = /*#__PURE__*/useContextSelector.createContext({});
|
10
10
|
|
11
|
+
function _extends() {
|
12
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
13
|
+
for (var e = 1; e < arguments.length; e++) {
|
14
|
+
var t = arguments[e];
|
15
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
16
|
+
}
|
17
|
+
return n;
|
18
|
+
}, _extends.apply(null, arguments);
|
19
|
+
}
|
11
20
|
function _objectWithoutPropertiesLoose(r, e) {
|
12
21
|
if (null == r) return {};
|
13
22
|
var t = {};
|
@@ -29,6 +38,12 @@ function getDefaultValueInZodStack(schema) {
|
|
29
38
|
}
|
30
39
|
|
31
40
|
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol('GetFieldConfig');
|
41
|
+
var extendZodWithFieldConfig = function extendZodWithFieldConfig(zod) {
|
42
|
+
zod.ZodType.prototype.fieldConfig = function (config) {
|
43
|
+
this._def.fieldConfig = config;
|
44
|
+
return this;
|
45
|
+
};
|
46
|
+
};
|
32
47
|
var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
33
48
|
return function (config) {
|
34
49
|
var refinementFunction = function refinementFunction() {
|
@@ -40,20 +55,19 @@ var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
|
40
55
|
};
|
41
56
|
};
|
42
57
|
function getFieldConfigInZodStack(schema) {
|
43
|
-
|
44
|
-
if (
|
45
|
-
|
46
|
-
var refinementFunction = effect.refinement;
|
47
|
-
if (FIELD_CONFIG_SYMBOL in refinementFunction) {
|
48
|
-
return refinementFunction[FIELD_CONFIG_SYMBOL];
|
49
|
-
}
|
58
|
+
// Verifica si el esquema tiene fieldConfig directamente
|
59
|
+
if (schema._def.fieldConfig) {
|
60
|
+
return schema._def.fieldConfig;
|
50
61
|
}
|
51
|
-
|
52
|
-
|
62
|
+
// Si el esquema es un ZodEffects, busca en el innerType
|
63
|
+
if ('innerType' in schema._def) {
|
64
|
+
return getFieldConfigInZodStack(schema._def.innerType);
|
53
65
|
}
|
54
|
-
|
55
|
-
|
66
|
+
// Si el esquema es un ZodEffects con un schema interno, busca en el schema
|
67
|
+
if ('schema' in schema._def) {
|
68
|
+
return getFieldConfigInZodStack(schema._def.schema);
|
56
69
|
}
|
70
|
+
// Si no se encuentra fieldConfig, retorna un objeto vacío
|
57
71
|
return {};
|
58
72
|
}
|
59
73
|
|
@@ -68,10 +82,58 @@ function inferFieldType(schema, fieldType) {
|
|
68
82
|
}
|
69
83
|
|
70
84
|
var _excluded = ["fieldType"];
|
71
|
-
|
85
|
+
var typeMapping = {
|
86
|
+
ZodDiscriminatedUnion: function ZodDiscriminatedUnion(baseSchema, name, history, dependecys) {
|
87
|
+
var _baseSchema$_def = baseSchema._def,
|
88
|
+
optionsMap = _baseSchema$_def.optionsMap,
|
89
|
+
discriminator = _baseSchema$_def.discriminator,
|
90
|
+
fieldConfig = _baseSchema$_def.fieldConfig;
|
91
|
+
if (!Object.keys(dependecys).length) return [{}];
|
92
|
+
var optionKey = dependecys[discriminator];
|
93
|
+
var option = optionsMap.get(optionKey);
|
94
|
+
if (!option) return [{}];
|
95
|
+
var entries = Object.entries(option.shape);
|
96
|
+
var fields = entries.filter(function (_ref) {
|
97
|
+
var key = _ref[0];
|
98
|
+
return key !== discriminator;
|
99
|
+
}).map(function (_ref2) {
|
100
|
+
var key = _ref2[0],
|
101
|
+
field = _ref2[1];
|
102
|
+
return parseField(key, field, [history, name].join('.'), dependecys);
|
103
|
+
});
|
104
|
+
fields.unshift({
|
105
|
+
name: {
|
106
|
+
current: discriminator,
|
107
|
+
history: [history, name, discriminator].join('.')
|
108
|
+
},
|
109
|
+
type: 'discriminator',
|
110
|
+
required: true,
|
111
|
+
"default": optionKey,
|
112
|
+
fieldConfig: fieldConfig,
|
113
|
+
options: Array.from(optionsMap.keys()).map(function (key) {
|
114
|
+
return [String(key), String(key)];
|
115
|
+
})
|
116
|
+
});
|
117
|
+
return fields;
|
118
|
+
},
|
119
|
+
ZodObject: function ZodObject(baseSchema, name, history, dependecys) {
|
120
|
+
return Object.entries(baseSchema.shape).map(function (_ref3) {
|
121
|
+
var key = _ref3[0],
|
122
|
+
field = _ref3[1];
|
123
|
+
return parseField(key, field, [history, name].join('.'), dependecys);
|
124
|
+
});
|
125
|
+
},
|
126
|
+
ZodArray: function ZodArray(baseSchema, name, history, dependecys) {
|
127
|
+
return [parseField('0', baseSchema._def.type, [history, name].join('.'), dependecys)];
|
128
|
+
}
|
129
|
+
};
|
130
|
+
function parseField(name, schema, history, dependecys) {
|
72
131
|
if (history === void 0) {
|
73
132
|
history = '';
|
74
133
|
}
|
134
|
+
if (dependecys === void 0) {
|
135
|
+
dependecys = {};
|
136
|
+
}
|
75
137
|
var baseSchema = getBaseSchema(schema);
|
76
138
|
var _getFieldConfigInZodS = getFieldConfigInZodStack(schema),
|
77
139
|
fieldType = _getFieldConfigInZodS.fieldType,
|
@@ -91,16 +153,14 @@ function parseField(name, schema, history) {
|
|
91
153
|
}
|
92
154
|
}
|
93
155
|
// Arrays and objects
|
94
|
-
var
|
95
|
-
|
96
|
-
subSchema = Object.entries(baseSchema.shape).map(function (_ref) {
|
97
|
-
var key = _ref[0],
|
98
|
-
field = _ref[1];
|
99
|
-
return parseField(key, field, [history, name].join('.'));
|
100
|
-
});
|
101
|
-
}
|
156
|
+
var getSubSchema = typeMapping[baseSchema._def.typeName];
|
157
|
+
var subSchema = getSubSchema == null ? void 0 : getSubSchema(baseSchema, name, history, dependecys);
|
102
158
|
if (baseSchema._def.typeName === 'ZodArray') {
|
103
|
-
|
159
|
+
var _baseSchema$_def$minL, _baseSchema$_def$maxL;
|
160
|
+
fieldConfig = _extends({
|
161
|
+
min: ((_baseSchema$_def$minL = baseSchema._def.minLength) == null ? void 0 : _baseSchema$_def$minL.value) || 1,
|
162
|
+
max: (_baseSchema$_def$maxL = baseSchema._def.maxLength) == null ? void 0 : _baseSchema$_def$maxL.value
|
163
|
+
}, fieldConfig);
|
104
164
|
}
|
105
165
|
var resp = {
|
106
166
|
name: {
|
@@ -129,10 +189,11 @@ function getBaseSchema(schema) {
|
|
129
189
|
var parseSchema = function parseSchema(S) {
|
130
190
|
var objectSchema = S instanceof zod.z.ZodEffects ? S.innerType() : S;
|
131
191
|
var shape = objectSchema.shape;
|
132
|
-
var
|
133
|
-
|
134
|
-
|
135
|
-
|
192
|
+
var dependecys = objectSchema._def.fieldConfig;
|
193
|
+
var fields = Object.fromEntries(Object.entries(shape).map(function (_ref4) {
|
194
|
+
var key = _ref4[0],
|
195
|
+
field = _ref4[1];
|
196
|
+
return [key, parseField(key, field, '', dependecys)];
|
136
197
|
}));
|
137
198
|
return {
|
138
199
|
fields: fields
|
@@ -152,29 +213,33 @@ var useFields = function useFields(key) {
|
|
152
213
|
var FormInstantElement = function FormInstantElement(_ref) {
|
153
214
|
var name = _ref.name;
|
154
215
|
var field = useFields(name);
|
155
|
-
var inputs = Object.values(field.schema);
|
216
|
+
var inputs = Object.values(field.schema || {});
|
156
217
|
var id = react.useId();
|
157
218
|
return jsxRuntime.jsx(jsxRuntime.Fragment, {
|
158
219
|
children: inputs.map(function (props) {
|
220
|
+
var _props$name;
|
159
221
|
return jsxRuntime.jsx(react.Fragment, {
|
160
222
|
children: jsxRuntime.jsx(reactInputMapping.ElementMapping, {
|
161
223
|
formProps: props
|
162
224
|
})
|
163
|
-
}, id + "-" + props.name.history);
|
225
|
+
}, id + "-" + (((_props$name = props.name) == null ? void 0 : _props$name.history) || ''));
|
164
226
|
})
|
165
227
|
});
|
166
228
|
};
|
167
229
|
|
168
|
-
var FormInstantProvider = function
|
230
|
+
var FormInstantProvider = /*#__PURE__*/react.memo(function (_ref) {
|
169
231
|
var children = _ref.children,
|
170
232
|
schema = _ref.schema;
|
171
233
|
return jsxRuntime.jsx(ZodResolverContext.Provider, {
|
172
234
|
value: schema,
|
173
235
|
children: children
|
174
236
|
});
|
175
|
-
};
|
237
|
+
});
|
176
238
|
|
239
|
+
exports.FIELD_CONFIG_SYMBOL = FIELD_CONFIG_SYMBOL;
|
177
240
|
exports.FormInstantElement = FormInstantElement;
|
178
241
|
exports.FormInstantProvider = FormInstantProvider;
|
179
242
|
exports.createZodSchemaFieldConfig = createZodSchemaFieldConfig;
|
243
|
+
exports.extendZodWithFieldConfig = extendZodWithFieldConfig;
|
244
|
+
exports.getFieldConfigInZodStack = getFieldConfigInZodStack;
|
180
245
|
//# 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"],"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 { ParsedField } from '@form-instant/react-input-mapping';\nimport { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(name: string, schema: z.ZodTypeAny, history: string = ''): 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._def.typeName === 'ZodObject') {\n subSchema = Object.entries((baseSchema as any).shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny, [history, name].join('.')),\n );\n }\n if (baseSchema._def.typeName === 'ZodArray') {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n name: { current: name, history: [history, name].join('.') },\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 useSchemaBase = () =>\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 = useSchemaBase();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { Fragment, 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 (\n <Fragment key={`${id}-${props.name.history}`}>\n <ElementMapping formProps={props} />\n </Fragment>\n );\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","name","history","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","shape","_ref","key","field","join","resp","current","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchemaBase","useContext","useFields","_parseSchema","FormInstantElement","inputs","id","useId","_jsx","props","Fragment","children","ElementMapping","formProps","FormInstantProvider","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,CA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GAAIG,GAAAA,GAAAA,KAAK,CAAC3C,IAAI,CAACC,OAAS,CAEjC,CAAA;KAElB,CAAA;AACF,GAAA,CAAA,CAAA;AAEX;;ICvBa+C,mBAAmB,GAA0D,SAA7EA,mBAAmBA,CAAA7B,IAAA,EAG3B;AAAA,EAAA,IAFD0B,QAAQ,GAAA1B,IAAA,CAAR0B,QAAQ;IACRvE,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAEN,EAAA,OAAOoE,cAAA,CAACvE,kBAAkB,CAAC8E,QAAQ,EAAA;AAACjC,IAAAA,KAAK,EAAE1C,MAAM;AAAAuE,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 { z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\ndeclare module 'zod' {\n // @ts-expect-error\n interface ZodType<Output = any, Def = any, Input = Output> {\n fieldConfig<AdditionalRenderable = null, FieldTypes = string>(\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n ): this;\n }\n}\n\nexport const extendZodWithFieldConfig = <AdditionalRenderable = null, FieldTypes = string>(\n zod: typeof z,\n) => {\n (zod.ZodType.prototype as any).fieldConfig = function (\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n ) {\n this._def.fieldConfig = config;\n return this;\n };\n};\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 // Verifica si el esquema tiene fieldConfig directamente\n if (schema._def.fieldConfig) {\n return schema._def.fieldConfig as FieldConfig;\n }\n\n // Si el esquema es un ZodEffects, busca en el innerType\n if ('innerType' in schema._def) {\n return getFieldConfigInZodStack(schema._def.innerType as z.ZodAny);\n }\n\n // Si el esquema es un ZodEffects con un schema interno, busca en el schema\n if ('schema' in schema._def) {\n return getFieldConfigInZodStack(schema._def.schema as z.ZodAny);\n }\n\n // Si no se encuentra fieldConfig, retorna un objeto vacío\n return {} as FieldConfig;\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 { ParsedField } from '@form-instant/react-input-mapping';\nimport { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedSchema, ZodObjectOrWrapped } from './types';\n\nconst typeMapping = {\n ZodDiscriminatedUnion(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n const { optionsMap, discriminator, fieldConfig } = baseSchema._def;\n\n if (!Object.keys(dependecys).length) return [{}] as ParsedField<any, string>[];\n\n const optionKey = dependecys[discriminator];\n const option = optionsMap.get(optionKey);\n\n if (!option) return [{}] as ParsedField<any, string>[];\n\n const entries = Object.entries(option.shape);\n\n const fields = entries\n .filter(([key]) => key !== discriminator)\n .map(([key, field]) => {\n return parseField(\n key,\n field as z.ZodTypeAny,\n [history, name].join('.'),\n dependecys,\n );\n });\n\n fields.unshift({\n name: {\n current: discriminator,\n history: [history, name, discriminator].join('.'),\n },\n type: 'discriminator',\n required: true,\n default: optionKey,\n fieldConfig,\n options: Array.from(optionsMap.keys()).map((key) => [String(key), String(key)]),\n });\n\n return fields;\n },\n\n ZodObject(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n return Object.entries((baseSchema as any).shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny, [history, name].join('.'), dependecys),\n );\n },\n\n ZodArray(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n return [parseField('0', baseSchema._def.type, [history, name].join('.'), dependecys)];\n },\n};\n\nfunction parseField(\n name: string,\n schema: z.ZodTypeAny,\n history: string = '',\n dependecys: Record<string, any> = {},\n): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n let { 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 const getSubSchema = typeMapping[baseSchema._def.typeName as keyof typeof typeMapping];\n\n const subSchema = getSubSchema?.(baseSchema, name, history, dependecys);\n\n if (baseSchema._def.typeName === 'ZodArray') {\n fieldConfig = {\n min: baseSchema._def.minLength?.value || 1,\n max: baseSchema._def.maxLength?.value,\n ...fieldConfig,\n };\n }\n\n const resp = {\n name: { current: name, history: [history, name].join('.') },\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 dependecys = (objectSchema._def as any).fieldConfig;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [\n key,\n parseField(key, field as z.ZodTypeAny, '', dependecys),\n ]),\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 useSchemaBase = () =>\n useContext(ZodResolverContext) as\n | ZodObject<never, never>\n | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchemaBase();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { Fragment, 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 (\n <Fragment key={`${id}-${props.name?.history || ''}`}>\n <ElementMapping formProps={props} />\n </Fragment>\n );\n })}\n </>\n );\n};\n","import { FC, memo, ReactNode } from \"react\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\nexport const FormInstantProvider: FC<{\n schema: zodResolverProps;\n children: ReactNode;\n}> = memo(({ children, schema }) => {\n return (\n <ZodResolverContext.Provider value={schema}>\n {children}\n </ZodResolverContext.Provider>\n );\n});\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","extendZodWithFieldConfig","zod","ZodType","prototype","fieldConfig","config","createZodSchemaFieldConfig","refinementFunction","getFieldConfigInZodStack","inferFieldType","fieldType","type","typeName","zodToInputType","Map","match","get","typeMapping","ZodDiscriminatedUnion","baseSchema","name","history","dependecys","_baseSchema$_def","optionsMap","discriminator","Object","keys","length","optionKey","option","entries","shape","fields","filter","_ref","key","map","_ref2","field","parseField","join","unshift","current","required","options","Array","from","String","ZodObject","_ref3","ZodArray","getBaseSchema","_getFieldConfigInZodS","_objectWithoutPropertiesLoose","_excluded","values","optionValues","isArray","value","getSubSchema","subSchema","_baseSchema$_def$minL","_baseSchema$_def$maxL","_extends","min","minLength","max","maxLength","resp","isOptional","description","parseSchema","S","objectSchema","fromEntries","_ref4","useSchemaBase","useContext","useFields","_parseSchema","FormInstantElement","inputs","id","useId","_jsx","props","_props$name","Fragment","children","ElementMapping","formProps","FormInstantProvider","memo","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;;ICXaC,mBAAmB,gBAAGC,MAAM,CAAC,gBAAgB,EAAC;IAW9CC,wBAAwB,GAAG,SAA3BA,wBAAwBA,CACjCC,GAAa,EACb;EACCA,GAAG,CAACC,OAAO,CAACC,SAAiB,CAACC,WAAW,GAAG,UACzCC,MAAqD,EAAA;AAErD,IAAA,IAAI,CAACZ,IAAI,CAACW,WAAW,GAAGC,MAAM,CAAA;AAC9B,IAAA,OAAO,IAAI,CAAA;GACd,CAAA;AACL,EAAC;AAEYC,IAAAA,0BAA0B,GACnC,SADSA,0BAA0BA,GAAA;EAAA,OAEnC,UAACD,MAAqD,EAAyB;AAC3E,IAAA,IAAME,kBAAkB,GAAwB,SAA1CA,kBAAkBA,GAA6B;AACjD;KACH,CAAA;AAED;AACAA,IAAAA,kBAAkB,CAACT,mBAAmB,CAAC,GAAGO,MAAM,CAAA;AAEhD,IAAA,OAAOE,kBAAkB,CAAA;GAC5B,CAAA;AAAA,EAAA;AAEC,SAAUC,wBAAwBA,CAAClB,MAAoB,EAAA;AACzD;AACA,EAAA,IAAIA,MAAM,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BsD,QAAQ,GAAAtD,IAAA,CAARsD,QAAQ;IAAEnG,MAAM,GAAA6C,IAAA,CAAN7C,MAAM,CAAA;AAC3B,EAAA,OACE+F,cAAA,CAAClG,kBAAkB,CAAC2G,QAAQ,EAAA;AAACnC,IAAAA,KAAK,EAAErE,MAAM;AAAAmG,IAAAA,QAAA,EACvCA,QAAAA;AAAQ,GAAA,CACmB,CAAA;AAElC,CAAC;;;;;;;;;"}
|
@@ -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"),o=t.createContext({});function a(e){return e instanceof i.z.ZodDefault?e._def.defaultValue():e instanceof i.z.ZodEffects?
|
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"),o=t.createContext({});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 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 d(e){return e._def.fieldConfig?e._def.fieldConfig:"innerType"in e._def?d(e._def.innerType):"schema"in e._def?d(e._def.schema):{}}var c=["fieldType"],s={ZodDiscriminatedUnion:function(e,n,r,t){var i=e._def,o=i.optionsMap,a=i.discriminator,f=i.fieldConfig;if(!Object.keys(t).length)return[{}];var u=t[a],d=o.get(u);if(!d)return[{}];var c=Object.entries(d.shape).filter((function(e){return e[0]!==a})).map((function(e){return l(e[0],e[1],[r,n].join("."),t)}));return c.unshift({name:{current:a,history:[r,n,a].join(".")},type:"discriminator",required:!0,default:u,fieldConfig:f,options:Array.from(o.keys()).map((function(e){return[String(e),String(e)]}))}),c},ZodObject:function(e,n,r,t){return Object.entries(e.shape).map((function(e){return l(e[0],e[1],[r,n].join("."),t)}))},ZodArray:function(e,n,r,t){return[l("0",e._def.type,[r,n].join("."),t)]}};function l(e,n,r,t){void 0===r&&(r=""),void 0===t&&(t={});var i=p(n),o=d(n),u=o.fieldType,l=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}(o,c),m=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"}(i,u),v=f(n),y=i._def.values,g=[];y&&(g=Array.isArray(y)?y.map((function(e){return[e,e]})):Object.entries(y));var h,_,j=s[i._def.typeName],Z=null==j?void 0:j(i,e,r,t);return"ZodArray"===i._def.typeName&&(l=a({min:(null==(h=i._def.minLength)?void 0:h.value)||1,max:null==(_=i._def.maxLength)?void 0:_.value},l)),{name:{current:e,history:[r,e].join(".")},type:m,required:!n.isOptional(),default:v,fieldConfig:l,options:g,schema:Z,description:i.description}}function p(e){return"innerType"in e._def?p(e._def.innerType):"schema"in e._def?p(e._def.schema):e}var m=r.memo((function(n){return e.jsx(o.Provider,{value:n.schema,children:n.children})}));exports.FIELD_CONFIG_SYMBOL=u,exports.FormInstantElement=function(a){var f,u,d,c,s=(f=a.name,(u=t.useContext(o),d=u instanceof i.z.ZodEffects?u.innerType():u,c=d._def.fieldConfig,{fields:Object.fromEntries(Object.entries(d.shape).map((function(e){var n=e[0];return[n,l(n,e[1],"",c)]})))}).fields[f]),p=Object.values(s.schema||{}),m=r.useId();return e.jsx(e.Fragment,{children:p.map((function(t){var i;return e.jsx(r.Fragment,{children:e.jsx(n.ElementMapping,{formProps:t})},m+"-"+((null==(i=t.name)?void 0:i.history)||""))}))})},exports.FormInstantProvider=m,exports.createZodSchemaFieldConfig=function(){return function(e){var n=function(){};return n[u]=e,n}},exports.extendZodWithFieldConfig=function(e){e.ZodType.prototype.fieldConfig=function(e){return this._def.fieldConfig=e,this}},exports.getFieldConfigInZodStack=d;
|
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/element.tsx","../src/useSchema.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 { ParsedField } from '@form-instant/react-input-mapping';\nimport { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(name: string, schema: z.ZodTypeAny, history: string = ''): 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._def.typeName === 'ZodObject') {\n subSchema = Object.entries((baseSchema as any).shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny, [history, name].join('.')),\n );\n }\n if (baseSchema._def.typeName === 'ZodArray') {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n name: { current: name, history: [history, name].join('.') },\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 { ElementMapping } from '@form-instant/react-input-mapping';\nimport { Fragment, 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 (\n <Fragment key={`${id}-${props.name.history}`}>\n <ElementMapping formProps={props} />\n </Fragment>\n );\n })}\n </>\n );\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 useSchemaBase = () =>\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 = useSchemaBase();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\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","name","history","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldType","fieldConfig","_objectWithoutPropertiesLoose","_excluded","type","Map","get","inferFieldType","options","values","optionValues","Array","isArray","map","value","Object","entries","subSchema","shape","_ref","join","current","required","isOptional","default","description","key","S","objectSchema","field","useContext","fields","fromEntries","_ref2","inputs","id","useId","_jsx","props","Fragment","children","ElementMapping","formProps","jsx","Provider","config"],"mappings":"qKAGaA,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,EAEjC,CAED,MAAI,cAAeG,EAAYP,KACpBM,EAAyBC,EAAYP,KAAKG,WAEjD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AEA,MAAI,cAAeA,EAAOG,KACfgB,EAAcnB,EAAOG,KAAKG,WAEjC,WAAYN,EAAOG,KACZgB,EAAcnB,EAAOG,KAAKH,QAG9BA,CACX,4BEpDkC,SAAHwC,GAAgE,ICArCO,EHsD9BC,EAClBC,EEtDAC,GCDgDH,EDAcP,EAAJxB,MFsDxCgC,EGxDxBG,EAAAA,WAAWtD,GHyDLoD,EAAeD,aAAa/C,EAACA,EAACI,WAAa2C,EAAE1C,YAAc0C,EAO1D,CAAEI,OAJMhB,OAAOiB,YAClBjB,OAAOC,QAHGY,EAAaV,OAGDL,KAAI,SAAAoB,GAAA,IAAEP,EAAGO,EAAA,GAAO,MAAM,CAACP,EAAKhC,EAAWgC,EAAvBO,EAAA,IAAmD,OGxDrFF,OAEML,IDFRQ,EAASnB,OAAON,OAAOoB,EAAMlD,QAE7BwD,EAAKC,EAAAA,QAEX,OACIC,EAAAA,yBACKH,EAAOrB,KAAI,SAACyB,GACT,OACID,EAAAA,IAACE,EAAAA,SAAQ,CAAAC,SACLH,MAACI,iBAAc,CAACC,UAAWJ,KADbH,EAAMG,IAAAA,EAAM3C,KAAKC,aAOvD,8BEvB0F,SAA1DuB,GAI5B,OAAOkB,EAAAM,IAACnE,EAAmBoE,SAAQ,CAAC9B,MAF9BK,EAANxC,OAEiD6D,SAHzCrB,EAARqB,UAIJ,qCLHI,WADmC,OAEnC,SAACK,GACG,IAAMtD,EAA0C,aAOhD,OAFAA,EAAmBL,GAAuB2D,EAEnCtD,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/provider.tsx","../src/element.tsx","../src/useSchema.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 { z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\ndeclare module 'zod' {\n // @ts-expect-error\n interface ZodType<Output = any, Def = any, Input = Output> {\n fieldConfig<AdditionalRenderable = null, FieldTypes = string>(\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n ): this;\n }\n}\n\nexport const extendZodWithFieldConfig = <AdditionalRenderable = null, FieldTypes = string>(\n zod: typeof z,\n) => {\n (zod.ZodType.prototype as any).fieldConfig = function (\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n ) {\n this._def.fieldConfig = config;\n return this;\n };\n};\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 // Verifica si el esquema tiene fieldConfig directamente\n if (schema._def.fieldConfig) {\n return schema._def.fieldConfig as FieldConfig;\n }\n\n // Si el esquema es un ZodEffects, busca en el innerType\n if ('innerType' in schema._def) {\n return getFieldConfigInZodStack(schema._def.innerType as z.ZodAny);\n }\n\n // Si el esquema es un ZodEffects con un schema interno, busca en el schema\n if ('schema' in schema._def) {\n return getFieldConfigInZodStack(schema._def.schema as z.ZodAny);\n }\n\n // Si no se encuentra fieldConfig, retorna un objeto vacío\n return {} as FieldConfig;\n}\n","import { ParsedField } from '@form-instant/react-input-mapping';\nimport { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedSchema, ZodObjectOrWrapped } from './types';\n\nconst typeMapping = {\n ZodDiscriminatedUnion(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n const { optionsMap, discriminator, fieldConfig } = baseSchema._def;\n\n if (!Object.keys(dependecys).length) return [{}] as ParsedField<any, string>[];\n\n const optionKey = dependecys[discriminator];\n const option = optionsMap.get(optionKey);\n\n if (!option) return [{}] as ParsedField<any, string>[];\n\n const entries = Object.entries(option.shape);\n\n const fields = entries\n .filter(([key]) => key !== discriminator)\n .map(([key, field]) => {\n return parseField(\n key,\n field as z.ZodTypeAny,\n [history, name].join('.'),\n dependecys,\n );\n });\n\n fields.unshift({\n name: {\n current: discriminator,\n history: [history, name, discriminator].join('.'),\n },\n type: 'discriminator',\n required: true,\n default: optionKey,\n fieldConfig,\n options: Array.from(optionsMap.keys()).map((key) => [String(key), String(key)]),\n });\n\n return fields;\n },\n\n ZodObject(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n return Object.entries((baseSchema as any).shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny, [history, name].join('.'), dependecys),\n );\n },\n\n ZodArray(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n return [parseField('0', baseSchema._def.type, [history, name].join('.'), dependecys)];\n },\n};\n\nfunction parseField(\n name: string,\n schema: z.ZodTypeAny,\n history: string = '',\n dependecys: Record<string, any> = {},\n): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n let { 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 const getSubSchema = typeMapping[baseSchema._def.typeName as keyof typeof typeMapping];\n\n const subSchema = getSubSchema?.(baseSchema, name, history, dependecys);\n\n if (baseSchema._def.typeName === 'ZodArray') {\n fieldConfig = {\n min: baseSchema._def.minLength?.value || 1,\n max: baseSchema._def.maxLength?.value,\n ...fieldConfig,\n };\n }\n\n const resp = {\n name: { current: name, history: [history, name].join('.') },\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 dependecys = (objectSchema._def as any).fieldConfig;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [\n key,\n parseField(key, field as z.ZodTypeAny, '', dependecys),\n ]),\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 { FC, memo, ReactNode } from \"react\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\nexport const FormInstantProvider: FC<{\n schema: zodResolverProps;\n children: ReactNode;\n}> = memo(({ children, schema }) => {\n return (\n <ZodResolverContext.Provider value={schema}>\n {children}\n </ZodResolverContext.Provider>\n );\n});\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { Fragment, 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 (\n <Fragment key={`${id}-${props.name?.history || ''}`}>\n <ElementMapping formProps={props} />\n </Fragment>\n );\n })}\n </>\n );\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 useSchemaBase = () =>\n useContext(ZodResolverContext) as\n | ZodObject<never, never>\n | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchemaBase();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","FIELD_CONFIG_SYMBOL","Symbol","getFieldConfigInZodStack","fieldConfig","typeMapping","ZodDiscriminatedUnion","baseSchema","name","history","dependecys","_baseSchema$_def","optionsMap","discriminator","Object","keys","length","optionKey","option","get","fields","entries","shape","filter","_ref","map","_ref2","parseField","join","unshift","current","type","required","default","options","Array","from","key","String","ZodObject","_ref3","ZodArray","getBaseSchema","_getFieldConfigInZodS","fieldType","_objectWithoutPropertiesLoose","_excluded","typeName","Map","inferFieldType","values","optionValues","isArray","value","_baseSchema$_def$minL","_baseSchema$_def$maxL","getSubSchema","subSchema","_extends","min","minLength","max","maxLength","isOptional","description","FormInstantProvider","memo","_jsx","jsx","Provider","children","S","objectSchema","field","useContext","fromEntries","_ref4","inputs","id","useId","props","_props$name","Fragment","ElementMapping","formProps","config","refinementFunction","zod","ZodType","prototype","this"],"mappings":"qKAGaA,EAAqBC,EAAaA,cAAmB,4NCA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,EAACA,EAACI,WACbN,EAA0BC,EAAOM,kBAD5C,CAKJ,KCXaC,EAAsBC,OAAO,kBAmCpC,SAAUC,EAAyBT,GAErC,OAAIA,EAAOG,KAAKO,YACLV,EAAOG,KAAKO,YAInB,cAAeV,EAAOG,KACfM,EAAyBT,EAAOG,KAAKG,WAI5C,WAAYN,EAAOG,KACZM,EAAyBT,EAAOG,KAAKH,QAIzC,EACX,qBChDMW,EAAc,CAChBC,sBAAqB,SACjBC,EACAC,EACAC,EACAC,GAEA,IAAAC,EAAmDJ,EAAWV,KAAtDe,EAAUD,EAAVC,WAAYC,EAAaF,EAAbE,cAAeT,EAAWO,EAAXP,YAEnC,IAAKU,OAAOC,KAAKL,GAAYM,OAAQ,MAAO,CAAC,CAAA,GAE7C,IAAMC,EAAYP,EAAWG,GACvBK,EAASN,EAAWO,IAAIF,GAE9B,IAAKC,EAAQ,MAAO,CAAC,CAAA,GAErB,IAEME,E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AAWnF,GJiIHiF,EAAeD,aAAa5E,EAACA,EAACI,WAAawE,EAAEvE,YAAcuE,EAG3D7D,EAAc8D,EAAa3E,KAAaO,YASvC,CAAEgB,OAPMN,OAAO6D,YAClB7D,OAAOO,QALGmD,EAAalD,OAKDG,KAAI,SAAAmD,GAAA,IAAEvC,EAAGuC,EAAA,GAAO,MAAM,CACxCvC,EACAV,EAAWU,EAFuBuC,EAAA,GAEK,GAAIlE,GAC9C,OInICU,OAEMiB,IDJNwC,EAAS/D,OAAOoC,OAAOuB,EAAM/E,QAAU,CAAA,GAEvCoF,EAAKC,EAAAA,QAEX,OACIZ,EAAAA,yBACKU,EAAOpD,KAAI,SAACuD,GAAS,IAAAC,EAClB,OACId,EAAAA,IAACe,EAAAA,SACG,CAAAZ,SAAAH,EAAAC,IAACe,iBAAe,CAAAC,UAAWJ,KADbF,EAAE,MAAc,OAAVG,EAAAD,EAAMxE,WAAI,EAAVyE,EAAYxE,UAAW,SAOnE,mEJDI,WADmC,OAEnC,SAAC4E,GACG,IAAMC,EAA0C,aAOhD,OAFAA,EAAmBrF,GAAuBoF,EAEnCC,EACV,mCAtBmC,SACpCC,GAECA,EAAIC,QAAQC,UAAkBrF,YAAc,SACzCiF,GAGA,OADAK,KAAK7F,KAAKO,YAAciF,EACjBK,KAEf"}
|
@@ -1,11 +1,20 @@
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
2
2
|
import { ElementMapping } from '@form-instant/react-input-mapping';
|
3
|
-
import { useId, Fragment as Fragment$1 } from 'react';
|
3
|
+
import { useId, Fragment as Fragment$1, memo } from 'react';
|
4
4
|
import { createContext, useContext } from 'use-context-selector';
|
5
5
|
import { z } from 'zod';
|
6
6
|
|
7
7
|
var ZodResolverContext = /*#__PURE__*/createContext({});
|
8
8
|
|
9
|
+
function _extends() {
|
10
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
11
|
+
for (var e = 1; e < arguments.length; e++) {
|
12
|
+
var t = arguments[e];
|
13
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
14
|
+
}
|
15
|
+
return n;
|
16
|
+
}, _extends.apply(null, arguments);
|
17
|
+
}
|
9
18
|
function _objectWithoutPropertiesLoose(r, e) {
|
10
19
|
if (null == r) return {};
|
11
20
|
var t = {};
|
@@ -27,6 +36,12 @@ function getDefaultValueInZodStack(schema) {
|
|
27
36
|
}
|
28
37
|
|
29
38
|
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol('GetFieldConfig');
|
39
|
+
var extendZodWithFieldConfig = function extendZodWithFieldConfig(zod) {
|
40
|
+
zod.ZodType.prototype.fieldConfig = function (config) {
|
41
|
+
this._def.fieldConfig = config;
|
42
|
+
return this;
|
43
|
+
};
|
44
|
+
};
|
30
45
|
var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
31
46
|
return function (config) {
|
32
47
|
var refinementFunction = function refinementFunction() {
|
@@ -38,20 +53,19 @@ var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
|
38
53
|
};
|
39
54
|
};
|
40
55
|
function getFieldConfigInZodStack(schema) {
|
41
|
-
|
42
|
-
if (
|
43
|
-
|
44
|
-
var refinementFunction = effect.refinement;
|
45
|
-
if (FIELD_CONFIG_SYMBOL in refinementFunction) {
|
46
|
-
return refinementFunction[FIELD_CONFIG_SYMBOL];
|
47
|
-
}
|
56
|
+
// Verifica si el esquema tiene fieldConfig directamente
|
57
|
+
if (schema._def.fieldConfig) {
|
58
|
+
return schema._def.fieldConfig;
|
48
59
|
}
|
49
|
-
|
50
|
-
|
60
|
+
// Si el esquema es un ZodEffects, busca en el innerType
|
61
|
+
if ('innerType' in schema._def) {
|
62
|
+
return getFieldConfigInZodStack(schema._def.innerType);
|
51
63
|
}
|
52
|
-
|
53
|
-
|
64
|
+
// Si el esquema es un ZodEffects con un schema interno, busca en el schema
|
65
|
+
if ('schema' in schema._def) {
|
66
|
+
return getFieldConfigInZodStack(schema._def.schema);
|
54
67
|
}
|
68
|
+
// Si no se encuentra fieldConfig, retorna un objeto vacío
|
55
69
|
return {};
|
56
70
|
}
|
57
71
|
|
@@ -66,10 +80,58 @@ function inferFieldType(schema, fieldType) {
|
|
66
80
|
}
|
67
81
|
|
68
82
|
var _excluded = ["fieldType"];
|
69
|
-
|
83
|
+
var typeMapping = {
|
84
|
+
ZodDiscriminatedUnion: function ZodDiscriminatedUnion(baseSchema, name, history, dependecys) {
|
85
|
+
var _baseSchema$_def = baseSchema._def,
|
86
|
+
optionsMap = _baseSchema$_def.optionsMap,
|
87
|
+
discriminator = _baseSchema$_def.discriminator,
|
88
|
+
fieldConfig = _baseSchema$_def.fieldConfig;
|
89
|
+
if (!Object.keys(dependecys).length) return [{}];
|
90
|
+
var optionKey = dependecys[discriminator];
|
91
|
+
var option = optionsMap.get(optionKey);
|
92
|
+
if (!option) return [{}];
|
93
|
+
var entries = Object.entries(option.shape);
|
94
|
+
var fields = entries.filter(function (_ref) {
|
95
|
+
var key = _ref[0];
|
96
|
+
return key !== discriminator;
|
97
|
+
}).map(function (_ref2) {
|
98
|
+
var key = _ref2[0],
|
99
|
+
field = _ref2[1];
|
100
|
+
return parseField(key, field, [history, name].join('.'), dependecys);
|
101
|
+
});
|
102
|
+
fields.unshift({
|
103
|
+
name: {
|
104
|
+
current: discriminator,
|
105
|
+
history: [history, name, discriminator].join('.')
|
106
|
+
},
|
107
|
+
type: 'discriminator',
|
108
|
+
required: true,
|
109
|
+
"default": optionKey,
|
110
|
+
fieldConfig: fieldConfig,
|
111
|
+
options: Array.from(optionsMap.keys()).map(function (key) {
|
112
|
+
return [String(key), String(key)];
|
113
|
+
})
|
114
|
+
});
|
115
|
+
return fields;
|
116
|
+
},
|
117
|
+
ZodObject: function ZodObject(baseSchema, name, history, dependecys) {
|
118
|
+
return Object.entries(baseSchema.shape).map(function (_ref3) {
|
119
|
+
var key = _ref3[0],
|
120
|
+
field = _ref3[1];
|
121
|
+
return parseField(key, field, [history, name].join('.'), dependecys);
|
122
|
+
});
|
123
|
+
},
|
124
|
+
ZodArray: function ZodArray(baseSchema, name, history, dependecys) {
|
125
|
+
return [parseField('0', baseSchema._def.type, [history, name].join('.'), dependecys)];
|
126
|
+
}
|
127
|
+
};
|
128
|
+
function parseField(name, schema, history, dependecys) {
|
70
129
|
if (history === void 0) {
|
71
130
|
history = '';
|
72
131
|
}
|
132
|
+
if (dependecys === void 0) {
|
133
|
+
dependecys = {};
|
134
|
+
}
|
73
135
|
var baseSchema = getBaseSchema(schema);
|
74
136
|
var _getFieldConfigInZodS = getFieldConfigInZodStack(schema),
|
75
137
|
fieldType = _getFieldConfigInZodS.fieldType,
|
@@ -89,16 +151,14 @@ function parseField(name, schema, history) {
|
|
89
151
|
}
|
90
152
|
}
|
91
153
|
// Arrays and objects
|
92
|
-
var
|
93
|
-
|
94
|
-
subSchema = Object.entries(baseSchema.shape).map(function (_ref) {
|
95
|
-
var key = _ref[0],
|
96
|
-
field = _ref[1];
|
97
|
-
return parseField(key, field, [history, name].join('.'));
|
98
|
-
});
|
99
|
-
}
|
154
|
+
var getSubSchema = typeMapping[baseSchema._def.typeName];
|
155
|
+
var subSchema = getSubSchema == null ? void 0 : getSubSchema(baseSchema, name, history, dependecys);
|
100
156
|
if (baseSchema._def.typeName === 'ZodArray') {
|
101
|
-
|
157
|
+
var _baseSchema$_def$minL, _baseSchema$_def$maxL;
|
158
|
+
fieldConfig = _extends({
|
159
|
+
min: ((_baseSchema$_def$minL = baseSchema._def.minLength) == null ? void 0 : _baseSchema$_def$minL.value) || 1,
|
160
|
+
max: (_baseSchema$_def$maxL = baseSchema._def.maxLength) == null ? void 0 : _baseSchema$_def$maxL.value
|
161
|
+
}, fieldConfig);
|
102
162
|
}
|
103
163
|
var resp = {
|
104
164
|
name: {
|
@@ -127,10 +187,11 @@ function getBaseSchema(schema) {
|
|
127
187
|
var parseSchema = function parseSchema(S) {
|
128
188
|
var objectSchema = S instanceof z.ZodEffects ? S.innerType() : S;
|
129
189
|
var shape = objectSchema.shape;
|
130
|
-
var
|
131
|
-
|
132
|
-
|
133
|
-
|
190
|
+
var dependecys = objectSchema._def.fieldConfig;
|
191
|
+
var fields = Object.fromEntries(Object.entries(shape).map(function (_ref4) {
|
192
|
+
var key = _ref4[0],
|
193
|
+
field = _ref4[1];
|
194
|
+
return [key, parseField(key, field, '', dependecys)];
|
134
195
|
}));
|
135
196
|
return {
|
136
197
|
fields: fields
|
@@ -150,27 +211,28 @@ var useFields = function useFields(key) {
|
|
150
211
|
var FormInstantElement = function FormInstantElement(_ref) {
|
151
212
|
var name = _ref.name;
|
152
213
|
var field = useFields(name);
|
153
|
-
var inputs = Object.values(field.schema);
|
214
|
+
var inputs = Object.values(field.schema || {});
|
154
215
|
var id = useId();
|
155
216
|
return jsx(Fragment, {
|
156
217
|
children: inputs.map(function (props) {
|
218
|
+
var _props$name;
|
157
219
|
return jsx(Fragment$1, {
|
158
220
|
children: jsx(ElementMapping, {
|
159
221
|
formProps: props
|
160
222
|
})
|
161
|
-
}, id + "-" + props.name.history);
|
223
|
+
}, id + "-" + (((_props$name = props.name) == null ? void 0 : _props$name.history) || ''));
|
162
224
|
})
|
163
225
|
});
|
164
226
|
};
|
165
227
|
|
166
|
-
var FormInstantProvider = function
|
228
|
+
var FormInstantProvider = /*#__PURE__*/memo(function (_ref) {
|
167
229
|
var children = _ref.children,
|
168
230
|
schema = _ref.schema;
|
169
231
|
return jsx(ZodResolverContext.Provider, {
|
170
232
|
value: schema,
|
171
233
|
children: children
|
172
234
|
});
|
173
|
-
};
|
235
|
+
});
|
174
236
|
|
175
|
-
export { FormInstantElement, FormInstantProvider, createZodSchemaFieldConfig };
|
237
|
+
export { FIELD_CONFIG_SYMBOL, FormInstantElement, FormInstantProvider, createZodSchemaFieldConfig, extendZodWithFieldConfig, getFieldConfigInZodStack };
|
176
238
|
//# 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"],"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 { ParsedField } from '@form-instant/react-input-mapping';\nimport { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedSchema, ZodObjectOrWrapped } from './types';\n\nfunction parseField(name: string, schema: z.ZodTypeAny, history: string = ''): 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._def.typeName === 'ZodObject') {\n subSchema = Object.entries((baseSchema as any).shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny, [history, name].join('.')),\n );\n }\n if (baseSchema._def.typeName === 'ZodArray') {\n subSchema = [parseField('0', baseSchema._def.type)];\n }\n\n const resp = {\n name: { current: name, history: [history, name].join('.') },\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 useSchemaBase = () =>\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 = useSchemaBase();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { Fragment, 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 (\n <Fragment key={`${id}-${props.name.history}`}>\n <ElementMapping formProps={props} />\n </Fragment>\n );\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","name","history","baseSchema","getBaseSchema","_getFieldConfigInZodS","fieldConfig","_objectWithoutPropertiesLoose","_excluded","options","values","optionValues","Array","isArray","Object","entries","map","value","subSchema","shape","_ref","key","field","join","resp","current","required","isOptional","description","parseSchema","S","objectSchema","fields","fromEntries","_ref2","useSchemaBase","useContext","useFields","_parseSchema","FormInstantElement","inputs","id","useId","_jsx","props","Fragment","children","ElementMapping","formProps","FormInstantProvider","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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AA,KAAK,EAAE1C,MAAM;AAAAuE,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 { z } from 'zod';\nimport { FieldConfig, SuperRefineFunction } from './types';\nexport const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');\n\ndeclare module 'zod' {\n // @ts-expect-error\n interface ZodType<Output = any, Def = any, Input = Output> {\n fieldConfig<AdditionalRenderable = null, FieldTypes = string>(\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n ): this;\n }\n}\n\nexport const extendZodWithFieldConfig = <AdditionalRenderable = null, FieldTypes = string>(\n zod: typeof z,\n) => {\n (zod.ZodType.prototype as any).fieldConfig = function (\n config: FieldConfig<AdditionalRenderable, FieldTypes>,\n ) {\n this._def.fieldConfig = config;\n return this;\n };\n};\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 // Verifica si el esquema tiene fieldConfig directamente\n if (schema._def.fieldConfig) {\n return schema._def.fieldConfig as FieldConfig;\n }\n\n // Si el esquema es un ZodEffects, busca en el innerType\n if ('innerType' in schema._def) {\n return getFieldConfigInZodStack(schema._def.innerType as z.ZodAny);\n }\n\n // Si el esquema es un ZodEffects con un schema interno, busca en el schema\n if ('schema' in schema._def) {\n return getFieldConfigInZodStack(schema._def.schema as z.ZodAny);\n }\n\n // Si no se encuentra fieldConfig, retorna un objeto vacío\n return {} as FieldConfig;\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 { ParsedField } from '@form-instant/react-input-mapping';\nimport { z } from 'zod';\nimport { getDefaultValueInZodStack } from './default-values';\nimport { getFieldConfigInZodStack } from './field-config';\nimport { inferFieldType } from './field-type-inference';\nimport { ParsedSchema, ZodObjectOrWrapped } from './types';\n\nconst typeMapping = {\n ZodDiscriminatedUnion(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n const { optionsMap, discriminator, fieldConfig } = baseSchema._def;\n\n if (!Object.keys(dependecys).length) return [{}] as ParsedField<any, string>[];\n\n const optionKey = dependecys[discriminator];\n const option = optionsMap.get(optionKey);\n\n if (!option) return [{}] as ParsedField<any, string>[];\n\n const entries = Object.entries(option.shape);\n\n const fields = entries\n .filter(([key]) => key !== discriminator)\n .map(([key, field]) => {\n return parseField(\n key,\n field as z.ZodTypeAny,\n [history, name].join('.'),\n dependecys,\n );\n });\n\n fields.unshift({\n name: {\n current: discriminator,\n history: [history, name, discriminator].join('.'),\n },\n type: 'discriminator',\n required: true,\n default: optionKey,\n fieldConfig,\n options: Array.from(optionsMap.keys()).map((key) => [String(key), String(key)]),\n });\n\n return fields;\n },\n\n ZodObject(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n return Object.entries((baseSchema as any).shape).map(([key, field]) =>\n parseField(key, field as z.ZodTypeAny, [history, name].join('.'), dependecys),\n );\n },\n\n ZodArray(\n baseSchema: z.ZodTypeAny,\n name: string,\n history: string,\n dependecys: Record<string, any>,\n ) {\n return [parseField('0', baseSchema._def.type, [history, name].join('.'), dependecys)];\n },\n};\n\nfunction parseField(\n name: string,\n schema: z.ZodTypeAny,\n history: string = '',\n dependecys: Record<string, any> = {},\n): ParsedField<any> {\n const baseSchema = getBaseSchema(schema);\n let { 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 const getSubSchema = typeMapping[baseSchema._def.typeName as keyof typeof typeMapping];\n\n const subSchema = getSubSchema?.(baseSchema, name, history, dependecys);\n\n if (baseSchema._def.typeName === 'ZodArray') {\n fieldConfig = {\n min: baseSchema._def.minLength?.value || 1,\n max: baseSchema._def.maxLength?.value,\n ...fieldConfig,\n };\n }\n\n const resp = {\n name: { current: name, history: [history, name].join('.') },\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 dependecys = (objectSchema._def as any).fieldConfig;\n\n const fields = Object.fromEntries(\n Object.entries(shape).map(([key, field]) => [\n key,\n parseField(key, field as z.ZodTypeAny, '', dependecys),\n ]),\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 useSchemaBase = () =>\n useContext(ZodResolverContext) as\n | ZodObject<never, never>\n | ZodEffects<never, never>;\n\nexport const useFields = <Sc extends Record<string, any>>(key: keyof Sc) => {\n const S = useSchemaBase();\n\n const { fields } = parseSchema(S);\n\n return fields[key as string];\n};\n","import { ElementMapping } from '@form-instant/react-input-mapping';\nimport { Fragment, 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 (\n <Fragment key={`${id}-${props.name?.history || ''}`}>\n <ElementMapping formProps={props} />\n </Fragment>\n );\n })}\n </>\n );\n};\n","import { FC, memo, ReactNode } from \"react\";\nimport { ZodResolverContext } from \"./context\";\nimport { zodResolverProps } from \"./type\";\nexport const FormInstantProvider: FC<{\n schema: zodResolverProps;\n children: ReactNode;\n}> = memo(({ children, schema }) => {\n return (\n <ZodResolverContext.Provider value={schema}>\n {children}\n </ZodResolverContext.Provider>\n );\n});\n"],"names":["ZodResolverContext","createContext","getDefaultValueInZodStack","schema","z","ZodDefault","_def","defaultValue","ZodEffects","innerType","undefined","FIELD_CONFIG_SYMBOL","Symbol","extendZodWithFieldConfig","zod","ZodType","prototype","fieldConfig","config","createZodSchemaFieldConfig","refinementFunction","getFieldConfigInZodStack","inferFieldType","fieldType","type","typeName","zodToInputType","Map","match","get","typeMapping","ZodDiscriminatedUnion","baseSchema","name","history","dependecys","_baseSchema$_def","optionsMap","discriminator","Object","keys","length","optionKey","option","entries","shape","fields","filter","_ref","key","map","_ref2","field","parseField","join","unshift","current","required","options","Array","from","String","ZodObject","_ref3","ZodArray","getBaseSchema","_getFieldConfigInZodS","_objectWithoutPropertiesLoose","_excluded","values","optionValues","isArray","value","getSubSchema","subSchema","_baseSchema$_def$minL","_baseSchema$_def$maxL","_extends","min","minLength","max","maxLength","resp","isOptional","description","parseSchema","S","objectSchema","fromEntries","_ref4","useSchemaBase","useContext","useFields","_parseSchema","FormInstantElement","inputs","id","useId","_jsx","props","_props$name","Fragment","children","ElementMapping","formProps","FormInstantProvider","memo","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,Y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aBase: () => ZodObject<never, never
|
1
|
+
import { ZodEffects, ZodObject } from "zod";
|
2
|
+
export declare const useSchemaBase: () => ZodObject<never, never> | ZodEffects<never, never>;
|
3
3
|
export declare const useFields: <Sc extends Record<string, any>>(key: keyof Sc) => import("@form-instant/react-input-mapping").ParsedField<null, string>;
|