@regle/schemas 1.7.0-beta.3 → 1.7.1

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.
@@ -3305,13 +3305,13 @@ interface useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = nev
3305
3305
  *
3306
3306
  * ```ts
3307
3307
  * import { useRegleSchema } from '@regle/schemas';
3308
- import * as v from 'valibot';
3309
-
3310
- const { r$ } = useRegleSchema({ name: '' }, v.object({
3311
- name: v.pipe(v.string(), v.minLength(3))
3312
- }))
3308
+ * import * as v from 'valibot';
3309
+ *
3310
+ * const { r$ } = useRegleSchema({ name: '' }, v.object({
3311
+ * name: v.pipe(v.string(), v.minLength(3))
3312
+ * }))
3313
3313
  * ```
3314
- * Docs: {@link https://reglejs.dev/integrations/valibot#usage}
3314
+ * Docs: {@link https://reglejs.dev/integrations/schemas-libraries}
3315
3315
  */
3316
3316
  declare const useRegleSchema: useRegleSchemaFn<RegleShortcutDefinition<any>, {}, {}>;
3317
3317
  //#endregion
@@ -1,6 +1,36 @@
1
1
  import { createScopedUseRegle, useRootStorage } from "@regle/core";
2
2
  import { computed, isRef, ref, unref, watch } from "vue";
3
3
 
4
+ //#region ../shared/utils/isFile.ts
5
+ /**
6
+ * Server side friendly way of checking for a File
7
+ */
8
+ function isFile(value) {
9
+ return value?.constructor.name == "File" || value?.constructor.name == "FileList";
10
+ }
11
+
12
+ //#endregion
13
+ //#region ../shared/utils/isEmpty.ts
14
+ /**
15
+ * This is the inverse of isFilled. It will check if the value is in any way empty (including arrays and objects)
16
+ *
17
+ * isEmpty also acts as a type guard.
18
+ *
19
+ * @param value - the target value
20
+ * @param [considerEmptyArrayInvalid=true] - will return false if set to `false`. (default: `true`)
21
+ */
22
+ function isEmpty(value, considerEmptyArrayInvalid = true) {
23
+ if (value === void 0 || value === null) return true;
24
+ if (value instanceof Date) return isNaN(value.getTime());
25
+ else if (isFile(value)) return value.size <= 0;
26
+ else if (Array.isArray(value)) {
27
+ if (considerEmptyArrayInvalid) return value.length === 0;
28
+ return false;
29
+ } else if (typeof value === "object" && value != null) return Object.keys(value).length === 0;
30
+ return !String(value).length;
31
+ }
32
+
33
+ //#endregion
4
34
  //#region ../shared/utils/symbol.ts
5
35
  const RegleRuleSymbol = Symbol("regle-rule");
6
36
 
@@ -51,7 +81,8 @@ function setObjectError(obj, propsArg, value, isArray) {
51
81
  while (thisProp = props.shift()) {
52
82
  prototypeCheck(thisProp);
53
83
  if (!isNaN(parseInt(thisProp))) {
54
- (obj.$each ??= [])[thisProp] = {};
84
+ if (obj.$each == void 0) obj.$each = [];
85
+ if (isEmpty(obj.$each[thisProp])) obj.$each[thisProp] = {};
55
86
  obj = obj.$each[thisProp];
56
87
  } else {
57
88
  if (typeof obj[thisProp] == "undefined") obj[thisProp] = {};
@@ -212,16 +243,16 @@ function createUseRegleSchemaComposable(options, shortcuts) {
212
243
  * @param state - This can be a plain object, a ref, a reactive object, or a structure containing nested refs.
213
244
  * @param schema - These should align with the structure of your state.
214
245
  * @param modifiers - customize regle behaviour
215
- *
246
+ *
216
247
  * ```ts
217
248
  * import { useRegleSchema } from '@regle/schemas';
218
- import * as v from 'valibot';
219
-
220
- const { r$ } = useRegleSchema({ name: '' }, v.object({
221
- name: v.pipe(v.string(), v.minLength(3))
222
- }))
249
+ * import * as v from 'valibot';
250
+ *
251
+ * const { r$ } = useRegleSchema({ name: '' }, v.object({
252
+ * name: v.pipe(v.string(), v.minLength(3))
253
+ * }))
223
254
  * ```
224
- * Docs: {@link https://reglejs.dev/integrations/valibot#usage}
255
+ * Docs: {@link https://reglejs.dev/integrations/schemas-libraries}
225
256
  */
226
257
  const useRegleSchema = createUseRegleSchemaComposable();
227
258
 
@@ -1 +1 @@
1
- import{createScopedUseRegle as e,useRootStorage as t}from"@regle/core";import{computed as n,isRef as r,ref as i,unref as a,watch as o}from"vue";Symbol(`regle-rule`);function s(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 c(e){let t=e,n={}.toString.call(e).slice(8,-1);if(n==`Set`&&(t=new Set([...e].map(e=>c(e)))),n==`Map`&&(t=new Map([...e].map(e=>[c(e[0]),c(e[1])]))),n==`Date`&&(t=new Date(e.getTime())),n==`RegExp`&&(t=RegExp(e.source,s(e))),n==`Array`||n==`Object`)for(let n in t=Array.isArray(e)?[]:{},e)t[n]=c(e[n]);return t}function l(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function u(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;f(a);for(var o;o=i.shift();)if(f(o),isNaN(parseInt(o))?(e[o]===void 0&&(e[o]={}),e=e[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]={...e[a],$self:n}:Array.isArray(e[a])?e[a]=e[a].concat(n):e[a]=n,!0}function d(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 f(e){if(e==`__proto__`||e==`constructor`||e==`prototype`)throw Error(`setting of prototype values not supported`)}function p(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 m(e,s){let f={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function m(e,m,h){let g=n(()=>a(m)),{syncState:_={onUpdate:!1,onValidate:!1},...v}=h??{},{onUpdate:y=!1,onValidate:b=!1}=_,x={...f,...v},S=n(()=>!l(C.value)),C=r(e)?e:i(e),w=i(l(C.value)?{...c(C.value)}:c(C.value)),T=i({}),E;if(!g.value?.[`~standard`])throw Error(`Only "standard-schema" compatible libraries are supported`);function D(e){let t={};if(e.issues){let n=e.issues.map(e=>{let t=e.path?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``,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(d(C.value,t)),a=!i&&typeof r==`number`;return a&&(t=e.path?.slice(0,e.path.length-1)?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``),{...e,$path:t,isArray:i,$property:r,$rule:`schema`,$message:e.message}});n.forEach(({isArray:e,$path:n,...r})=>{u(t,n,[r],e)})}return t}async function O(e=!1){let t=g.value[`~standard`].validate(C.value);return t instanceof Promise&&(t=await t),S.value?T.value=t.issues?.map(e=>({$message:e.message,$property:e.path?.[e.path.length-1]?.toString()??`-`,$rule:`schema`,...e}))??[]:T.value=D(t),t.issues||(e&&b||!e&&y)&&(k?.(),l(C.value)?C.value=p(C.value,t.value):C.value=t.value,A()),t}let k;function A(){k=o([C,g],()=>O(),{deep:!0})}A(),O(),E=async()=>{try{let e=await O(!0);return{valid:!e.issues?.length,data:C.value}}catch(e){return Promise.reject(e)}};let j=t({scopeRules:n(()=>({})),state:C,options:x,schemaErrors:T,initialState:w,shortcuts:s,schemaMode:!0,onValidate:E});return{r$:j.regle}}return m}const h=m();function g(e,t){return e}function _(){function e(e,t){return t}return e}const v=_();function y({modifiers:e,shortcuts:t}){let n=m(e,t),r=_();return{useRegleSchema:n,inferSchema:r}}const{useCollectScope:b,useScopedRegle:x}=e({customUseRegle:h}),S=t=>{let{customStore:n,customUseRegle:r=h,asRecord:i=!1}=t??{};return e({customStore:n,customUseRegle:r,asRecord:i})};export{S as createScopedUseRegleSchema,y as defineRegleSchemaConfig,v as inferSchema,b as useCollectSchemaScope,h as useRegleSchema,x as useScopedRegleSchema,g as withDeps};
1
+ import{createScopedUseRegle as e,useRootStorage as t}from"@regle/core";import{computed as n,isRef as r,ref as i,unref as a,watch as o}from"vue";function s(e){return e?.constructor.name==`File`||e?.constructor.name==`FileList`}function c(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):s(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}Symbol(`regle-rule`);function l(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,n={}.toString.call(e).slice(8,-1);if(n==`Set`&&(t=new Set([...e].map(e=>u(e)))),n==`Map`&&(t=new Map([...e].map(e=>[u(e[0]),u(e[1])]))),n==`Date`&&(t=new Date(e.getTime())),n==`RegExp`&&(t=RegExp(e.source,l(e))),n==`Array`||n==`Object`)for(let n in t=Array.isArray(e)?[]:{},e)t[n]=u(e[n]);return t}function d(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function f(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;m(a);for(var o;o=i.shift();)if(m(o),isNaN(parseInt(o))?(e[o]===void 0&&(e[o]={}),e=e[o]):(e.$each??=[],c(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]={...e[a],$self:n}:Array.isArray(e[a])?e[a]=e[a].concat(n):e[a]=n,!0}function p(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 m(e){if(e==`__proto__`||e==`constructor`||e==`prototype`)throw Error(`setting of prototype values not supported`)}function h(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 g(e,s){let c={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function l(e,l,m){let g=n(()=>a(l)),{syncState:_={onUpdate:!1,onValidate:!1},...v}=m??{},{onUpdate:y=!1,onValidate:b=!1}=_,x={...c,...v},S=n(()=>!d(C.value)),C=r(e)?e:i(e),w=i(d(C.value)?{...u(C.value)}:u(C.value)),T=i({}),E;if(!g.value?.[`~standard`])throw Error(`Only "standard-schema" compatible libraries are supported`);function D(e){let t={};if(e.issues){let n=e.issues.map(e=>{let t=e.path?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``,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(p(C.value,t)),a=!i&&typeof r==`number`;return a&&(t=e.path?.slice(0,e.path.length-1)?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``),{...e,$path:t,isArray:i,$property:r,$rule:`schema`,$message:e.message}});n.forEach(({isArray:e,$path:n,...r})=>{f(t,n,[r],e)})}return t}async function O(e=!1){let t=g.value[`~standard`].validate(C.value);return t instanceof Promise&&(t=await t),S.value?T.value=t.issues?.map(e=>({$message:e.message,$property:e.path?.[e.path.length-1]?.toString()??`-`,$rule:`schema`,...e}))??[]:T.value=D(t),t.issues||(e&&b||!e&&y)&&(k?.(),d(C.value)?C.value=h(C.value,t.value):C.value=t.value,A()),t}let k;function A(){k=o([C,g],()=>O(),{deep:!0})}A(),O(),E=async()=>{try{let e=await O(!0);return{valid:!e.issues?.length,data:C.value}}catch(e){return Promise.reject(e)}};let j=t({scopeRules:n(()=>({})),state:C,options:x,schemaErrors:T,initialState:w,shortcuts:s,schemaMode:!0,onValidate:E});return{r$:j.regle}}return l}const _=g();function v(e,t){return e}function y(){function e(e,t){return t}return e}const b=y();function x({modifiers:e,shortcuts:t}){let n=g(e,t),r=y();return{useRegleSchema:n,inferSchema:r}}const{useCollectScope:S,useScopedRegle:C}=e({customUseRegle:_}),w=t=>{let{customStore:n,customUseRegle:r=_,asRecord:i=!1}=t??{};return e({customStore:n,customUseRegle:r,asRecord:i})};export{w as createScopedUseRegleSchema,x as defineRegleSchemaConfig,b as inferSchema,S as useCollectSchemaScope,_ as useRegleSchema,C as useScopedRegleSchema,v as withDeps};
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@regle/schemas",
3
- "version": "1.7.0-beta.3",
3
+ "version": "1.7.1",
4
4
  "description": "Schemas adapter for Regle",
5
5
  "dependencies": {
6
6
  "@standard-schema/spec": "1.0.0",
7
- "@regle/core": "1.7.0-beta.3",
8
- "@regle/rules": "1.7.0-beta.3"
7
+ "@regle/core": "1.7.1",
8
+ "@regle/rules": "1.7.1"
9
9
  },
10
10
  "peerDependencies": {
11
11
  "valibot": "^1.0.0",