@form-instant/react-resolver-zod 0.0.14-alpha → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -160
- package/dist/context.d.ts +2 -3
- package/dist/funcs/default-values.d.ts +2 -2
- package/dist/funcs/field-config.d.ts +2 -2
- package/dist/funcs/field-type-inference.d.ts +3 -3
- package/dist/funcs/schema-parser.d.ts +1 -1
- package/dist/funcs/types.d.ts +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/provider.d.ts +2 -2
- package/dist/react-resolver-zod.cjs.development.js +12 -11
- 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 +11 -10
- package/dist/react-resolver-zod.esm.js.map +1 -1
- package/dist/type.d.ts +1 -1
- package/dist/useSchema.d.ts +1 -1
- package/package.json +16 -6
- package/src/context.tsx +2 -2
- package/src/element.tsx +3 -3
- package/src/funcs/default-values.ts +2 -2
- package/src/funcs/field-config.ts +6 -6
- package/src/funcs/field-type-inference.ts +12 -12
- package/src/funcs/schema-parser.ts +8 -8
- package/src/funcs/types.ts +1 -1
- package/src/global.d.ts +1 -1
- package/src/index.ts +5 -5
- package/src/provider.tsx +3 -4
- package/src/type.ts +1 -1
- package/src/useSchema.tsx +4 -4
package/README.md
CHANGED
@@ -1,160 +1 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
Congrats! You just saved yourself hours of work by bootstrapping this project with DTS. Let’s get you oriented with what’s here and how to use it.
|
4
|
-
|
5
|
-
> This DTS setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
|
6
|
-
|
7
|
-
> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
|
8
|
-
|
9
|
-
## Commands
|
10
|
-
|
11
|
-
DTS scaffolds your new library inside `/src`, and also sets up a [Vite-based](https://vitejs.dev) playground for it inside `/example`.
|
12
|
-
|
13
|
-
The recommended workflow is to run DTS in one terminal:
|
14
|
-
|
15
|
-
```bash
|
16
|
-
npm start # or yarn start
|
17
|
-
```
|
18
|
-
|
19
|
-
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.
|
20
|
-
|
21
|
-
Then run the example inside another:
|
22
|
-
|
23
|
-
```bash
|
24
|
-
cd example
|
25
|
-
npm i # or yarn to install dependencies
|
26
|
-
npm start # or yarn start
|
27
|
-
```
|
28
|
-
|
29
|
-
The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure DTS is running in watch mode like we recommend above.
|
30
|
-
|
31
|
-
To do a one-off build, use `npm run build` or `yarn build`.
|
32
|
-
|
33
|
-
To run tests, use `npm test` or `yarn test`.
|
34
|
-
|
35
|
-
## Configuration
|
36
|
-
|
37
|
-
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
|
38
|
-
|
39
|
-
### Jest
|
40
|
-
|
41
|
-
Jest tests are set up to run with `npm test` or `yarn test`.
|
42
|
-
|
43
|
-
### Bundle analysis
|
44
|
-
|
45
|
-
Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`.
|
46
|
-
|
47
|
-
#### Setup Files
|
48
|
-
|
49
|
-
This is the folder structure we set up for you:
|
50
|
-
|
51
|
-
```txt
|
52
|
-
/example
|
53
|
-
index.html
|
54
|
-
index.tsx # test your component here in a demo app
|
55
|
-
package.json
|
56
|
-
tsconfig.json
|
57
|
-
/src
|
58
|
-
index.tsx # EDIT THIS
|
59
|
-
/test
|
60
|
-
index.test.tsx # EDIT THIS
|
61
|
-
.gitignore
|
62
|
-
package.json
|
63
|
-
README.md # EDIT THIS
|
64
|
-
tsconfig.json
|
65
|
-
```
|
66
|
-
|
67
|
-
#### React Testing Library
|
68
|
-
|
69
|
-
We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
|
70
|
-
|
71
|
-
### Rollup
|
72
|
-
|
73
|
-
DTS uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
|
74
|
-
|
75
|
-
### TypeScript
|
76
|
-
|
77
|
-
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
|
78
|
-
|
79
|
-
## Continuous Integration
|
80
|
-
|
81
|
-
### GitHub Actions
|
82
|
-
|
83
|
-
Two actions are added by default:
|
84
|
-
|
85
|
-
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
|
86
|
-
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)
|
87
|
-
|
88
|
-
## Optimizations
|
89
|
-
|
90
|
-
Please see the main `dts` [optimizations docs](https://github.com/weiran-zsd/dts-cli#optimizations). In particular, know that you can take advantage of development-only optimizations:
|
91
|
-
|
92
|
-
```js
|
93
|
-
// ./types/index.d.ts
|
94
|
-
declare var __DEV__: boolean;
|
95
|
-
|
96
|
-
// inside your code...
|
97
|
-
if (__DEV__) {
|
98
|
-
console.log('foo');
|
99
|
-
}
|
100
|
-
```
|
101
|
-
|
102
|
-
You can also choose to install and use [invariant](https://github.com/weiran-zsd/dts-cli#invariant) and [warning](https://github.com/weiran-zsd/dts-cli#warning) functions.
|
103
|
-
|
104
|
-
## Module Formats
|
105
|
-
|
106
|
-
CJS, ESModules, and UMD module formats are supported.
|
107
|
-
|
108
|
-
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
|
109
|
-
|
110
|
-
## Deploying the Example Playground
|
111
|
-
|
112
|
-
The Playground is just a simple [Vite](https://vitejs.dev) app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for **manually** deploying with the Netlify CLI (`npm i -g netlify-cli`):
|
113
|
-
|
114
|
-
```bash
|
115
|
-
cd example # if not already in the example folder
|
116
|
-
npm run build # builds to dist
|
117
|
-
netlify deploy # deploy the dist folder
|
118
|
-
```
|
119
|
-
|
120
|
-
Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:
|
121
|
-
|
122
|
-
```bash
|
123
|
-
netlify init
|
124
|
-
# build command: yarn build && cd example && yarn && yarn build
|
125
|
-
# directory to deploy: example/dist
|
126
|
-
# pick yes for netlify.toml
|
127
|
-
```
|
128
|
-
|
129
|
-
## Named Exports
|
130
|
-
|
131
|
-
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
|
132
|
-
|
133
|
-
## Including Styles
|
134
|
-
|
135
|
-
There are many ways to ship styles, including with CSS-in-JS. DTS has no opinion on this, configure how you like.
|
136
|
-
|
137
|
-
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
|
138
|
-
|
139
|
-
## Publishing to NPM
|
140
|
-
|
141
|
-
We recommend using [np](https://github.com/sindresorhus/np).
|
142
|
-
|
143
|
-
## Usage with Lerna
|
144
|
-
|
145
|
-
When creating a new package with DTS within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_.
|
146
|
-
|
147
|
-
The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.
|
148
|
-
|
149
|
-
Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.
|
150
|
-
|
151
|
-
```diff
|
152
|
-
"alias": {
|
153
|
-
- "react": "../node_modules/react",
|
154
|
-
- "react-dom": "../node_modules/react-dom"
|
155
|
-
+ "react": "../../../node_modules/react",
|
156
|
-
+ "react-dom": "../../../node_modules/react-dom"
|
157
|
-
},
|
158
|
-
```
|
159
|
-
|
160
|
-
An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/formium/tsdx/issues/64)
|
1
|
+
# GO to **_[@form-instant/react-input-mapping](https://www.npmjs.com/package/@form-instant/react-input-mapping)_**
|
package/dist/context.d.ts
CHANGED
@@ -1,3 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
export declare const ZodResolverContext: import("react").Context<zodResolverProps>;
|
1
|
+
import { zodResolverProps } from './type';
|
2
|
+
export declare const ZodResolverContext: import("use-context-selector").Context<zodResolverProps>;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { z } from
|
2
|
-
import { ZodObjectOrWrapped } from
|
1
|
+
import { z } from 'zod';
|
2
|
+
import { ZodObjectOrWrapped } from './types';
|
3
3
|
export declare function getDefaultValueInZodStack(schema: z.ZodTypeAny): any;
|
4
4
|
export declare function getDefaultValues(schema: ZodObjectOrWrapped): Record<string, any>;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { z } from
|
2
|
-
import { FieldConfig, SuperRefineFunction } from
|
1
|
+
import { z } from 'zod';
|
2
|
+
import { FieldConfig, SuperRefineFunction } from './types';
|
3
3
|
export declare const FIELD_CONFIG_SYMBOL: unique symbol;
|
4
4
|
export declare const createZodSchemaFieldConfig: <AdditionalRenderable = null, FieldTypes = string>() => (config: FieldConfig<AdditionalRenderable, FieldTypes>) => SuperRefineFunction;
|
5
5
|
export declare function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig;
|
@@ -1,3 +1,3 @@
|
|
1
|
-
import { z } from
|
2
|
-
import { FieldConfig } from
|
3
|
-
export declare function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[
|
1
|
+
import { z } from 'zod';
|
2
|
+
import { FieldConfig } from './types';
|
3
|
+
export declare function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string;
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import { ParsedSchema, ZodObjectOrWrapped } from
|
1
|
+
import { ParsedSchema, ZodObjectOrWrapped } from './types';
|
2
2
|
export declare const parseSchema: (S: ZodObjectOrWrapped) => ParsedSchema;
|
package/dist/funcs/types.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ZodEffects, ZodObject } from
|
1
|
+
import { ZodEffects, ZodObject } from 'zod';
|
2
2
|
export type ZodObjectOrWrapped = ZodObject<any, any> | ZodEffects<ZodObject<any, any>>;
|
3
3
|
export interface ParsedField<AdditionalRenderable, FieldTypes = string> {
|
4
4
|
key: string;
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
export * from
|
2
|
-
export { createZodSchemaFieldConfig } from
|
3
|
-
export * from
|
4
|
-
export * from
|
5
|
-
export * from
|
1
|
+
export * from './element';
|
2
|
+
export { createZodSchemaFieldConfig } from './funcs/field-config';
|
3
|
+
export * from './provider';
|
4
|
+
export * from './type';
|
5
|
+
export * from './useSchema';
|
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;
|
@@ -3,6 +3,7 @@
|
|
3
3
|
var jsxRuntime = require('react/jsx-runtime');
|
4
4
|
var reactInputMapping = require('@form-instant/react-input-mapping');
|
5
5
|
var react = require('react');
|
6
|
+
var useContextSelector = require('use-context-selector');
|
6
7
|
var zod = require('zod');
|
7
8
|
|
8
9
|
function _extends() {
|
@@ -24,7 +25,7 @@ function _objectWithoutPropertiesLoose(r, e) {
|
|
24
25
|
return t;
|
25
26
|
}
|
26
27
|
|
27
|
-
var ZodResolverContext = /*#__PURE__*/
|
28
|
+
var ZodResolverContext = /*#__PURE__*/useContextSelector.createContext({});
|
28
29
|
|
29
30
|
function getDefaultValueInZodStack(schema) {
|
30
31
|
if (schema instanceof zod.z.ZodDefault) {
|
@@ -36,7 +37,7 @@ function getDefaultValueInZodStack(schema) {
|
|
36
37
|
return undefined;
|
37
38
|
}
|
38
39
|
|
39
|
-
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol(
|
40
|
+
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol('GetFieldConfig');
|
40
41
|
var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
41
42
|
return function (config) {
|
42
43
|
var refinementFunction = function refinementFunction() {
|
@@ -49,17 +50,17 @@ var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
|
49
50
|
};
|
50
51
|
function getFieldConfigInZodStack(schema) {
|
51
52
|
var typedSchema = schema;
|
52
|
-
if (typedSchema._def.typeName ===
|
53
|
+
if (typedSchema._def.typeName === 'ZodEffects') {
|
53
54
|
var effect = typedSchema._def.effect;
|
54
55
|
var refinementFunction = effect.refinement;
|
55
56
|
if (FIELD_CONFIG_SYMBOL in refinementFunction) {
|
56
57
|
return refinementFunction[FIELD_CONFIG_SYMBOL];
|
57
58
|
}
|
58
59
|
}
|
59
|
-
if (
|
60
|
+
if ('innerType' in typedSchema._def) {
|
60
61
|
return getFieldConfigInZodStack(typedSchema._def.innerType);
|
61
62
|
}
|
62
|
-
if (
|
63
|
+
if ('schema' in typedSchema._def) {
|
63
64
|
return getFieldConfigInZodStack(typedSchema._def.schema);
|
64
65
|
}
|
65
66
|
return {};
|
@@ -70,9 +71,9 @@ function inferFieldType(schema, fieldType) {
|
|
70
71
|
return fieldType;
|
71
72
|
}
|
72
73
|
var type = schema._def.typeName;
|
73
|
-
var zodToInputType = new Map([[
|
74
|
+
var zodToInputType = new Map([['ZodObject', 'object'], ['ZodString', 'string'], ['ZodNumber', 'number'], ['ZodBoolean', 'boolean'], ['ZodDate', 'date'], ['ZodEnum', 'select'], ['ZodNativeEnum', 'select'], ['ZodArray', 'array']]);
|
74
75
|
var match = zodToInputType.get(type);
|
75
|
-
return match ||
|
76
|
+
return match || 'string'; // Default to string for unknown types
|
76
77
|
}
|
77
78
|
|
78
79
|
var _excluded$1 = ["fieldType"];
|
@@ -105,7 +106,7 @@ function parseField(key, schema) {
|
|
105
106
|
});
|
106
107
|
}
|
107
108
|
if (baseSchema instanceof zod.z.ZodArray) {
|
108
|
-
subSchema = [parseField(
|
109
|
+
subSchema = [parseField('0', baseSchema._def.type)];
|
109
110
|
}
|
110
111
|
var resp = {
|
111
112
|
key: key,
|
@@ -120,10 +121,10 @@ function parseField(key, schema) {
|
|
120
121
|
return resp;
|
121
122
|
}
|
122
123
|
function getBaseSchema(schema) {
|
123
|
-
if (
|
124
|
+
if ('innerType' in schema._def) {
|
124
125
|
return getBaseSchema(schema._def.innerType);
|
125
126
|
}
|
126
|
-
if (
|
127
|
+
if ('schema' in schema._def) {
|
127
128
|
return getBaseSchema(schema._def.schema);
|
128
129
|
}
|
129
130
|
return schema;
|
@@ -142,7 +143,7 @@ var parseSchema = function parseSchema(S) {
|
|
142
143
|
};
|
143
144
|
|
144
145
|
var useSchema = function useSchema() {
|
145
|
-
return
|
146
|
+
return useContextSelector.useContext(ZodResolverContext);
|
146
147
|
};
|
147
148
|
var useFields = function useFields(key) {
|
148
149
|
var S = useSchema();
|
@@ -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 \"react\";\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 \"react\";\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","\"use client\";\nimport { 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,mBAAa,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,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,WA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AAM,CAACJ,MAAM,CAACY,KAAK,CAAC9C,MAAO,CAAC,CAAA;AAE3C,EAAA,IAAMmE,EAAE,GAAGC,WAAK,EAAE,CAAA;EAElB,OACIC;cACKH,MAAM,CAAC1B,GAAG,CAAC,UAAC8B,KAAK,EAAI;MAClB,IAAMC,OAAO,GAAGP,YAAY,CAACxC,GAAG,CAAC8C,KAAK,CAAClD,IAAI,CAAC,CAAA;AAE5C,MAAA,IAAQM,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,mBAAa,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;;ICzBaQ,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,cAAA,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 { 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,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,kBAAk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t=require("zod");function
|
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;
|
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 \"react\";\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 \"react\";\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","\"use client\";\nimport { 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":"oeAGO,IAAMA,EAAqBC,EAAaA,cAAmB,ICA5D,SAAUC,EAA0BC,GACtC,OAAIA,aAAkBC,EAACA,EAACC,WACbF,EAAOG,KAAKC,eAGnBJ,aAAkBC,EAACA,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C,OAEnC,SAACC,GACG,IAAM3D,EAA0C,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 { 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,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,WAAYI,EAAYP,KACjBM,EAA0BC,EAAYP,KAAaH,QA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EmD,EAAY,SAAiChC,GACtD,IFoDwBiC,EAClBC,EEnDNC,GFmDMD,GADkBD,EEpDdH,eFqDwB7C,EAACA,EAACI,WAAa4C,EAAE3C,YAAc2C,EAO1D,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,uCCNkC,SAAHwB,GAAgE,IAA3Be,EAAIf,EAAJe,KAC1DC,EAAeC,EAAAA,kBACfC,EAAQV,EAAUO,GAElBI,EAASxB,OAAON,OAAO6B,EAAM1D,QAE7B4D,EAAKC,EAAAA,QAEX,OACIC,EAAAA,yBACKH,EAAO1B,KAAI,SAAC8B,GACT,IAAMC,EAAUR,EAAa9B,IAAIqC,EAAMvC,MAE/BR,EAAiB+C,EAAjB/C,IAAQiD,EAAI3C,EAAKyC,EAAKxC,GAE9B,OAAKyC,EAEEE,EAAaA,cAACF,EAAOG,KAAOF,EAAI,CAAEjD,IAAQA,EAAO4C,IAAAA,KAFnC,SAMrC,8BC1B0F,SAA1DpB,GAI5B,OAAOsB,EAAAM,IAACvE,EAAmBwE,SAAQ,CAACnC,MAF9BM,EAANxC,OAEiDsE,SAHzC9B,EAAR8B,UAIJ,qCLHI,WADmC,OAEnC,SAACC,GACG,IAAM3D,EAA0C,aAOhD,OAFAA,EAAmBL,GAAuBgE,EAEnC3D,EACV"}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
2
2
|
import { useInputMapping } from '@form-instant/react-input-mapping';
|
3
|
-
import {
|
3
|
+
import { useId, createElement } from 'react';
|
4
|
+
import { createContext, useContext } from 'use-context-selector';
|
4
5
|
import { z } from 'zod';
|
5
6
|
|
6
7
|
function _extends() {
|
@@ -34,7 +35,7 @@ function getDefaultValueInZodStack(schema) {
|
|
34
35
|
return undefined;
|
35
36
|
}
|
36
37
|
|
37
|
-
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol(
|
38
|
+
var FIELD_CONFIG_SYMBOL = /*#__PURE__*/Symbol('GetFieldConfig');
|
38
39
|
var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
39
40
|
return function (config) {
|
40
41
|
var refinementFunction = function refinementFunction() {
|
@@ -47,17 +48,17 @@ var createZodSchemaFieldConfig = function createZodSchemaFieldConfig() {
|
|
47
48
|
};
|
48
49
|
function getFieldConfigInZodStack(schema) {
|
49
50
|
var typedSchema = schema;
|
50
|
-
if (typedSchema._def.typeName ===
|
51
|
+
if (typedSchema._def.typeName === 'ZodEffects') {
|
51
52
|
var effect = typedSchema._def.effect;
|
52
53
|
var refinementFunction = effect.refinement;
|
53
54
|
if (FIELD_CONFIG_SYMBOL in refinementFunction) {
|
54
55
|
return refinementFunction[FIELD_CONFIG_SYMBOL];
|
55
56
|
}
|
56
57
|
}
|
57
|
-
if (
|
58
|
+
if ('innerType' in typedSchema._def) {
|
58
59
|
return getFieldConfigInZodStack(typedSchema._def.innerType);
|
59
60
|
}
|
60
|
-
if (
|
61
|
+
if ('schema' in typedSchema._def) {
|
61
62
|
return getFieldConfigInZodStack(typedSchema._def.schema);
|
62
63
|
}
|
63
64
|
return {};
|
@@ -68,9 +69,9 @@ function inferFieldType(schema, fieldType) {
|
|
68
69
|
return fieldType;
|
69
70
|
}
|
70
71
|
var type = schema._def.typeName;
|
71
|
-
var zodToInputType = new Map([[
|
72
|
+
var zodToInputType = new Map([['ZodObject', 'object'], ['ZodString', 'string'], ['ZodNumber', 'number'], ['ZodBoolean', 'boolean'], ['ZodDate', 'date'], ['ZodEnum', 'select'], ['ZodNativeEnum', 'select'], ['ZodArray', 'array']]);
|
72
73
|
var match = zodToInputType.get(type);
|
73
|
-
return match ||
|
74
|
+
return match || 'string'; // Default to string for unknown types
|
74
75
|
}
|
75
76
|
|
76
77
|
var _excluded$1 = ["fieldType"];
|
@@ -103,7 +104,7 @@ function parseField(key, schema) {
|
|
103
104
|
});
|
104
105
|
}
|
105
106
|
if (baseSchema instanceof z.ZodArray) {
|
106
|
-
subSchema = [parseField(
|
107
|
+
subSchema = [parseField('0', baseSchema._def.type)];
|
107
108
|
}
|
108
109
|
var resp = {
|
109
110
|
key: key,
|
@@ -118,10 +119,10 @@ function parseField(key, schema) {
|
|
118
119
|
return resp;
|
119
120
|
}
|
120
121
|
function getBaseSchema(schema) {
|
121
|
-
if (
|
122
|
+
if ('innerType' in schema._def) {
|
122
123
|
return getBaseSchema(schema._def.innerType);
|
123
124
|
}
|
124
|
-
if (
|
125
|
+
if ('schema' in schema._def) {
|
125
126
|
return getBaseSchema(schema._def.schema);
|
126
127
|
}
|
127
128
|
return schema;
|
@@ -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 \"react\";\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 \"react\";\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","\"use client\";\nimport { 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,C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AAC,CAAA;AAE5C,MAAA,IAAQM,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;;ICzBaQ,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 { 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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,MAAM,CAAC1B,GAAG,CAAC,UAAC8B,KAAK,EAAI;MAClB,IAAMC,OAAO,GAAGP,YAAY,CAACxC,GAAG,CAAC8C,KAAK,CAAClD,IAAI,CAAC,CAAA;AAE5C,MAAA,IAAQM,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;;;;"}
|
package/dist/type.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import { ZodDiscriminatedUnion, ZodEffects, ZodObject } from
|
1
|
+
import { ZodDiscriminatedUnion, ZodEffects, ZodObject } from 'zod';
|
2
2
|
export type zodResolverProps = ZodObject<any, any> | ZodEffects<any, any> | ZodDiscriminatedUnion<any, any>;
|
package/dist/useSchema.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import { ZodEffects, ZodObject } from
|
1
|
+
import { ZodEffects, ZodObject } from 'zod';
|
2
2
|
export declare const useSchema: () => ZodObject<never, never, import("zod").ZodTypeAny, never, never> | ZodEffects<never, 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.
|
3
|
+
"version": "0.1.5",
|
4
4
|
"license": "MIT",
|
5
5
|
"author": {
|
6
6
|
"name": "leomerida15",
|
@@ -60,10 +60,10 @@
|
|
60
60
|
"@size-limit/preset-small-lib": "^11.1.6",
|
61
61
|
"@tsconfig/recommended": "^1.0.7",
|
62
62
|
"@tsconfig/vite-react": "^3.0.2",
|
63
|
-
"@types/bun": "^1.1.
|
64
|
-
"@types/react": "^18.3.
|
65
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
66
|
-
"@typescript-eslint/parser": "^8.
|
63
|
+
"@types/bun": "^1.1.12",
|
64
|
+
"@types/react": "^18.3.12",
|
65
|
+
"@typescript-eslint/eslint-plugin": "^8.11.0",
|
66
|
+
"@typescript-eslint/parser": "^8.11.0",
|
67
67
|
"dts-cli": "^2.0.5",
|
68
68
|
"eslint": "^9.13.0",
|
69
69
|
"eslint-plugin-prettier": "^5.2.1",
|
@@ -74,5 +74,15 @@
|
|
74
74
|
"tslib": "^2.8.0",
|
75
75
|
"typescript": "~5.0.4",
|
76
76
|
"zod": "^3.23.8"
|
77
|
-
}
|
77
|
+
},
|
78
|
+
"homepage": "https://leomerida15.github.io/form-instant-react-mapping",
|
79
|
+
"repository": {
|
80
|
+
"type": "git",
|
81
|
+
"url": "git+https://github.com/leomerida15/form-instant-react-mapping.git"
|
82
|
+
},
|
83
|
+
"keywords": [
|
84
|
+
"@form-instant",
|
85
|
+
"react",
|
86
|
+
"mapping"
|
87
|
+
]
|
78
88
|
}
|
package/src/context.tsx
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { createContext } from
|
2
|
-
import { zodResolverProps } from
|
1
|
+
import { createContext } from 'use-context-selector';
|
2
|
+
import { zodResolverProps } from './type';
|
3
3
|
|
4
4
|
export const ZodResolverContext = createContext<zodResolverProps>({} as zodResolverProps);
|
package/src/element.tsx
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { useInputMapping } from
|
2
|
-
import { createElement, useId } from
|
3
|
-
import { useFields } from
|
1
|
+
import { useInputMapping } from '@form-instant/react-input-mapping';
|
2
|
+
import { createElement, useId } from 'react';
|
3
|
+
import { useFields } from './useSchema';
|
4
4
|
|
5
5
|
export interface ElementProps<Schema extends Record<string, any>> {
|
6
6
|
name: keyof Schema;
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { RefinementEffect, z } from
|
2
|
-
import { FieldConfig, SuperRefineFunction } from
|
3
|
-
export const FIELD_CONFIG_SYMBOL = Symbol(
|
1
|
+
import { RefinementEffect, z } from 'zod';
|
2
|
+
import { FieldConfig, SuperRefineFunction } from './types';
|
3
|
+
export const FIELD_CONFIG_SYMBOL = Symbol('GetFieldConfig');
|
4
4
|
|
5
5
|
export const createZodSchemaFieldConfig =
|
6
6
|
<AdditionalRenderable = null, FieldTypes = string>() =>
|
@@ -18,7 +18,7 @@ export const createZodSchemaFieldConfig =
|
|
18
18
|
export function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {
|
19
19
|
const typedSchema = schema as unknown as z.ZodEffects<z.ZodNumber | z.ZodString>;
|
20
20
|
|
21
|
-
if (typedSchema._def.typeName ===
|
21
|
+
if (typedSchema._def.typeName === 'ZodEffects') {
|
22
22
|
const effect = typedSchema._def.effect as RefinementEffect<any>;
|
23
23
|
const refinementFunction = effect.refinement;
|
24
24
|
|
@@ -27,10 +27,10 @@ export function getFieldConfigInZodStack(schema: z.ZodTypeAny): FieldConfig {
|
|
27
27
|
}
|
28
28
|
}
|
29
29
|
|
30
|
-
if (
|
30
|
+
if ('innerType' in typedSchema._def) {
|
31
31
|
return getFieldConfigInZodStack(typedSchema._def.innerType as unknown as z.ZodAny);
|
32
32
|
}
|
33
|
-
if (
|
33
|
+
if ('schema' in typedSchema._def) {
|
34
34
|
return getFieldConfigInZodStack((typedSchema._def as any).schema as z.ZodAny);
|
35
35
|
}
|
36
36
|
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { z } from
|
2
|
-
import { FieldConfig } from
|
1
|
+
import { z } from 'zod';
|
2
|
+
import { FieldConfig } from './types';
|
3
3
|
|
4
|
-
export function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig[
|
4
|
+
export function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig['fieldType']): string {
|
5
5
|
if (fieldType) {
|
6
6
|
return fieldType;
|
7
7
|
}
|
@@ -9,17 +9,17 @@ export function inferFieldType(schema: z.ZodTypeAny, fieldType?: FieldConfig["fi
|
|
9
9
|
const type = schema._def.typeName;
|
10
10
|
|
11
11
|
const zodToInputType = new Map([
|
12
|
-
[
|
13
|
-
[
|
14
|
-
[
|
15
|
-
[
|
16
|
-
[
|
17
|
-
[
|
18
|
-
[
|
19
|
-
[
|
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
20
|
]);
|
21
21
|
|
22
22
|
const match = zodToInputType.get(type);
|
23
23
|
|
24
|
-
return match ||
|
24
|
+
return match || 'string'; // Default to string for unknown types
|
25
25
|
}
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import { z } from
|
2
|
-
import { getDefaultValueInZodStack } from
|
3
|
-
import { getFieldConfigInZodStack } from
|
4
|
-
import { inferFieldType } from
|
5
|
-
import { ParsedField, ParsedSchema, ZodObjectOrWrapped } from
|
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
6
|
|
7
7
|
function parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {
|
8
8
|
const baseSchema = getBaseSchema(schema);
|
@@ -29,7 +29,7 @@ function parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {
|
|
29
29
|
);
|
30
30
|
}
|
31
31
|
if (baseSchema instanceof z.ZodArray) {
|
32
|
-
subSchema = [parseField(
|
32
|
+
subSchema = [parseField('0', baseSchema._def.type)];
|
33
33
|
}
|
34
34
|
|
35
35
|
const resp = {
|
@@ -49,10 +49,10 @@ function parseField(key: string, schema: z.ZodTypeAny): ParsedField<any> {
|
|
49
49
|
function getBaseSchema<ChildType extends z.ZodAny | z.ZodTypeAny | z.AnyZodObject = z.ZodAny>(
|
50
50
|
schema: ChildType | z.ZodEffects<ChildType>,
|
51
51
|
): ChildType {
|
52
|
-
if (
|
52
|
+
if ('innerType' in schema._def) {
|
53
53
|
return getBaseSchema(schema._def.innerType as ChildType);
|
54
54
|
}
|
55
|
-
if (
|
55
|
+
if ('schema' in schema._def) {
|
56
56
|
return getBaseSchema(schema._def.schema as ChildType);
|
57
57
|
}
|
58
58
|
|
package/src/funcs/types.ts
CHANGED
package/src/global.d.ts
CHANGED
package/src/index.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
export * from
|
2
|
-
export { createZodSchemaFieldConfig } from
|
3
|
-
export * from
|
4
|
-
export * from
|
5
|
-
export * from
|
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
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import { zodResolverProps } from "./type";
|
1
|
+
import { FC, ReactNode } from 'react';
|
2
|
+
import { ZodResolverContext } from './context';
|
3
|
+
import { zodResolverProps } from './type';
|
5
4
|
export const FormInstantProvider: FC<{ schema: zodResolverProps; children: ReactNode }> = ({
|
6
5
|
children,
|
7
6
|
schema,
|
package/src/type.ts
CHANGED
package/src/useSchema.tsx
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import { useContext } from
|
2
|
-
import { ZodEffects, ZodObject } from
|
3
|
-
import { ZodResolverContext } from
|
4
|
-
import { parseSchema } from
|
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
5
|
|
6
6
|
export const useSchema = () =>
|
7
7
|
useContext(ZodResolverContext) as ZodObject<never, never> | ZodEffects<never, never>;
|