@regle/schemas 1.12.2-beta.1 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,8 +30,8 @@ type RegleSingleFieldSchema<TState extends Maybe<PrimitiveTypes>, TSchema extend
30
30
  type RegleSchemaResult<TSchema extends unknown> = {
31
31
  valid: false;
32
32
  data: PartialDeep<TSchema>;
33
- issues: RegleIssuesTree<TSchema>;
34
- errors: RegleErrorTree<TSchema>;
33
+ issues: RegleIssuesTree<TSchema, true>;
34
+ errors: RegleErrorTree<TSchema, false, true>;
35
35
  } | {
36
36
  valid: true;
37
37
  data: TSchema;
@@ -48,13 +48,13 @@ type RegleSchemaStatus<TState extends Record<string, any> | undefined = Record<s
48
48
  /** Collection of all issues, collected for all children properties and nested forms.
49
49
  *
50
50
  * Only contains errors from properties where $dirty equals true. */
51
- readonly $issues: RegleIssuesTree<TState>;
51
+ readonly $issues: RegleIssuesTree<TState, true>;
52
52
  /** Collection of all the error messages, collected for all children properties and nested forms.
53
53
  *
54
54
  * Only contains errors from properties where $dirty equals true. */
55
- readonly $errors: RegleErrorTree<TState>;
55
+ readonly $errors: RegleErrorTree<TState, false, true>;
56
56
  /** Collection of all the error messages, collected for all children properties. */
57
- readonly $silentErrors: RegleErrorTree<TState>;
57
+ readonly $silentErrors: RegleErrorTree<TState, false, true>;
58
58
  /** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
59
59
  $extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
60
60
  } & ProcessNestedFields<TState, TShortcuts> & (IsRoot extends true ? {
@@ -64,7 +64,7 @@ type RegleSchemaStatus<TState extends Record<string, any> | undefined = Record<s
64
64
  /**
65
65
  * @public
66
66
  */
67
- type InferRegleSchemaStatusType<TState extends unknown, TShortcuts extends RegleShortcutDefinition = {}> = NonNullable<TState> extends Array<infer A> ? A extends Record<string, any> ? RegleSchemaCollectionStatus<NonNullable<TState>, TShortcuts> : RegleSchemaFieldStatus<TState, TShortcuts> : NonNullable<TState> extends Date | File ? RegleSchemaFieldStatus<TState, TShortcuts> : unknown extends TState ? RegleSchemaFieldStatus<TState extends EmptyObject ? unknown : TState, TShortcuts> : NonNullable<TState> extends Record<string, any> ? NonNullable<NonNullable<TState>> extends RegleStaticImpl<infer U> ? RegleSchemaFieldStatus<Raw<U>, TShortcuts> : MaybeSchemaVariantStatus<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, StandardSchemaV1, TShortcuts> : RegleSchemaFieldStatus<TState, TShortcuts>;
67
+ type InferRegleSchemaStatusType<TState extends unknown, TShortcuts extends RegleShortcutDefinition = {}> = NonNullable<TState> extends Array<any> ? RegleSchemaCollectionStatus<NonNullable<TState>, TShortcuts> : NonNullable<TState> extends Date | File ? RegleSchemaFieldStatus<TState, TShortcuts> : unknown extends TState ? RegleSchemaFieldStatus<TState extends EmptyObject ? unknown : TState, TShortcuts> : NonNullable<TState> extends Record<string, any> ? NonNullable<NonNullable<TState>> extends RegleStaticImpl<infer U> ? RegleSchemaFieldStatus<Raw<U>, TShortcuts> : MaybeSchemaVariantStatus<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, StandardSchemaV1, TShortcuts> : RegleSchemaFieldStatus<TState, TShortcuts>;
68
68
  /**
69
69
  * @public
70
70
  */
@@ -82,7 +82,10 @@ function setObjectError(obj, propsArg, value, isArray) {
82
82
  }
83
83
  if (isArray) if (!obj[lastProp]) obj[lastProp] = { $self: value };
84
84
  else obj[lastProp].$self = (obj[lastProp].$self ??= []).concat(value);
85
- else if (Array.isArray(obj[lastProp])) obj[lastProp] = obj[lastProp].concat(value);
85
+ else if (!isNaN(parseInt(lastProp))) {
86
+ if (obj.$each == void 0) obj.$each = [];
87
+ obj.$each[lastProp] = (obj.$each[lastProp] ??= []).concat(value);
88
+ } else if (Array.isArray(obj[lastProp])) obj[lastProp] = obj[lastProp].concat(value);
86
89
  else obj[lastProp] = value;
87
90
  return true;
88
91
  }
@@ -149,10 +152,8 @@ function createUseRegleSchemaComposable(options, shortcuts) {
149
152
  let $path = getIssuePath(issue);
150
153
  const lastItem = issue.path?.[issue.path.length - 1];
151
154
  const lastItemKey = typeof lastItem === "object" ? lastItem.key : lastItem;
152
- const isArray = (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false) || Array.isArray(getDotPath(processedState.value, $path));
153
155
  return {
154
- isArray,
155
- isPrimitivesArray: !isArray && typeof lastItemKey === "number",
156
+ isArray: (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false) || Array.isArray(getDotPath(processedState.value, $path)),
156
157
  $path,
157
158
  lastItemKey,
158
159
  lastItem
@@ -195,8 +196,6 @@ function createUseRegleSchemaComposable(options, shortcuts) {
195
196
  function issuesToRegleErrors(result, isValidate = false) {
196
197
  const output = {};
197
198
  const mappedIssues = result.issues?.map((issue) => {
198
- const { isPrimitivesArray } = getPropertiesFromIssue(issue);
199
- if (isPrimitivesArray) return issue;
200
199
  const parentArrayPath = getParentArrayPath(issue);
201
200
  if (parentArrayPath) {
202
201
  const $currentArrayValue = getDotPath(processedState.value, getIssuePath(parentArrayPath));
@@ -212,8 +211,7 @@ function createUseRegleSchemaComposable(options, shortcuts) {
212
211
  const filteredIssues = filterIssues(mappedIssues ?? [], isValidate);
213
212
  if (mappedIssues?.length) {
214
213
  const issues = filteredIssues.map((issue) => {
215
- let { isArray, isPrimitivesArray, $path, lastItemKey } = getPropertiesFromIssue(issue);
216
- if (isPrimitivesArray) $path = issue.path?.slice(0, issue.path.length - 1)?.map((item) => typeof item === "object" ? item.key : item.toString()).join(".") ?? "";
214
+ let { isArray, $path, lastItemKey } = getPropertiesFromIssue(issue);
217
215
  return {
218
216
  ...issue,
219
217
  $path,
@@ -1 +1 @@
1
- import{createScopedUseRegle as e,useRootStorage as t}from"@regle/core";import{computed as n,getCurrentScope as r,isRef as i,onScopeDispose as a,ref as o,unref as s,watch as c}from"vue";function l(e){return e?.constructor?.name==`File`||e?.constructor?.name==`FileList`}function u(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):l(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function d(e){if(typeof e.source.flags==`string`)return e.source.flags;{let t=[];return e.global&&t.push(`g`),e.ignoreCase&&t.push(`i`),e.multiline&&t.push(`m`),e.sticky&&t.push(`y`),e.unicode&&t.push(`u`),t.join(``)}}function f(e,t=0){if(t>20)return e;let n=e,r={}.toString.call(e).slice(8,-1);if(r==`Set`&&(n=new Set([...e].map(e=>f(e,t++)))),r==`Map`&&(n=new Map([...e].map(e=>[f(e[0]),f(e[1])]))),r==`Date`&&(n=new Date(e.getTime())),r==`RegExp`&&(n=RegExp(e.source,d(e))),r==`Array`||r==`Object`)for(let r in n=Array.isArray(e)?[]:{},e)n[r]=f(e[r],t++);return n}function p(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e,t,n,r){var i,a;if(Array.isArray(t)&&(i=t.slice(0)),typeof t==`string`&&(i=t.split(`.`)),typeof t==`symbol`&&(i=[t]),!Array.isArray(i))throw Error(`props arg must be an array, a string or a symbol`);if(a=i.pop(),!a)return!1;g(a);for(var o;o=i.shift();)if(g(o),isNaN(parseInt(o))?(e[o]===void 0&&(e[o]={}),e=e[o]):(e.$each??=[],u(e.$each[o])&&(e.$each[o]={}),e=e.$each[o]),!e||typeof e!=`object`)return!1;return r?e[a]?e[a].$self=(e[a].$self??=[]).concat(n):e[a]={$self:n}:Array.isArray(e[a])?e[a]=e[a].concat(n):e[a]=n,!0}function h(e,t,n){if(!e)return n;var r,i;if(Array.isArray(t)&&(r=t.slice(0)),typeof t==`string`&&(r=t.split(`.`)),typeof t==`symbol`&&(r=[t]),!Array.isArray(r))throw Error(`props arg must be an array, a string or a symbol`);for(;r.length;)if(i=r.shift(),!e||!i||(e=e[i],e===void 0))return n;return e}function g(e){if(e==`__proto__`||e==`constructor`||e==`prototype`)throw Error(`setting of prototype values not supported`)}function _(e,...t){for(var n=[].slice.call(arguments),r,i=n.length;r=n[i-1],i--;)if(!r||typeof r!=`object`&&typeof r!=`function`)throw Error(`expected object, got `+r);for(var a=n[0],o=n.slice(1),s=o.length,i=0;i<s;i++){var c=o[i];for(var l in c)a[l]=c[l]}return a}function v(e,l){let u={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function d(e,d,g){let v=n(()=>s(d)),{syncState:y={onUpdate:!1,onValidate:!1},...b}=g??{},{onUpdate:x=!1,onValidate:S=!1}=y,C={...u,...b},w=n(()=>!p(T.value)),T=i(e)?e:o(e),E=o(p(T.value)?{...f(T.value)}:f(T.value)),D=p(T.value)?{...f(T.value)}:f(T.value),O=o({}),k=o([]),A;function j(e){let t=M(e),n=e.path?.[e.path.length-1],r=typeof n==`object`?n.key:n,i=(typeof n==`object`&&`value`in n?Array.isArray(n.value):!1)||(`type`in e?e.type===`array`:!1)||Array.isArray(h(T.value,t));return{isArray:i,isPrimitivesArray:!i&&typeof r==`number`,$path:t,lastItemKey:r,lastItem:n}}function M(e){return e.path?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``}function N(e){let t=e.path?.at(-1),n=typeof t==`object`?typeof t.key==`string`:typeof t==`string`,r=e.path?.findLastIndex(e=>typeof e==`object`?typeof e.key==`number`:typeof e==`number`);if(!(!n&&r===-1)&&r!=null){let t=e.path?.slice(0,r+1);return{...e,path:t}}}if(!v.value?.[`~standard`])throw Error(`Only "standard-schema" compatible libraries are supported`);function P(e,t=!1){return!t&&C.rewardEarly?k.value.length?k.value.reduce((t,n)=>{if(`$currentArrayValue`in n&&p(n.$currentArrayValue)&&`$id`in n.$currentArrayValue){let r=n.$currentArrayValue.$id,i=e.find(e=>e?.$currentArrayValue?.$id===r);i&&t.push({...n,path:i?.path??[]})}else e.some(e=>M(e)===M(n))&&t.push(n);return t},[]):[]:e}function F(e,t=!1){let n={},r=e.issues?.map(e=>{let{isPrimitivesArray:t}=j(e);if(t)return e;let n=N(e);if(n){let t=h(T.value,M(n));Object.defineProperty(e,`$currentArrayValue`,{value:t,enumerable:!0,configurable:!0,writable:!0})}return e}),i=P(r??[],t);if(r?.length){let e=i.map(e=>{let{isArray:t,isPrimitivesArray:n,$path:r,lastItemKey:i}=j(e);return n&&(r=e.path?.slice(0,e.path.length-1)?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``),{...e,$path:r,isArray:t,$property:i,$rule:`schema`,$message:e.message}});e.forEach(({isArray:e,$path:t,...r})=>{m(n,t,[r],e)}),k.value=e}else k.value=[];return n}async function I(e=!1){let t=v.value[`~standard`].validate(T.value);return t instanceof Promise&&(t=await t),w.value?O.value=P(t.issues??[],e)?.map(e=>({$message:e.message,$property:e.path?.[e.path.length-1]?.toString()??`-`,$rule:`schema`,...e}))??[]:O.value=F(t,e),t.issues||(e&&S||!e&&x)&&(L?.(),p(T.value)?T.value=_(T.value,t.value):T.value=t.value,R()),t}let L;function R(){L=c([T,v],()=>{C.silent||I()},{deep:!0})}R(),I(),A=async()=>{try{let e=await I(!0);return z?.regle?.$touch(),{valid:!e.issues?.length,data:T.value,errors:z?.regle?.$errors,issues:O.value}}catch(e){return Promise.reject(e)}},r()&&a(()=>{L()});let z=t({scopeRules:n(()=>({})),state:T,options:C,schemaErrors:O,initialState:E,originalState:D,shortcuts:l,schemaMode:!0,onValidate:A});return{r$:z.regle}}return d}const y=v();function b(e,t){return e}function x(){function e(e,t){return t}return e}const S=x();function C({modifiers:e,shortcuts:t}){return{useRegleSchema:v(e,t),inferSchema:x()}}const{useCollectScope:w,useScopedRegle:T}=e({customUseRegle:y}),E=t=>{let{customStore:n,customUseRegle:r=y,asRecord:i=!1}=t??{};return e({customStore:n,customUseRegle:r,asRecord:i})};export{E as createScopedUseRegleSchema,C as defineRegleSchemaConfig,S as inferSchema,w as useCollectSchemaScope,y as useRegleSchema,T as useScopedRegleSchema,b as withDeps};
1
+ import{createScopedUseRegle as e,useRootStorage as t}from"@regle/core";import{computed as n,getCurrentScope as r,isRef as i,onScopeDispose as a,ref as o,unref as s,watch as c}from"vue";function l(e){return e?.constructor?.name==`File`||e?.constructor?.name==`FileList`}function u(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):l(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function d(e){if(typeof e.source.flags==`string`)return e.source.flags;{let t=[];return e.global&&t.push(`g`),e.ignoreCase&&t.push(`i`),e.multiline&&t.push(`m`),e.sticky&&t.push(`y`),e.unicode&&t.push(`u`),t.join(``)}}function f(e,t=0){if(t>20)return e;let n=e,r={}.toString.call(e).slice(8,-1);if(r==`Set`&&(n=new Set([...e].map(e=>f(e,t++)))),r==`Map`&&(n=new Map([...e].map(e=>[f(e[0]),f(e[1])]))),r==`Date`&&(n=new Date(e.getTime())),r==`RegExp`&&(n=RegExp(e.source,d(e))),r==`Array`||r==`Object`)for(let r in n=Array.isArray(e)?[]:{},e)n[r]=f(e[r],t++);return n}function p(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e,t,n,r){var i,a;if(Array.isArray(t)&&(i=t.slice(0)),typeof t==`string`&&(i=t.split(`.`)),typeof t==`symbol`&&(i=[t]),!Array.isArray(i))throw Error(`props arg must be an array, a string or a symbol`);if(a=i.pop(),!a)return!1;g(a);for(var o;o=i.shift();)if(g(o),isNaN(parseInt(o))?(e[o]===void 0&&(e[o]={}),e=e[o]):(e.$each??=[],u(e.$each[o])&&(e.$each[o]={}),e=e.$each[o]),!e||typeof e!=`object`)return!1;return r?e[a]?e[a].$self=(e[a].$self??=[]).concat(n):e[a]={$self:n}:isNaN(parseInt(a))?Array.isArray(e[a])?e[a]=e[a].concat(n):e[a]=n:(e.$each??=[],e.$each[a]=(e.$each[a]??=[]).concat(n)),!0}function h(e,t,n){if(!e)return n;var r,i;if(Array.isArray(t)&&(r=t.slice(0)),typeof t==`string`&&(r=t.split(`.`)),typeof t==`symbol`&&(r=[t]),!Array.isArray(r))throw Error(`props arg must be an array, a string or a symbol`);for(;r.length;)if(i=r.shift(),!e||!i||(e=e[i],e===void 0))return n;return e}function g(e){if(e==`__proto__`||e==`constructor`||e==`prototype`)throw Error(`setting of prototype values not supported`)}function _(e,...t){for(var n=[].slice.call(arguments),r,i=n.length;r=n[i-1],i--;)if(!r||typeof r!=`object`&&typeof r!=`function`)throw Error(`expected object, got `+r);for(var a=n[0],o=n.slice(1),s=o.length,i=0;i<s;i++){var c=o[i];for(var l in c)a[l]=c[l]}return a}function v(e,l){let u={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function d(e,d,g){let v=n(()=>s(d)),{syncState:y={onUpdate:!1,onValidate:!1},...b}=g??{},{onUpdate:x=!1,onValidate:S=!1}=y,C={...u,...b},w=n(()=>!p(T.value)),T=i(e)?e:o(e),E=o(p(T.value)?{...f(T.value)}:f(T.value)),D=p(T.value)?{...f(T.value)}:f(T.value),O=o({}),k=o([]),A;function j(e){let t=M(e),n=e.path?.[e.path.length-1],r=typeof n==`object`?n.key:n;return{isArray:(typeof n==`object`&&`value`in n?Array.isArray(n.value):!1)||(`type`in e?e.type===`array`:!1)||Array.isArray(h(T.value,t)),$path:t,lastItemKey:r,lastItem:n}}function M(e){return e.path?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``}function N(e){let t=e.path?.at(-1),n=typeof t==`object`?typeof t.key==`string`:typeof t==`string`,r=e.path?.findLastIndex(e=>typeof e==`object`?typeof e.key==`number`:typeof e==`number`);if(!(!n&&r===-1)&&r!=null){let t=e.path?.slice(0,r+1);return{...e,path:t}}}if(!v.value?.[`~standard`])throw Error(`Only "standard-schema" compatible libraries are supported`);function P(e,t=!1){return!t&&C.rewardEarly?k.value.length?k.value.reduce((t,n)=>{if(`$currentArrayValue`in n&&p(n.$currentArrayValue)&&`$id`in n.$currentArrayValue){let r=n.$currentArrayValue.$id,i=e.find(e=>e?.$currentArrayValue?.$id===r);i&&t.push({...n,path:i?.path??[]})}else e.some(e=>M(e)===M(n))&&t.push(n);return t},[]):[]:e}function F(e,t=!1){let n={},r=e.issues?.map(e=>{let t=N(e);if(t){let n=h(T.value,M(t));Object.defineProperty(e,`$currentArrayValue`,{value:n,enumerable:!0,configurable:!0,writable:!0})}return e}),i=P(r??[],t);if(r?.length){let e=i.map(e=>{let{isArray:t,$path:n,lastItemKey:r}=j(e);return{...e,$path:n,isArray:t,$property:r,$rule:`schema`,$message:e.message}});e.forEach(({isArray:e,$path:t,...r})=>{m(n,t,[r],e)}),k.value=e}else k.value=[];return n}async function I(e=!1){let t=v.value[`~standard`].validate(T.value);return t instanceof Promise&&(t=await t),w.value?O.value=P(t.issues??[],e)?.map(e=>({$message:e.message,$property:e.path?.[e.path.length-1]?.toString()??`-`,$rule:`schema`,...e}))??[]:O.value=F(t,e),t.issues||(e&&S||!e&&x)&&(L?.(),p(T.value)?T.value=_(T.value,t.value):T.value=t.value,R()),t}let L;function R(){L=c([T,v],()=>{C.silent||I()},{deep:!0})}R(),I(),A=async()=>{try{let e=await I(!0);return z?.regle?.$touch(),{valid:!e.issues?.length,data:T.value,errors:z?.regle?.$errors,issues:O.value}}catch(e){return Promise.reject(e)}},r()&&a(()=>{L()});let z=t({scopeRules:n(()=>({})),state:T,options:C,schemaErrors:O,initialState:E,originalState:D,shortcuts:l,schemaMode:!0,onValidate:A});return{r$:z.regle}}return d}const y=v();function b(e,t){return e}function x(){function e(e,t){return t}return e}const S=x();function C({modifiers:e,shortcuts:t}){return{useRegleSchema:v(e,t),inferSchema:x()}}const{useCollectScope:w,useScopedRegle:T}=e({customUseRegle:y}),E=t=>{let{customStore:n,customUseRegle:r=y,asRecord:i=!1}=t??{};return e({customStore:n,customUseRegle:r,asRecord:i})};export{E as createScopedUseRegleSchema,C as defineRegleSchemaConfig,S as inferSchema,w as useCollectSchemaScope,y as useRegleSchema,T as useScopedRegleSchema,b as withDeps};
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@regle/schemas",
3
- "version": "1.12.2-beta.1",
3
+ "version": "1.13.0",
4
4
  "description": "Schemas adapter for Regle",
5
5
  "dependencies": {
6
6
  "@standard-schema/spec": "1.0.0",
7
7
  "type-fest": "5.2.0",
8
- "@regle/rules": "1.12.2-beta.1",
9
- "@regle/core": "1.12.2-beta.1"
8
+ "@regle/rules": "1.13.0",
9
+ "@regle/core": "1.13.0"
10
10
  },
11
11
  "peerDependencies": {
12
12
  "valibot": "^1.0.0",