@regle/schemas 1.2.0-beta.5 → 1.2.0-beta.7

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.
@@ -1,7 +1,7 @@
1
1
  import * as _regle_core from '@regle/core';
2
- import { Maybe, PrimitiveTypes, RegleShortcutDefinition, RegleCommonStatus, RegleRuleStatus, JoinDiscriminatedUnions, RegleCollectionErrors, RegleErrorTree, DeepReactiveState, DeepMaybeRef, RegleBehaviourOptions, LocalRegleBehaviourOptions, NoInferLegacy, ScopedInstancesRecordLike, MergedScopedRegles } from '@regle/core';
2
+ import { Maybe, PrimitiveTypes, RegleShortcutDefinition, RegleCommonStatus, RegleRuleStatus, JoinDiscriminatedUnions, RegleCollectionErrors, RegleErrorTree, DeepReactiveState, HaveAnyRequiredProps, DeepMaybeRef, RegleBehaviourOptions, LocalRegleBehaviourOptions, NoInferLegacy, UseScopedRegleOptions, CreateScopedUseRegleOptions, useCollectScopeFn, MergedScopedRegles } from '@regle/core';
3
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
4
- import { Raw, MaybeRef, UnwrapNestedRefs, MaybeRefOrGetter, Ref } from 'vue';
4
+ import { Raw, MaybeRef, UnwrapNestedRefs, MaybeRefOrGetter } from 'vue';
5
5
 
6
6
  /**
7
7
  Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
@@ -843,12 +843,17 @@ type RegleSchemaBehaviourOptions = {
843
843
  };
844
844
  };
845
845
 
846
+ type useRegleSchemaFnOptions<TAdditionalOptions extends Record<string, any>> = Omit<Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>, {}, never>, 'validationGroups' | 'lazy' | 'rewardEarly' | 'silent'> & RegleSchemaBehaviourOptions & TAdditionalOptions;
846
847
  interface useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never, TAdditionalReturnProperties extends Record<string, any> = {}, TAdditionalOptions extends Record<string, any> = {}> {
847
- <TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(state: MaybeRef<PartialDeep<TState, {
848
- recurseIntoArrays: true;
849
- }>> | DeepReactiveState<PartialDeep<TState, {
850
- recurseIntoArrays: true;
851
- }>>, rulesFactory: MaybeRef<TSchema>, options?: Omit<Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>, {}, never>, 'validationGroups' | 'lazy' | 'rewardEarly' | 'silent'> & RegleSchemaBehaviourOptions & TAdditionalOptions): NonNullable<TState> extends PrimitiveTypes ? RegleSingleFieldSchema<NonNullable<TState>, StandardSchemaV1.InferInput<TSchema>, TShortcuts, TAdditionalReturnProperties> : RegleSchema<UnwrapNestedRefs<NonNullable<TState>>, UnwrapNestedRefs<NonNullable<StandardSchemaV1.InferInput<TSchema>>>, TShortcuts, TAdditionalReturnProperties>;
848
+ <TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(...params: [
849
+ state: MaybeRef<PartialDeep<TState, {
850
+ recurseIntoArrays: true;
851
+ }>> | DeepReactiveState<PartialDeep<TState, {
852
+ recurseIntoArrays: true;
853
+ }>>,
854
+ rulesFactory: MaybeRef<TSchema>,
855
+ ...(HaveAnyRequiredProps<useRegleSchemaFnOptions<TAdditionalOptions>> extends true ? [options: useRegleSchemaFnOptions<TAdditionalOptions>] : [options?: useRegleSchemaFnOptions<TAdditionalOptions>])
856
+ ]): NonNullable<TState> extends PrimitiveTypes ? RegleSingleFieldSchema<NonNullable<TState>, StandardSchemaV1.InferInput<TSchema>, TShortcuts, TAdditionalReturnProperties> : RegleSchema<UnwrapNestedRefs<NonNullable<TState>>, UnwrapNestedRefs<NonNullable<StandardSchemaV1.InferInput<TSchema>>>, TShortcuts, TAdditionalReturnProperties>;
852
857
  }
853
858
  /**
854
859
  * useRegle serves as the foundation for validation logic.
@@ -923,23 +928,25 @@ declare function defineRegleSchemaConfig<TShortcuts extends RegleShortcutDefinit
923
928
  inferSchema: inferSchemaFn;
924
929
  };
925
930
 
931
+ type CreateScopedUseRegleSchemaOptions<TCustomRegle extends useRegleSchemaFn<any, any>, TAsRecord extends boolean> = Omit<CreateScopedUseRegleOptions<any, TAsRecord>, 'customUseRegle'> & {
932
+ /**
933
+ * Inject a global configuration to the exported composables to keep your translations and typings
934
+ */
935
+ customUseRegle?: TCustomRegle;
936
+ };
926
937
  declare const useCollectSchemaScope: <TValue extends Record<string, unknown>[] = Record<string, unknown>[]>(namespace?: MaybeRefOrGetter<string>) => {
927
938
  r$: MergedScopedRegles<TValue>;
928
939
  };
929
940
  declare const useScopedRegleSchema: useRegleSchemaFn<_regle_core.RegleShortcutDefinition<any>, {}, {}>;
930
- declare const createScopedUseRegleSchema: <TCustomRegle extends useRegleSchemaFn = useRegleSchemaFn, TReturnedRegle extends useRegleSchemaFn = TCustomRegle extends useRegleSchemaFn<infer S> ? useRegleSchemaFn<S, {
941
+ declare const createScopedUseRegleSchema: <TCustomRegle extends useRegleSchemaFn = useRegleSchemaFn, TAsRecord extends boolean = false, TReturnedRegle extends useRegleSchemaFn<any, any, any> = TCustomRegle extends useRegleSchemaFn<infer S> ? useRegleSchemaFn<S, {
931
942
  dispose: () => void;
932
943
  register: () => void;
933
- }, {
934
- namespace?: MaybeRefOrGetter<string>;
935
- }> : useRegleSchemaFn>(options?: {
936
- customUseRegle?: TCustomRegle;
937
- customStore?: Ref<ScopedInstancesRecordLike>;
938
- }) => {
944
+ }, UseScopedRegleOptions<TAsRecord>> : useRegleSchemaFn<any, {
945
+ dispose: () => void;
946
+ register: () => void;
947
+ }, UseScopedRegleOptions<TAsRecord>>>(options?: CreateScopedUseRegleSchemaOptions<TCustomRegle, TAsRecord>) => {
939
948
  useScopedRegle: TReturnedRegle;
940
- useCollectScope<TValue extends Record<string, unknown>[] = Record<string, unknown>[]>(namespace?: MaybeRefOrGetter<string>): {
941
- r$: MergedScopedRegles<TValue>;
942
- };
949
+ useCollectScope: useCollectScopeFn<TAsRecord>;
943
950
  };
944
951
 
945
952
  export { type InferRegleSchemaStatusType, type RegleSchema, type RegleSchemaBehaviourOptions, type RegleSchemaCollectionStatus, type RegleSchemaFieldStatus, type RegleSchemaResult, type RegleSchemaStatus, type RegleSingleFieldSchema, createScopedUseRegleSchema, defineRegleSchemaConfig, inferSchema, useCollectSchemaScope, useRegleSchema, useScopedRegleSchema, withDeps };
@@ -1 +1 @@
1
- import {createScopedUseRegle,useRootStorage}from'@regle/core';import {computed,unref,ref,isRef,watch}from'vue';function q(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 p(e){let t=e,r={}.toString.call(e).slice(8,-1);if(r=="Set"&&(t=new Set([...e].map(a=>p(a)))),r=="Map"&&(t=new Map([...e].map(a=>[p(a[0]),p(a[1])]))),r=="Date"&&(t=new Date(e.getTime())),r=="RegExp"&&(t=RegExp(e.source,q(e))),r=="Array"||r=="Object"){t=Array.isArray(e)?[]:{};for(let a in e)t[a]=p(e[a]);}return t}function g(e){return e&&(e instanceof Date||e.constructor.name=="File"||e.constructor.name=="FileList")?false:typeof e=="object"&&e!==null&&!Array.isArray(e)}function E(e,t,r,a){var n,o;if(Array.isArray(t)&&(n=t.slice(0)),typeof t=="string"&&(n=t.split(".")),typeof t=="symbol"&&(n=[t]),!Array.isArray(n))throw new Error("props arg must be an array, a string or a symbol");if(o=n.pop(),!o)return false;P(o);for(var i;i=n.shift();)if(P(i),isNaN(parseInt(i))?(typeof e[i]>"u"&&(e[i]={}),e=e[i]):((e.$each??=[])[i]={},e=e.$each[i]),!e||typeof e!="object")return false;return a?e[o]?e[o].$self=(e[o].$self??=[]).concat(r):e[o]={...e[o],$self:r}:Array.isArray(e[o])?e[o]=e[o].concat(r):e[o]=r,true}function I(e,t,r){if(!e)return r;var a,n;if(Array.isArray(t)&&(a=t.slice(0)),typeof t=="string"&&(a=t.split(".")),typeof t=="symbol"&&(a=[t]),!Array.isArray(a))throw new Error("props arg must be an array, a string or a symbol");for(;a.length;)if(n=a.shift(),!e||!n||(e=e[n],e===void 0))return r;return e}function P(e){if(e=="__proto__"||e=="constructor"||e=="prototype")throw new Error("setting of prototype values not supported")}function x(e,t){let r={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function a(n,o,i){let m=computed(()=>unref(o)),{syncState:k={onUpdate:false,onValidate:false},...L}=i??{},{onUpdate:$=false,onValidate:z=false}=k,G={...r,...L},H=computed(()=>!g(c.value)),c=isRef(n)?n:ref(n),W=ref(g(c.value)?{...p(c.value)}:p(c.value)),y=ref({}),v;if(!m.value?.["~standard"])throw new Error('Only "standard-schema" compatible libraries are supported');function _(u){let s={};return u.issues&&u.issues.map(l=>{let F=l.path?.map(h=>typeof h=="object"?h.key:h.toString()).join(".")??"",S=l.path?.[l.path.length-1],J=(typeof S=="object"&&"value"in S?Array.isArray(S.value):false)||("type"in l?l.type==="array":false)||Array.isArray(I(c.value,F));return {path:F,message:l.message,isArray:J}}).forEach(l=>{E(s,l.path,[l.message],l.isArray);}),s}async function d(u=false){let s=m.value["~standard"].validate(c.value);return s instanceof Promise&&(s=await s),H.value?y.value=s.issues?.map(b=>b.message)??[]:y.value=_(s),s.issues||(u&&z||!u&&$)&&(w?.(),c.value=s.value,O()),s}let w;function O(){w=watch([c,m],()=>d(),{deep:true});}return O(),d(),v=async()=>{try{return {valid:!(await d(!0)).issues?.length,data:c.value}}catch(u){return Promise.reject(u)}},{r$:useRootStorage({scopeRules:computed(()=>({})),state:c,options:G,schemaErrors:y,initialState:W,shortcuts:t,schemaMode:true,onValidate:v}).regle}}return a}var f=x();function A(e,t){return e}function D(){function e(t,r){return r}return e}var C=D();function N({modifiers:e,shortcuts:t}){let r=x(e,t),a=D();return {useRegleSchema:r,inferSchema:a}}var {useCollectScope:U,useScopedRegle:B}=createScopedUseRegle({customUseRegle:f}),V=e=>{let{customStore:t,customUseRegle:r=f}=e??{};return createScopedUseRegle({customStore:t,customUseRegle:r})};export{V as createScopedUseRegleSchema,N as defineRegleSchemaConfig,C as inferSchema,U as useCollectSchemaScope,f as useRegleSchema,B as useScopedRegleSchema,A as withDeps};
1
+ import {createScopedUseRegle,useRootStorage}from'@regle/core';import {computed,unref,ref,isRef,watch}from'vue';function K(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 u(e){let t=e,r={}.toString.call(e).slice(8,-1);if(r=="Set"&&(t=new Set([...e].map(a=>u(a)))),r=="Map"&&(t=new Map([...e].map(a=>[u(a[0]),u(a[1])]))),r=="Date"&&(t=new Date(e.getTime())),r=="RegExp"&&(t=RegExp(e.source,K(e))),r=="Array"||r=="Object"){t=Array.isArray(e)?[]:{};for(let a in e)t[a]=u(e[a]);}return t}function S(e){return e&&(e instanceof Date||e.constructor.name=="File"||e.constructor.name=="FileList")?false:typeof e=="object"&&e!==null&&!Array.isArray(e)}function U(e,t,r,a){var n,o;if(Array.isArray(t)&&(n=t.slice(0)),typeof t=="string"&&(n=t.split(".")),typeof t=="symbol"&&(n=[t]),!Array.isArray(n))throw new Error("props arg must be an array, a string or a symbol");if(o=n.pop(),!o)return false;C(o);for(var s;s=n.shift();)if(C(s),isNaN(parseInt(s))?(typeof e[s]>"u"&&(e[s]={}),e=e[s]):((e.$each??=[])[s]={},e=e.$each[s]),!e||typeof e!="object")return false;return a?e[o]?e[o].$self=(e[o].$self??=[]).concat(r):e[o]={...e[o],$self:r}:Array.isArray(e[o])?e[o]=e[o].concat(r):e[o]=r,true}function E(e,t,r){if(!e)return r;var a,n;if(Array.isArray(t)&&(a=t.slice(0)),typeof t=="string"&&(a=t.split(".")),typeof t=="symbol"&&(a=[t]),!Array.isArray(a))throw new Error("props arg must be an array, a string or a symbol");for(;a.length;)if(n=a.shift(),!e||!n||(e=e[n],e===void 0))return r;return e}function C(e){if(e=="__proto__"||e=="constructor"||e=="prototype")throw new Error("setting of prototype values not supported")}function N(e,...t){for(var r=[].slice.call(arguments),a,n=r.length;a=r[n-1],n--;)if(!a||typeof a!="object"&&typeof a!="function")throw new Error("expected object, got "+a);for(var o=r[0],s=r.slice(1),f=s.length,n=0;n<f;n++){var d=s[n];for(var y in d)o[y]=d[y];}return o}function O(e,t){let r={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function a(n,o,s){let f=computed(()=>unref(o)),{syncState:d={onUpdate:false,onValidate:false},...y}=s??{},{onUpdate:H=false,onValidate:z=false}=d,W={...r,...y},_=computed(()=>!S(i.value)),i=isRef(n)?n:ref(n),q=ref(S(i.value)?{...u(i.value)}:u(i.value)),h=ref({}),w;if(!f.value?.["~standard"])throw new Error('Only "standard-schema" compatible libraries are supported');function G(p){let c={};return p.issues&&p.issues.map(l=>{let P=l.path?.map(T=>typeof T=="object"?T.key:T.toString()).join(".")??"",R=l.path?.[l.path.length-1],J=(typeof R=="object"&&"value"in R?Array.isArray(R.value):false)||("type"in l?l.type==="array":false)||Array.isArray(E(i.value,P));return {path:P,message:l.message,isArray:J}}).forEach(l=>{U(c,l.path,[l.message],l.isArray);}),c}async function g(p=false){let c=f.value["~standard"].validate(i.value);return c instanceof Promise&&(c=await c),_.value?h.value=c.issues?.map(A=>A.message)??[]:h.value=G(c),c.issues||(p&&z||!p&&H)&&(b?.(),S(i.value)?i.value=N(i.value,c.value):i.value=c.value,F()),c}let b;function F(){b=watch([i,f],()=>g(),{deep:true});}return F(),g(),w=async()=>{try{return {valid:!(await g(!0)).issues?.length,data:i.value}}catch(p){return Promise.reject(p)}},{r$:useRootStorage({scopeRules:computed(()=>({})),state:i,options:W,schemaErrors:h,initialState:q,shortcuts:t,schemaMode:true,onValidate:w}).regle}}return a}var m=O();function I(e,t){return e}function D(){function e(t,r){return r}return e}var M=D();function B({modifiers:e,shortcuts:t}){let r=O(e,t),a=D();return {useRegleSchema:r,inferSchema:a}}var {useCollectScope:k,useScopedRegle:$}=createScopedUseRegle({customUseRegle:m}),L=e=>{let{customStore:t,customUseRegle:r=m,asRecord:a=false}=e??{};return createScopedUseRegle({customStore:t,customUseRegle:r,asRecord:a})};export{L as createScopedUseRegleSchema,B as defineRegleSchemaConfig,M as inferSchema,k as useCollectSchemaScope,m as useRegleSchema,$ as useScopedRegleSchema,I as withDeps};
@@ -135,6 +135,26 @@ function prototypeCheck(prop) {
135
135
  throw new Error("setting of prototype values not supported");
136
136
  }
137
137
  }
138
+ function merge(obj1, ...objs) {
139
+ var args = [].slice.call(arguments);
140
+ var arg;
141
+ var i = args.length;
142
+ while (arg = args[i - 1], i--) {
143
+ if (!arg || typeof arg != "object" && typeof arg != "function") {
144
+ throw new Error("expected object, got " + arg);
145
+ }
146
+ }
147
+ var result = args[0];
148
+ var extenders = args.slice(1);
149
+ var len = extenders.length;
150
+ for (var i = 0; i < len; i++) {
151
+ var extender = extenders[i];
152
+ for (var key in extender) {
153
+ result[key] = extender[key];
154
+ }
155
+ }
156
+ return result;
157
+ }
138
158
 
139
159
  // src/core/useRegleSchema.ts
140
160
  function createUseRegleSchemaComposable(options, shortcuts) {
@@ -194,7 +214,11 @@ function createUseRegleSchemaComposable(options, shortcuts) {
194
214
  if (!result.issues) {
195
215
  if (isValidate && syncOnValidate || !isValidate && syncOnUpdate) {
196
216
  unWatchState?.();
197
- processedState.value = result.value;
217
+ if (isObject(processedState.value)) {
218
+ processedState.value = merge(processedState.value, result.value);
219
+ } else {
220
+ processedState.value = result.value;
221
+ }
198
222
  defineWatchState();
199
223
  }
200
224
  }
@@ -258,8 +282,8 @@ var { useCollectScope: useCollectSchemaScope, useScopedRegle: useScopedRegleSche
258
282
  customUseRegle: useRegleSchema
259
283
  });
260
284
  var createScopedUseRegleSchema = (options) => {
261
- const { customStore, customUseRegle = useRegleSchema } = options ?? {};
262
- return createScopedUseRegle({ customStore, customUseRegle });
285
+ const { customStore, customUseRegle = useRegleSchema, asRecord = false } = options ?? {};
286
+ return createScopedUseRegle({ customStore, customUseRegle, asRecord });
263
287
  };
264
288
 
265
289
  export { createScopedUseRegleSchema, defineRegleSchemaConfig, inferSchema, useCollectSchemaScope, useRegleSchema, useScopedRegleSchema, withDeps };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@regle/schemas",
3
- "version": "1.2.0-beta.5",
3
+ "version": "1.2.0-beta.7",
4
4
  "description": "Schemas adapter for Regle",
5
5
  "dependencies": {
6
6
  "@standard-schema/spec": "1.0.0",
7
- "@regle/core": "1.2.0-beta.5",
8
- "@regle/rules": "1.2.0-beta.5"
7
+ "@regle/rules": "1.2.0-beta.7",
8
+ "@regle/core": "1.2.0-beta.7"
9
9
  },
10
10
  "peerDependencies": {
11
11
  "valibot": "^1.0.0",