@saas-ui/forms 0.6.0-next.0 → 0.6.0-next.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +6 -0
- package/dist/yup/index.js +2 -0
- package/dist/yup/index.js.map +1 -0
- package/dist/yup/index.modern.js +2 -0
- package/dist/yup/index.modern.js.map +1 -0
- package/dist/yup/src/index.d.ts +2 -0
- package/dist/yup/src/index.d.ts.map +1 -0
- package/dist/yup/src/yup.d.ts +23 -0
- package/dist/yup/src/yup.d.ts.map +1 -0
- package/package.json +2 -11
- package/yup/index.ts +1 -0
- package/yup/package.json +11 -4
- package/yup/src/index.ts +1 -0
- package/yup/src/yup.ts +100 -0
- package/yup/tsconfig.json +8 -0
package/CHANGELOG.md
CHANGED
@@ -0,0 +1,2 @@
|
|
1
|
+
var e=require("yup"),r=require("@hookform/resolvers/yup");function t(){return t=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},t.apply(this,arguments)}function n(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}var a=function(e){var r;if(null!=(r=e.spec.meta)&&r.type)return e.spec.meta.type;switch(e.type){case"array":return"array";case"object":return"object";case"number":return"number";case"date":return"date";default:return"text"}},o=function(e,r){for(var t,a=function(e,r){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return n(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,r):void 0}}(e))){t&&(e=t);var a=0;return function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e.tests);!(t=a()).done;){var o,i=t.value;if(null!=(o=i.OPTIONS)&&o.params[r])return i.OPTIONS.params[r]}},i=function(e){var r,n=[];for(var i in r="array"===e.type?e.innerType.fields:e.fields){var u=r[i],s={};"array"===u.type&&(s.min=o(u,"min"),s.max=o(u,"max")),n.push(t({name:i,label:u.spec.label||i,type:a(u)},s))}return n},u=function(r,t){return e.reach(r,t)},s=function(e){return{getFields:function(){return i(e)},getNestedFields:function(r){return i(u(e,r))}}};exports.fieldResolver=s,exports.getFieldsFromSchema=i,exports.getNestedSchema=u,exports.yupForm=function(e,t,n){return void 0===t&&(t={}),void 0===n&&(n={}),{schema:e,resolver:r.yupResolver(e,t,n),fieldResolver:s(e)}};
|
2
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../yup/src/yup.ts"],"sourcesContent":["import { reach, AnyObjectSchema } from 'yup'\nimport { yupResolver } from '@hookform/resolvers/yup'\n\nimport { FieldProps } from '../../src/field'\n\n// @TODO get proper typings for the schema fields\n\nconst getType = (field: any) => {\n if (field.spec.meta?.type) {\n return field.spec.meta.type\n }\n\n switch (field.type) {\n case 'array':\n return 'array'\n case 'object':\n return 'object'\n case 'number':\n return 'number'\n case 'date':\n return 'date'\n case 'string':\n default:\n return 'text'\n }\n}\n\ntype Options = {\n min?: number\n max?: number\n}\n\nconst getArrayOption = (field: any, name: string) => {\n for (const test of field.tests) {\n if (test.OPTIONS?.params[name]) return test.OPTIONS.params[name]\n }\n}\n\n/**\n * A helper function to render forms automatically based on a Yup schema\n *\n * @param schema The Yup schema\n * @returns {FieldProps[]}\n */\nexport const getFieldsFromSchema = (schema: AnyObjectSchema): FieldProps[] => {\n const fields = []\n\n let schemaFields: Record<string, any> = {}\n if (schema.type === 'array') {\n /* @ts-ignore this is actually valid */\n schemaFields = schema.innerType.fields\n } else {\n schemaFields = schema.fields\n }\n\n for (const name in schemaFields) {\n const field = schemaFields[name]\n\n const options: Options = {}\n if (field.type === 'array') {\n options.min = getArrayOption(field, 'min')\n options.max = getArrayOption(field, 'max')\n }\n\n fields.push({\n name,\n label: field.spec.label || name,\n type: getType(field),\n ...options,\n })\n }\n return fields\n}\n\nexport const getNestedSchema = (schema: AnyObjectSchema, path: string) => {\n return reach(schema, path)\n}\n\nexport const fieldResolver = (schema: AnyObjectSchema) => {\n return {\n getFields() {\n return getFieldsFromSchema(schema)\n },\n getNestedFields(name: string) {\n return getFieldsFromSchema(getNestedSchema(schema, name))\n },\n }\n}\n\nexport const yupForm = (\n schema: AnyObjectSchema,\n schemaOptions = {},\n resolverOptions = {}\n) => {\n return {\n schema,\n resolver: yupResolver(schema, schemaOptions, resolverOptions),\n fieldResolver: fieldResolver(schema),\n }\n}\n"],"names":["getType","field","spec","meta","_field$spec$meta","type","getArrayOption","name","tests","test","OPTIONS","_test$OPTIONS","params","getFieldsFromSchema","schema","schemaFields","fields","innerType","options","min","max","push","label","getNestedSchema","path","reach","fieldResolver","getFields","getNestedFields","schemaOptions","resolverOptions","resolver","yupResolver"],"mappings":"oXAOA,IAAMA,EAAU,SAACC,SACf,YAAIA,EAAMC,KAAKC,OAAXC,EAAiBC,KACnB,OAAOJ,EAAMC,KAAKC,KAAKE,KAGzB,OAAQJ,EAAMI,MACZ,IAAK,QACH,MAAO,QACT,IAAK,SACH,MAAO,SACT,IAAK,SACH,MAAO,SACT,IAAK,OACH,MAAO,OAET,QACE,MAAO,SASPC,EAAiB,SAACL,EAAYM,GAClC,srBAAmBN,EAAMO,sBAAO,OAArBC,UACT,YAAIA,EAAKC,UAALC,EAAcC,OAAOL,GAAO,OAAOE,EAAKC,QAAQE,OAAOL,KAUlDM,EAAsB,SAACC,GAClC,IAEIC,EAFEC,EAAS,GAUf,IAAK,IAAMT,KALTQ,EAFkB,UAAhBD,EAAOT,KAEMS,EAAOG,UAAUD,OAEjBF,EAAOE,OAGS,CAC/B,IAAMf,EAAQc,EAAaR,GAErBW,EAAmB,GACN,UAAfjB,EAAMI,OACRa,EAAQC,IAAMb,EAAeL,EAAO,OACpCiB,EAAQE,IAAMd,EAAeL,EAAO,QAGtCe,EAAOK,QACLd,KAAAA,EACAe,MAAOrB,EAAMC,KAAKoB,OAASf,EAC3BF,KAAML,EAAQC,IACXiB,IAGP,OAAOF,GAGIO,EAAkB,SAACT,EAAyBU,GACvD,OAAOC,QAAMX,EAAQU,IAGVE,EAAgB,SAACZ,GAC5B,MAAO,CACLa,qBACE,OAAOd,EAAoBC,IAE7Bc,yBAAgBrB,GACd,OAAOM,EAAoBU,EAAgBT,EAAQP,uGAKlC,SACrBO,EACAe,EACAC,GAEA,gBAHAD,IAAAA,EAAgB,aAChBC,IAAAA,EAAkB,IAEX,CACLhB,OAAAA,EACAiB,SAAUC,cAAYlB,EAAQe,EAAeC,GAC7CJ,cAAeA,EAAcZ"}
|
@@ -0,0 +1,2 @@
|
|
1
|
+
import{reach as e}from"yup";import{yupResolver as r}from"@hookform/resolvers/yup";function t(){return t=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a])}return e},t.apply(this,arguments)}const a=e=>{var r;if(null!=(r=e.spec.meta)&&r.type)return e.spec.meta.type;switch(e.type){case"array":return"array";case"object":return"object";case"number":return"number";case"date":return"date";default:return"text"}},n=(e,r)=>{for(const a of e.tests){var t;if(null!=(t=a.OPTIONS)&&t.params[r])return a.OPTIONS.params[r]}},s=e=>{const r=[];let s={};s="array"===e.type?e.innerType.fields:e.fields;for(const e in s){const o=s[e],p={};"array"===o.type&&(p.min=n(o,"min"),p.max=n(o,"max")),r.push(t({name:e,label:o.spec.label||e,type:a(o)},p))}return r},o=(r,t)=>e(r,t),p=e=>({getFields:()=>s(e),getNestedFields:r=>s(o(e,r))}),l=(e,t={},a={})=>({schema:e,resolver:r(e,t,a),fieldResolver:p(e)});export{p as fieldResolver,s as getFieldsFromSchema,o as getNestedSchema,l as yupForm};
|
2
|
+
//# sourceMappingURL=index.modern.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.modern.js","sources":["../../yup/src/yup.ts"],"sourcesContent":["import { reach, AnyObjectSchema } from 'yup'\nimport { yupResolver } from '@hookform/resolvers/yup'\n\nimport { FieldProps } from '../../src/field'\n\n// @TODO get proper typings for the schema fields\n\nconst getType = (field: any) => {\n if (field.spec.meta?.type) {\n return field.spec.meta.type\n }\n\n switch (field.type) {\n case 'array':\n return 'array'\n case 'object':\n return 'object'\n case 'number':\n return 'number'\n case 'date':\n return 'date'\n case 'string':\n default:\n return 'text'\n }\n}\n\ntype Options = {\n min?: number\n max?: number\n}\n\nconst getArrayOption = (field: any, name: string) => {\n for (const test of field.tests) {\n if (test.OPTIONS?.params[name]) return test.OPTIONS.params[name]\n }\n}\n\n/**\n * A helper function to render forms automatically based on a Yup schema\n *\n * @param schema The Yup schema\n * @returns {FieldProps[]}\n */\nexport const getFieldsFromSchema = (schema: AnyObjectSchema): FieldProps[] => {\n const fields = []\n\n let schemaFields: Record<string, any> = {}\n if (schema.type === 'array') {\n /* @ts-ignore this is actually valid */\n schemaFields = schema.innerType.fields\n } else {\n schemaFields = schema.fields\n }\n\n for (const name in schemaFields) {\n const field = schemaFields[name]\n\n const options: Options = {}\n if (field.type === 'array') {\n options.min = getArrayOption(field, 'min')\n options.max = getArrayOption(field, 'max')\n }\n\n fields.push({\n name,\n label: field.spec.label || name,\n type: getType(field),\n ...options,\n })\n }\n return fields\n}\n\nexport const getNestedSchema = (schema: AnyObjectSchema, path: string) => {\n return reach(schema, path)\n}\n\nexport const fieldResolver = (schema: AnyObjectSchema) => {\n return {\n getFields() {\n return getFieldsFromSchema(schema)\n },\n getNestedFields(name: string) {\n return getFieldsFromSchema(getNestedSchema(schema, name))\n },\n }\n}\n\nexport const yupForm = (\n schema: AnyObjectSchema,\n schemaOptions = {},\n resolverOptions = {}\n) => {\n return {\n schema,\n resolver: yupResolver(schema, schemaOptions, resolverOptions),\n fieldResolver: fieldResolver(schema),\n }\n}\n"],"names":["getType","field","spec","meta","_field$spec$meta","type","getArrayOption","name","test","tests","OPTIONS","_test$OPTIONS","params","getFieldsFromSchema","schema","fields","schemaFields","innerType","options","min","max","push","label","getNestedSchema","path","reach","fieldResolver","getFields","getNestedFields","yupForm","schemaOptions","resolverOptions","resolver","yupResolver"],"mappings":"kSAOA,MAAMA,EAAWC,UACf,YAAIA,EAAMC,KAAKC,OAAXC,EAAiBC,KACnB,OAAOJ,EAAMC,KAAKC,KAAKE,KAGzB,OAAQJ,EAAMI,MACZ,IAAK,QACH,MAAO,QACT,IAAK,SACH,MAAO,SACT,IAAK,SACH,MAAO,SACT,IAAK,OACH,MAAO,OAET,QACE,MAAO,SASPC,EAAiB,CAACL,EAAYM,KAClC,IAAK,MAAMC,KAAQP,EAAMQ,MAAO,OAC9B,YAAID,EAAKE,UAALC,EAAcC,OAAOL,GAAO,OAAOC,EAAKE,QAAQE,OAAOL,KAUlDM,EAAuBC,IAClC,MAAMC,EAAS,GAEf,IAAIC,EAAoC,GAGtCA,EAFkB,UAAhBF,EAAOT,KAEMS,EAAOG,UAAUF,OAEjBD,EAAOC,OAGxB,IAAK,MAAMR,KAAQS,EAAc,CAC/B,MAAMf,EAAQe,EAAaT,GAErBW,EAAmB,GACN,UAAfjB,EAAMI,OACRa,EAAQC,IAAMb,EAAeL,EAAO,OACpCiB,EAAQE,IAAMd,EAAeL,EAAO,QAGtCc,EAAOM,QACLd,KAAAA,EACAe,MAAOrB,EAAMC,KAAKoB,OAASf,EAC3BF,KAAML,EAAQC,IACXiB,IAGP,OAAOH,GAGIQ,EAAkB,CAACT,EAAyBU,IAChDC,EAAMX,EAAQU,GAGVE,EAAiBZ,IACrB,CACLa,UAAS,IACAd,EAAoBC,GAE7Bc,gBAAgBrB,GACPM,EAAoBU,EAAgBT,EAAQP,MAK5CsB,EAAU,CACrBf,EACAgB,EAAgB,GAChBC,EAAkB,MAEX,CACLjB,OAAAA,EACAkB,SAAUC,EAAYnB,EAAQgB,EAAeC,GAC7CL,cAAeA,EAAcZ"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../yup/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAA"}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { AnyObjectSchema } from 'yup';
|
2
|
+
import { FieldProps } from '../../src/field';
|
3
|
+
/**
|
4
|
+
* A helper function to render forms automatically based on a Yup schema
|
5
|
+
*
|
6
|
+
* @param schema The Yup schema
|
7
|
+
* @returns {FieldProps[]}
|
8
|
+
*/
|
9
|
+
export declare const getFieldsFromSchema: (schema: AnyObjectSchema) => FieldProps[];
|
10
|
+
export declare const getNestedSchema: (schema: AnyObjectSchema, path: string) => any;
|
11
|
+
export declare const fieldResolver: (schema: AnyObjectSchema) => {
|
12
|
+
getFields(): FieldProps<import("react-hook-form").FieldValues, string>[];
|
13
|
+
getNestedFields(name: string): FieldProps<import("react-hook-form").FieldValues, string>[];
|
14
|
+
};
|
15
|
+
export declare const yupForm: (schema: AnyObjectSchema, schemaOptions?: {}, resolverOptions?: {}) => {
|
16
|
+
schema: AnyObjectSchema;
|
17
|
+
resolver: <TFieldValues extends import("react-hook-form").FieldValues, TContext>(values: import("react-hook-form").UnpackNestedValue<TFieldValues>, context: TContext | undefined, options: import("react-hook-form").ResolverOptions<TFieldValues>) => Promise<import("react-hook-form").ResolverResult<TFieldValues>>;
|
18
|
+
fieldResolver: {
|
19
|
+
getFields(): FieldProps<import("react-hook-form").FieldValues, string>[];
|
20
|
+
getNestedFields(name: string): FieldProps<import("react-hook-form").FieldValues, string>[];
|
21
|
+
};
|
22
|
+
};
|
23
|
+
//# sourceMappingURL=yup.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"yup.d.ts","sourceRoot":"","sources":["../../../yup/src/yup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,eAAe,EAAE,MAAM,KAAK,CAAA;AAG5C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAmC5C;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,+BAA8B,UAAU,EA4BvE,CAAA;AAED,eAAO,MAAM,eAAe,kCAAmC,MAAM,QAEpE,CAAA;AAED,eAAO,MAAM,aAAa;;0BAKA,MAAM;CAI/B,CAAA;AAED,eAAO,MAAM,OAAO;;;;;8BANM,MAAM;;CAgB/B,CAAA"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@saas-ui/forms",
|
3
|
-
"version": "0.6.0-next.
|
3
|
+
"version": "0.6.0-next.1",
|
4
4
|
"description": "Theme and components agnostic SaasProvider",
|
5
5
|
"source": "src/index.ts",
|
6
6
|
"exports": {
|
@@ -10,13 +10,6 @@
|
|
10
10
|
},
|
11
11
|
"./src": {
|
12
12
|
"default": "./src/index.ts"
|
13
|
-
},
|
14
|
-
"./yup": {
|
15
|
-
"require": "./yup/dist/index.js",
|
16
|
-
"default": "./yup/dist/index.modern.js"
|
17
|
-
},
|
18
|
-
"./yup/src": {
|
19
|
-
"default": "./yup/src/index.ts"
|
20
13
|
}
|
21
14
|
},
|
22
15
|
"main": "./dist/index.js",
|
@@ -34,9 +27,7 @@
|
|
34
27
|
"files": [
|
35
28
|
"dist",
|
36
29
|
"src",
|
37
|
-
"yup
|
38
|
-
"yup/src",
|
39
|
-
"yup/dist"
|
30
|
+
"yup"
|
40
31
|
],
|
41
32
|
"sideEffects": false,
|
42
33
|
"publishConfig": {
|
package/yup/index.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './src'
|
package/yup/package.json
CHANGED
@@ -3,10 +3,17 @@
|
|
3
3
|
"description": "Saas UI Forms field resolver: yup",
|
4
4
|
"version": "1.0.0",
|
5
5
|
"private": true,
|
6
|
-
"source": "src/index.ts",
|
7
|
-
"
|
8
|
-
|
9
|
-
|
6
|
+
"source": "./src/index.ts",
|
7
|
+
"exports": {
|
8
|
+
".": {
|
9
|
+
"require": "./../dist/yup/index.js",
|
10
|
+
"types": "./../dist/yup/src/index.d.ts",
|
11
|
+
"default": "./../dist/yup/index.modern.js"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"main": "../dist/yup/index.js",
|
15
|
+
"module": "../dist/yup/index.modern.js",
|
16
|
+
"types": "../dist/yup/src/index.d.ts",
|
10
17
|
"author": "Eelco Wiersma <eelco@appulse.nl>",
|
11
18
|
"license": "MIT",
|
12
19
|
"peerDependencies": {
|
package/yup/src/index.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './yup'
|
package/yup/src/yup.ts
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
import { reach, AnyObjectSchema } from 'yup'
|
2
|
+
import { yupResolver } from '@hookform/resolvers/yup'
|
3
|
+
|
4
|
+
import { FieldProps } from '../../src/field'
|
5
|
+
|
6
|
+
// @TODO get proper typings for the schema fields
|
7
|
+
|
8
|
+
const getType = (field: any) => {
|
9
|
+
if (field.spec.meta?.type) {
|
10
|
+
return field.spec.meta.type
|
11
|
+
}
|
12
|
+
|
13
|
+
switch (field.type) {
|
14
|
+
case 'array':
|
15
|
+
return 'array'
|
16
|
+
case 'object':
|
17
|
+
return 'object'
|
18
|
+
case 'number':
|
19
|
+
return 'number'
|
20
|
+
case 'date':
|
21
|
+
return 'date'
|
22
|
+
case 'string':
|
23
|
+
default:
|
24
|
+
return 'text'
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
type Options = {
|
29
|
+
min?: number
|
30
|
+
max?: number
|
31
|
+
}
|
32
|
+
|
33
|
+
const getArrayOption = (field: any, name: string) => {
|
34
|
+
for (const test of field.tests) {
|
35
|
+
if (test.OPTIONS?.params[name]) return test.OPTIONS.params[name]
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* A helper function to render forms automatically based on a Yup schema
|
41
|
+
*
|
42
|
+
* @param schema The Yup schema
|
43
|
+
* @returns {FieldProps[]}
|
44
|
+
*/
|
45
|
+
export const getFieldsFromSchema = (schema: AnyObjectSchema): FieldProps[] => {
|
46
|
+
const fields = []
|
47
|
+
|
48
|
+
let schemaFields: Record<string, any> = {}
|
49
|
+
if (schema.type === 'array') {
|
50
|
+
/* @ts-ignore this is actually valid */
|
51
|
+
schemaFields = schema.innerType.fields
|
52
|
+
} else {
|
53
|
+
schemaFields = schema.fields
|
54
|
+
}
|
55
|
+
|
56
|
+
for (const name in schemaFields) {
|
57
|
+
const field = schemaFields[name]
|
58
|
+
|
59
|
+
const options: Options = {}
|
60
|
+
if (field.type === 'array') {
|
61
|
+
options.min = getArrayOption(field, 'min')
|
62
|
+
options.max = getArrayOption(field, 'max')
|
63
|
+
}
|
64
|
+
|
65
|
+
fields.push({
|
66
|
+
name,
|
67
|
+
label: field.spec.label || name,
|
68
|
+
type: getType(field),
|
69
|
+
...options,
|
70
|
+
})
|
71
|
+
}
|
72
|
+
return fields
|
73
|
+
}
|
74
|
+
|
75
|
+
export const getNestedSchema = (schema: AnyObjectSchema, path: string) => {
|
76
|
+
return reach(schema, path)
|
77
|
+
}
|
78
|
+
|
79
|
+
export const fieldResolver = (schema: AnyObjectSchema) => {
|
80
|
+
return {
|
81
|
+
getFields() {
|
82
|
+
return getFieldsFromSchema(schema)
|
83
|
+
},
|
84
|
+
getNestedFields(name: string) {
|
85
|
+
return getFieldsFromSchema(getNestedSchema(schema, name))
|
86
|
+
},
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
export const yupForm = (
|
91
|
+
schema: AnyObjectSchema,
|
92
|
+
schemaOptions = {},
|
93
|
+
resolverOptions = {}
|
94
|
+
) => {
|
95
|
+
return {
|
96
|
+
schema,
|
97
|
+
resolver: yupResolver(schema, schemaOptions, resolverOptions),
|
98
|
+
fieldResolver: fieldResolver(schema),
|
99
|
+
}
|
100
|
+
}
|