runcheck 0.31.0 → 0.33.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.
package/README.md CHANGED
@@ -125,6 +125,20 @@ const shape = rc_record(rc_number, {
125
125
  })
126
126
  ```
127
127
 
128
+ ## `rc_loose_record`
129
+
130
+ Validates only the values of a object, equivalent to `Record<string, T>` in typescript. But, it will reject invalid keys and return the valid ones.
131
+
132
+
133
+
134
+ ```ts
135
+ const shape = rc_loose_record(rc_number)
136
+
137
+
138
+
139
+ ```
140
+
141
+
128
142
  # Parsing
129
143
 
130
144
  ```ts
@@ -252,7 +266,7 @@ const positiveNumberType = rc_number.where((input) => input > 0)
252
266
 
253
267
  # Infer types from schemas
254
268
 
255
- You can use `RcInferType<typeof T>` to infer the types from a schema.
269
+ You can use `RcInferType<typeof schema>` to infer the types from a schema.
256
270
 
257
271
  ```ts
258
272
  const schema = rc_object({
@@ -264,15 +278,17 @@ const schema = rc_object({
264
278
  export type Person = RcInferType<typeof schema>
265
279
  ```
266
280
 
281
+ You can also use the `RcPrettyInferType<typeof schema>` to get a more readable type.
282
+
267
283
  # Type modifiers
268
284
 
269
- You can use also modiers like `rc_string.optional()` to extend or modify the rc types:
285
+ You can use also modiers like `rc_string.optional()` to extend the rc types:
270
286
 
271
- | runcheck modifier | ts type equivalent |
272
- | ---------------------- | ------------------------ |
273
- | `rc_[type].optional()` | `T \| undefined` |
274
- | `rc_[type].nullable()` | `T \| null` |
275
- | `rc_[type].nullish()` | `T \| null \| undefined` |
287
+ | runcheck modifier | ts type equivalent |
288
+ | ----------------------- | ------------------------ |
289
+ | `rc_[type].optional()` | `T \| undefined` |
290
+ | `rc_[type].orNull()` | `T \| null` |
291
+ | `rc_[type].orNullish()` | `T \| null \| undefined` |
276
292
 
277
293
  # Recursive types
278
294
 
@@ -400,4 +416,36 @@ const shape = rc_object({
400
416
  const baseSchema = rc_obj_omit(shape, ['isCool'])
401
417
  ```
402
418
 
403
- # Other useful utils
419
+ # Investigage rc nested type errors
420
+
421
+ There are two type utils to help investigate deeply nested type errors, the `RcPrettyInferType` and the `RcSchemaHasType`
422
+
423
+ ```ts
424
+ const schema = rc_object({
425
+ level1: {
426
+ level2: {
427
+ level3: {
428
+ level4: {
429
+ level5: rc_string,
430
+ },
431
+ },
432
+ },
433
+ },
434
+ })
435
+
436
+ type SchemaType = {
437
+ level1: {
438
+ level2: {
439
+ level3: {
440
+ level4: {
441
+ level5: number
442
+ }
443
+ }
444
+ }
445
+ }
446
+ }
447
+
448
+ type CheckType = RcSchemaHasType<SchemaType, RcPrettyInferType<typeof schema>>
449
+ // this will reporte better errors than:
450
+ // const schema: RcType<SchemaType> = rc_object...
451
+ ```
@@ -1 +1 @@
1
- "use strict";var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var h=(e,r)=>{for(var n in r)u(e,n,{get:r[n],enumerable:!0})},_=(e,r,n,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of R(r))!b.call(e,t)&&t!==n&&u(e,t,{get:()=>r[t],enumerable:!(a=T(r,t))||a.enumerable});return e};var j=e=>_(u({},"__esModule",{value:!0}),e);var K={};h(K,{rc_boolean_autofix:()=>P,rc_number_autofix:()=>C,rc_string_autofix:()=>I});module.exports=j(K);function g(e){return{...this,i:e}}function x(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function f(e,r){e.warnings.push(x(e,r))}function s(e,r,n,a){if(e.c&&r===void 0)return[!0,r];if(e.u&&r==null)return[!0,r];if(e.f&&r===null)return[!0,r];let t=a();if(t&&(t===!0||!t.errors)){let i=t===!0?r:t.data;return e.t&&!e.t(i)?[!1,[`Predicate failed for type '${e.e}'`]]:[!0,i]}let c=e.i;if(c!==void 0)return f(n,`Fallback used, errors -> ${l(t,n,e,r)}`),[!0,S(c)?c():c];if(e.l&&e.o){let i=e.o(r);if(i)return e.t&&!e.t(i.fixed)?[!1,[`Predicate failed for autofix in type '${e.e}'`]]:(f(n,`Autofixed from error "${l(t,n,e,r)}"`),[!0,i.fixed])}return[!1,t?t.errors:[e.y(r)]]}function l(e,r,n,a){return e?e.errors.map(t=>t.replace(r.path,"")).join("; "):n.y(a)}function O(e){return{...this,l:!0,o:e}}function m(e){return{...this,t:e}}function w(){return{...this,c:!0}}function k(e){return`Type '${A(e,!!this.d)}' is not assignable to '${this.e}'`}function $(){return{...this,f:!0,e:`${this.e}_or_null`}}function E(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var o={withFallback:g,where:m,optional:w,y:k,orNullish:E,withAutofix:O,orNull:$,i:void 0,t:void 0,c:!1,f:!1,u:!1,l:!1,d:!1,s:void 0,o:void 0,R:void 0,n:void 0,a:!1,p:!1,T:[]},V={...o,r(e,r){return s(this,e,r,()=>e===void 0)},e:"undefined"},q={...o,r(e,r){return s(this,e,r,()=>e===null)},e:"null"},v={...o,r(e,r){return s(this,e,r,()=>!0)},e:"any"},B={...o,r(e,r){return s(this,e,r,()=>!0)},e:"unknown"},y={...o,r(e,r){return s(this,e,r,()=>typeof e=="boolean")},e:"boolean"},d={...o,r(e,r){return s(this,e,r,()=>typeof e=="string")},e:"string"},p={...o,r(e,r){return s(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},F={...o,r(e,r){return s(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function A(e,r){let n=(()=>{if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e})();return r&&(n==="string"||n==="number"||n==="boolean")?`${n}(${e})`:n}function S(e){return typeof e=="function"}var P=y.withAutofix(e=>e==null||e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),I=d.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),C=p.withAutofix(e=>{if(typeof e=="string"){let r=Number(e);if(!Number.isNaN(r))return{fixed:r}}return!1});0&&(module.exports={rc_boolean_autofix,rc_number_autofix,rc_string_autofix});
1
+ "use strict";var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var h=(e,r)=>{for(var n in r)u(e,n,{get:r[n],enumerable:!0})},_=(e,r,n,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of R(r))!b.call(e,t)&&t!==n&&u(e,t,{get:()=>r[t],enumerable:!(a=T(r,t))||a.enumerable});return e};var j=e=>_(u({},"__esModule",{value:!0}),e);var K={};h(K,{rc_boolean_autofix:()=>A,rc_number_autofix:()=>C,rc_string_autofix:()=>I});module.exports=j(K);function g(e){return{...this,i:e}}function x(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function f(e,r){e.warnings.push(x(e,r))}function o(e,r,n,a){if(e.c&&r===void 0)return[!0,r];if(e.u&&r==null)return[!0,r];if(e.f&&r===null)return[!0,r];let t=a();if(t&&(t===!0||!t.errors)){let i=t===!0?r:t.data;return e.t&&!e.t(i)?[!1,[`Predicate failed for type '${e.e}'`]]:[!0,i]}let c=e.i;if(c!==void 0)return f(n,`Fallback used, errors -> ${y(t,n,e,r)}`),[!0,S(c)?c():c];if(e.y&&e.s){let i=e.s(r);if(i)return e.t&&!e.t(i.fixed)?[!1,[`Predicate failed for autofix in type '${e.e}'`]]:(f(n,`Autofixed from error "${y(t,n,e,r)}"`),[!0,i.fixed])}return[!1,t?t.errors:[e.l(r)]]}function y(e,r,n,a){return e?e.errors.map(t=>t.replace(r.path,"")).join("; "):n.l(a)}function O(e){return{...this,y:!0,s:e}}function m(e){return{...this,t:e}}function w(){return{...this,c:!0}}function k(e){return`Type '${P(e,!!this.d)}' is not assignable to '${this.e}'`}function $(){return{...this,f:!0,e:`${this.e}_or_null`}}function E(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var s={withFallback:g,where:m,optional:w,l:k,orNullish:E,withAutofix:O,orNull:$,i:void 0,t:void 0,c:!1,f:!1,u:!1,y:!1,d:!1,o:void 0,s:void 0,R:void 0,n:void 0,a:!1,p:!1,T:[]},V={...s,r(e,r){return o(this,e,r,()=>e===void 0)},e:"undefined"},q={...s,r(e,r){return o(this,e,r,()=>e===null)},e:"null"},v={...s,r(e,r){return o(this,e,r,()=>!0)},e:"any"},B={...s,r(e,r){return o(this,e,r,()=>!0)},e:"unknown"},l={...s,r(e,r){return o(this,e,r,()=>typeof e=="boolean")},e:"boolean"},d={...s,r(e,r){return o(this,e,r,()=>typeof e=="string")},e:"string"},p={...s,r(e,r){return o(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},F={...s,r(e,r){return o(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function P(e,r){let n=(()=>{if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e})();return r&&(n==="string"||n==="number"||n==="boolean")?`${n}(${e})`:n}function S(e){return typeof e=="function"}var A=l.withAutofix(e=>e==null||e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),I=d.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),C=p.withAutofix(e=>{if(typeof e=="string"){let r=Number(e);if(!Number.isNaN(r))return{fixed:r}}return!1});0&&(module.exports={rc_boolean_autofix,rc_number_autofix,rc_string_autofix});
@@ -1 +1 @@
1
- import{e as f,f as t,g as o}from"./chunk-YAXWQU2M.js";var s=f.withAutofix(r=>r==null||r===0||r===1?{fixed:!!r}:r==="true"||r==="false"?{fixed:r==="true"}:!1),n=t.withAutofix(r=>typeof r=="number"&&!Number.isNaN(r)?{fixed:r.toString()}:!1),u=o.withAutofix(r=>{if(typeof r=="string"){let e=Number(r);if(!Number.isNaN(e))return{fixed:e}}return!1});export{s as rc_boolean_autofix,u as rc_number_autofix,n as rc_string_autofix};
1
+ import{e as f,f as t,g as o}from"./chunk-3GG5XFSK.js";var s=f.withAutofix(r=>r==null||r===0||r===1?{fixed:!!r}:r==="true"||r==="false"?{fixed:r==="true"}:!1),n=t.withAutofix(r=>typeof r=="number"&&!Number.isNaN(r)?{fixed:r.toString()}:!1),u=o.withAutofix(r=>{if(typeof r=="string"){let e=Number(r);if(!Number.isNaN(e))return{fixed:e}}return!1});export{s as rc_boolean_autofix,u as rc_number_autofix,n as rc_string_autofix};
@@ -0,0 +1 @@
1
+ function q(e){return{...this,i:e}}function g(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function m(e,r){e.warnings.push(g(e,r))}function C(e,r){r.forEach(n=>m(e,n))}function u(e,r,n,t){if(e.c&&r===void 0)return[!0,r];if(e.u&&r==null)return[!0,r];if(e.f&&r===null)return[!0,r];let s=t();if(s&&(s===!0||!s.errors)){let o=s===!0?r:s.data;return e.t&&!e.t(o)?[!1,[`Predicate failed for type '${e.e}'`]]:[!0,o]}let a=e.i;if(a!==void 0)return m(n,`Fallback used, errors -> ${I(s,n,e,r)}`),[!0,J(a)?a():a];if(e.y&&e.s){let o=e.s(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[`Predicate failed for autofix in type '${e.e}'`]]:(m(n,`Autofixed from error "${I(s,n,e,r)}"`),[!0,o.fixed])}return[!1,s?s.errors:[e.l(r)]]}function I(e,r,n,t){return e?e.errors.map(s=>s.replace(r.path,"")).join("; "):n.l(t)}function N(e){return{...this,y:!0,s:e}}function v(e){return{...this,t:e}}function B(){return{...this,c:!0}}function F(e){return`Type '${$(e,!!this.d)}' is not assignable to '${this.e}'`}function W(){return{...this,f:!0,e:`${this.e}_or_null`}}function D(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var i={withFallback:q,where:v,optional:B,l:F,orNullish:D,withAutofix:N,orNull:W,i:void 0,t:void 0,c:!1,f:!1,u:!1,y:!1,d:!1,o:void 0,s:void 0,R:void 0,n:void 0,a:!1,p:!1,T:[]},L={...i,r(e,r){return u(this,e,r,()=>e===void 0)},e:"undefined"},Z={...i,r(e,r){return u(this,e,r,()=>e===null)},e:"null"},G={...i,r(e,r){return u(this,e,r,()=>!0)},e:"any"},Q={...i,r(e,r){return u(this,e,r,()=>!0)},e:"unknown"},X={...i,r(e,r){return u(this,e,r,()=>typeof e=="boolean")},e:"boolean"},Y={...i,r(e,r){return u(this,e,r,()=>typeof e=="string")},e:"string"},ee={...i,r(e,r){return u(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},re={...i,r(e,r){return u(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function ne(e){return{...i,r(r,n){return u(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function te(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return u(this,r,n,()=>{for(let t of e)if(r===t)return!0;return!1})},d:!0,e:e.length==1?$(e[0],!0):e.map(r=>$(r,!0)).join(" | ")}}var A=1;function se(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return u(this,r,n,()=>{let t=n.path,s=[],a=0,o=!1,T=[],y=0;for(let c of e){y+=1,c.a&&(n.path=`${t}|union ${y}|`),n.objErrShortCircuit=!0,n.objErrKeyIndex=0;let[f,b]=c.r(r,n);n.objErrShortCircuit=!1;let l=n.objErrKeyIndex;if(n.objErrKeyIndex=0,f)return!0;c.a&&l!==-1?l>0?T.push(...b):(a<A&&s.push(...b),a+=1):o=!0}return n.path=t,T.length>0||s.length>0?((a>A||o)&&s.push("not matches any other union member"),{errors:[...T,...s],data:void 0}):!1})},e:e.map(r=>r.e).join(" | ")}}function U(e,r){return{...r,o:e}}var oe=U;function O(e,{normalizeKeysFrom:r}={}){return{...i,n:e,e:"object",a:!0,T:Object.entries(e),r(n,t){return u(this,n,t,()=>{if(!S(n))return t.objErrKeyIndex=-1,!1;let s=this.e==="strict_obj"?new Set(Object.keys(n)):void 0,a={},o=[],T=t.path,y=-1;for(let[c,f]of this.T){let b=c;y+=1;let l=`.${c}`;t.path=`${T}${l}`;let p,R=c;if(f.o&&(p=n[f.o],R=f.o),p===void 0&&(p=n[c],R=c),p===void 0&&r==="snake_case"){let h=H(c);p=n[h],R=h}s?.delete(R);let[j,x]=f.r(p,t);if(j)a[b]=x;else{for(let h of x)o.push(g(t,h));if(t.objErrShortCircuit){t.objErrKeyIndex=y;break}}}if(s&&s.size>0)for(let c of s)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o,data:void 0}:this.p?{errors:!1,data:{...n,...a}}:{errors:!1,data:a}})}}}function ae(e){return{...O(e),e:"extends_object",p:!0}}function ie(e){return e.n}function ce(e){return{...O(e),e:"strict_obj"}}function ue(...e){let r={};for(let n of e)Object.assign(r,n.n);return O(r)}function ye(e,r){let n={};if(!e.n)throw new Error("rc_obj_pick: obj must be an object type");for(let t of r){let s=e.n[t];s&&(n[t]=s)}return O(n)}function fe(e,r){let n={};if(!e.n)throw new Error("rc_obj_omit: obj must be an object type");for(let t of Object.keys(e.n))r.includes(t)||(n[t]=e.n[t]);return O(n)}function M(e,{checkKey:r,looseCheck:n}={}){return{...i,e:`record<string, ${e.e}>`,r(t,s){return u(this,t,s,()=>{if(!S(t))return!1;let a={},o=[],T=s.path;for(let[y,c]of Object.entries(t)){let f=`.${y}`,b=`${T}${f}`;if(s.path=b,r&&!r(y)){o.push(g(s,`Key '${y}' is not allowed`));continue}let l=t[y],[p,R]=e.r(c,s);if(p)a[y]=l;else{let j=R;for(let x of j)o.push(g(s,x));if(s.objErrShortCircuit)break}}if(o.length>0)if(n)C(s,o);else return{errors:o,data:void 0};return{errors:!1,data:a}})}}}function le(e,{checkKey:r}={}){return M(e,{checkKey:r,looseCheck:!0})}function K(e,r){if(typeof r=="string"&&!e.n?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function E(e,r,n,t=!1,s){let a=[],o=[],T=new Set,y=n.path,c=Array.isArray(r),f=-1;for(let b of e){f++;let l=c?r[f]:r,p=`[${f}]`,R=`${y}${p}`;n.path=R;let j=l.r(b,n),[x,h]=j;n.path=R;let _=s?.unique;if(x&&_){let d=h,w=typeof _=="string";w?d=h[_]:typeof _=="function"&&(d=_(h)),T.has(d)?(w&&(n.path=`${y}${p}.${_}`),j=[!1,[g(n,w?`Type '${l.n?.[_]?.e}' with value "${d}" is not unique`:typeof _=="function"?`Type '${l.e}' unique fn return with value "${d}" is not unique`:`${l.e} value is not unique`)]]):T.add(d)}let[V,k]=j;if(V)o.push(k);else if(t){a.push(k.map(d=>g(n,d)));continue}else return{errors:k.map(d=>g(n,d)),data:void 0}}if(a.length>0){if(o.length===0)return{errors:a.slice(0,5).flat(),data:void 0};C(n,a.flat())}return{errors:!1,data:o}}function pe(e,r){return K(e,r?.unique),{...i,e:`${e.e}[]`,r(n,t){return u(this,n,t,()=>Array.isArray(n)?n.length===0?!0:E.call(this,n,e,t,!1,r):!1)}}}function Te(e,r){return K(e,r?.unique),{...i,e:`${e.e}[]`,r(n,t){return u(this,n,t,()=>Array.isArray(n)?n.length===0?!0:E.call(this,n,e,t,!0,r):!1)}}}function de(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return u(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:E.call(this,r,e,n))}}}function P(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0},[t,s]=r.r(e,n);return t?{error:!1,data:s,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:s}}function Re(e){return r=>P(r,e)}function be(e,r){let n=P(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function z(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0};return!!r.r(e,n)[0]}function he(e){return r=>z(r,e)}function _e(e){return{...i,e:"recursive",r(r,n){return e().r(r,n)}}}function je(e,r){return{...i,e:`transform_from_${e.e}`,r(n,t){let[s,a]=e.r(n,t);return s?[!0,r(a)]:[!1,a]}}}function $(e,r){let n=(()=>{if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e})();return r&&(n==="string"||n==="number"||n==="boolean")?`${n}(${e})`:n}function ge(e){if(e.error)throw new Error(`invalid input: ${e.errors.join(", ")}`)}function S(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function H(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("_")}function xe(e,r){try{let n=JSON.parse(e);return P(n,r)}catch(n){return{error:!0,errors:[`json parse error: ${S(n)?n.message:""}`]}}}function J(e){return typeof e=="function"}export{L as a,Z as b,G as c,Q as d,X as e,Y as f,ee as g,re as h,ne as i,te as j,se as k,U as l,oe as m,O as n,ae as o,ie as p,ce as q,ue as r,ye as s,fe as t,M as u,le as v,pe as w,Te as x,de as y,P as z,Re as A,be as B,z as C,he as D,_e as E,je as F,ge as G,H,xe as I};
package/dist/runcheck.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var $=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var U=(e,r)=>{for(var n in r)$(e,n,{get:r[n],enumerable:!0})},M=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of W(r))!D.call(e,t)&&t!==n&&$(e,t,{get:()=>r[t],enumerable:!(s=F(r,t))||s.enumerable});return e};var z=e=>M($({},"__esModule",{value:!0}),e);var Pe={};U(Pe,{rc_any:()=>re,rc_array:()=>he,rc_assert_is_valid:()=>me,rc_boolean:()=>te,rc_date:()=>ae,rc_extends_obj:()=>fe,rc_get_obj_schema:()=>ye,rc_instanceof:()=>ie,rc_is_valid:()=>N,rc_literals:()=>ce,rc_loose_array:()=>_e,rc_loose_parse:()=>xe,rc_null:()=>ee,rc_number:()=>oe,rc_obj_intersection:()=>de,rc_obj_omit:()=>Re,rc_obj_pick:()=>Te,rc_object:()=>O,rc_parse:()=>w,rc_parse_json:()=>$e,rc_parser:()=>ge,rc_record:()=>be,rc_recursive:()=>we,rc_rename_from_key:()=>V,rc_rename_key:()=>le,rc_strict_obj:()=>pe,rc_string:()=>se,rc_transform:()=>ke,rc_tuple:()=>je,rc_undefined:()=>Y,rc_union:()=>ue,rc_unknown:()=>ne,rc_validator:()=>Oe,snakeCase:()=>v});module.exports=z(Pe);function J(e){return{...this,i:e}}function g(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function E(e,r){e.warnings.push(g(e,r))}function K(e,r){r.forEach(n=>E(e,n))}function u(e,r,n,s){if(e.c&&r===void 0)return[!0,r];if(e.u&&r==null)return[!0,r];if(e.l&&r===null)return[!0,r];let t=s();if(t&&(t===!0||!t.errors)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[`Predicate failed for type '${e.e}'`]]:[!0,o]}let a=e.i;if(a!==void 0)return E(n,`Fallback used, errors -> ${I(t,n,e,r)}`),[!0,Ee(a)?a():a];if(e.f&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[`Predicate failed for autofix in type '${e.e}'`]]:(E(n,`Autofixed from error "${I(t,n,e,r)}"`),[!0,o.fixed])}return[!1,t?t.errors:[e.y(r)]]}function I(e,r,n,s){return e?e.errors.map(t=>t.replace(r.path,"")).join("; "):n.y(s)}function L(e){return{...this,f:!0,o:e}}function Z(e){return{...this,t:e}}function G(){return{...this,c:!0}}function H(e){return`Type '${P(e,!!this.p)}' is not assignable to '${this.e}'`}function Q(){return{...this,l:!0,e:`${this.e}_or_null`}}function X(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var i={withFallback:J,where:Z,optional:G,y:H,orNullish:X,withAutofix:L,orNull:Q,i:void 0,t:void 0,c:!1,l:!1,u:!1,f:!1,p:!1,s:void 0,o:void 0,R:void 0,n:void 0,a:!1,d:!1,T:[]},Y={...i,r(e,r){return u(this,e,r,()=>e===void 0)},e:"undefined"},ee={...i,r(e,r){return u(this,e,r,()=>e===null)},e:"null"},re={...i,r(e,r){return u(this,e,r,()=>!0)},e:"any"},ne={...i,r(e,r){return u(this,e,r,()=>!0)},e:"unknown"},te={...i,r(e,r){return u(this,e,r,()=>typeof e=="boolean")},e:"boolean"},se={...i,r(e,r){return u(this,e,r,()=>typeof e=="string")},e:"string"},oe={...i,r(e,r){return u(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},ae={...i,r(e,r){return u(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function ie(e){return{...i,r(r,n){return u(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function ce(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return u(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},p:!0,e:e.length==1?P(e[0],!0):e.map(r=>P(r,!0)).join(" | ")}}var C=1;function ue(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return u(this,r,n,()=>{let s=n.path,t=[],a=0,o=!1,d=[],l=0;for(let c of e){l+=1,c.a&&(n.path=`${s}|union ${l}|`),n.objErrShortCircuit=!0,n.objErrKeyIndex=0;let[f,b]=c.r(r,n);n.objErrShortCircuit=!1;let y=n.objErrKeyIndex;if(n.objErrKeyIndex=0,f)return!0;c.a&&y!==-1?y>0?d.push(...b):(a<C&&t.push(...b),a+=1):o=!0}return n.path=s,d.length>0||t.length>0?((a>C||o)&&t.push("not matches any other union member"),{errors:[...d,...t],data:void 0}):!1})},e:e.map(r=>r.e).join(" | ")}}function V(e,r){return{...r,s:e}}var le=V;function O(e,{normalizeKeysFrom:r}={}){return{...i,n:e,e:"object",a:!0,T:Object.entries(e),r(n,s){return u(this,n,s,()=>{if(!A(n))return s.objErrKeyIndex=-1,!1;let t=this.e==="strict_obj"?new Set(Object.keys(n)):void 0,a={},o=[],d=s.path,l=-1;for(let[c,f]of this.T){let b=c;l+=1;let y=`.${c}`;s.path=`${d}${y}`;let p,R=c;if(f.s&&(p=n[f.s],R=f.s),p===void 0&&(p=n[c],R=c),p===void 0&&r==="snake_case"){let h=v(c);p=n[h],R=h}t?.delete(R);let[j,x]=f.r(p,s);if(j)a[b]=x;else{for(let h of x)o.push(g(s,h));if(s.objErrShortCircuit){s.objErrKeyIndex=l;break}}}if(t&&t.size>0)for(let c of t)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o,data:void 0}:this.d?{errors:!1,data:{...n,...a}}:{errors:!1,data:a}})}}}function fe(e){return{...O(e),e:"extends_object",d:!0}}function ye(e){return e.n}function pe(e){return{...O(e),e:"strict_obj"}}function de(...e){let r={};for(let n of e)Object.assign(r,n.n);return O(r)}function Te(e,r){let n={};if(!e.n)throw new Error("rc_obj_pick: obj must be an object type");for(let s of r){let t=e.n[s];t&&(n[s]=t)}return O(n)}function Re(e,r){let n={};if(!e.n)throw new Error("rc_obj_omit: obj must be an object type");for(let s of Object.keys(e.n))r.includes(s)||(n[s]=e.n[s]);return O(n)}function be(e,{checkKey:r,looseCheck:n}={}){return{...i,e:`record<string, ${e.e}>`,r(s,t){return u(this,s,t,()=>{if(!A(s))return!1;let a={},o=[],d=t.path;for(let[l,c]of Object.entries(s)){let f=`.${l}`,b=`${d}${f}`;if(t.path=b,r&&!r(l)){o.push(g(t,`Key '${l}' is not allowed`));continue}let y=s[l],[p,R]=e.r(c,t);if(p)a[l]=y;else{let j=R;for(let x of j)o.push(g(t,x));if(t.objErrShortCircuit)break}}if(o.length>0)if(n)K(t,o);else return{errors:o,data:void 0};return{errors:!1,data:a}})}}}function q(e,r){if(typeof r=="string"&&!e.n?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function S(e,r,n,s=!1,t){let a=[],o=[],d=new Set,l=n.path,c=Array.isArray(r),f=-1;for(let b of e){f++;let y=c?r[f]:r,p=`[${f}]`,R=`${l}${p}`;n.path=R;let j=y.r(b,n),[x,h]=j;n.path=R;let _=t?.unique;if(x&&_){let T=h,m=typeof _=="string";m?T=h[_]:typeof _=="function"&&(T=_(h)),d.has(T)?(m&&(n.path=`${l}${p}.${_}`),j=[!1,[g(n,m?`Type '${y.n?.[_]?.e}' with value "${T}" is not unique`:typeof _=="function"?`Type '${y.e}' unique fn return with value "${T}" is not unique`:`${y.e} value is not unique`)]]):d.add(T)}let[B,k]=j;if(B)o.push(k);else if(s){a.push(k.map(T=>g(n,T)));continue}else return{errors:k.map(T=>g(n,T)),data:void 0}}if(a.length>0){if(o.length===0)return{errors:a.slice(0,5).flat(),data:void 0};K(n,a.flat())}return{errors:!1,data:o}}function he(e,r){return q(e,r?.unique),{...i,e:`${e.e}[]`,r(n,s){return u(this,n,s,()=>Array.isArray(n)?n.length===0?!0:S.call(this,n,e,s,!1,r):!1)}}}function _e(e,r){return q(e,r?.unique),{...i,e:`${e.e}[]`,r(n,s){return u(this,n,s,()=>Array.isArray(n)?n.length===0?!0:S.call(this,n,e,s,!0,r):!1)}}}function je(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return u(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:S.call(this,r,e,n))}}}function w(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0},[s,t]=r.r(e,n);return s?{error:!1,data:t,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:t}}function ge(e){return r=>w(r,e)}function xe(e,r){let n=w(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function N(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0};return!!r.r(e,n)[0]}function Oe(e){return r=>N(r,e)}function we(e){return{...i,e:"recursive",r(r,n){return e().r(r,n)}}}function ke(e,r){return{...i,e:`transform_from_${e.e}`,r(n,s){let[t,a]=e.r(n,s);return t?[!0,r(a)]:[!1,a]}}}function P(e,r){let n=(()=>{if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e})();return r&&(n==="string"||n==="number"||n==="boolean")?`${n}(${e})`:n}function me(e){if(e.error)throw new Error(`invalid input: ${e.errors.join(", ")}`)}function A(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function v(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("_")}function $e(e,r){try{let n=JSON.parse(e);return w(n,r)}catch(n){return{error:!0,errors:[`json parse error: ${A(n)?n.message:""}`]}}}function Ee(e){return typeof e=="function"}0&&(module.exports={rc_any,rc_array,rc_assert_is_valid,rc_boolean,rc_date,rc_extends_obj,rc_get_obj_schema,rc_instanceof,rc_is_valid,rc_literals,rc_loose_array,rc_loose_parse,rc_null,rc_number,rc_obj_intersection,rc_obj_omit,rc_obj_pick,rc_object,rc_parse,rc_parse_json,rc_parser,rc_record,rc_recursive,rc_rename_from_key,rc_rename_key,rc_strict_obj,rc_string,rc_transform,rc_tuple,rc_undefined,rc_union,rc_unknown,rc_validator,snakeCase});
1
+ "use strict";var $=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var M=(e,r)=>{for(var n in r)$(e,n,{get:r[n],enumerable:!0})},z=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of D(r))!U.call(e,t)&&t!==n&&$(e,t,{get:()=>r[t],enumerable:!(s=W(r,t))||s.enumerable});return e};var H=e=>z($({},"__esModule",{value:!0}),e);var Se={};M(Se,{rc_any:()=>ne,rc_array:()=>_e,rc_assert_is_valid:()=>$e,rc_boolean:()=>se,rc_date:()=>ie,rc_extends_obj:()=>le,rc_get_obj_schema:()=>pe,rc_instanceof:()=>ce,rc_is_valid:()=>v,rc_literals:()=>ue,rc_loose_array:()=>je,rc_loose_parse:()=>Oe,rc_loose_record:()=>he,rc_null:()=>re,rc_number:()=>ae,rc_obj_intersection:()=>de,rc_obj_omit:()=>be,rc_obj_pick:()=>Re,rc_object:()=>O,rc_parse:()=>k,rc_parse_json:()=>Ee,rc_parser:()=>xe,rc_record:()=>q,rc_recursive:()=>we,rc_rename_from_key:()=>V,rc_rename_key:()=>fe,rc_strict_obj:()=>Te,rc_string:()=>oe,rc_transform:()=>me,rc_tuple:()=>ge,rc_undefined:()=>ee,rc_union:()=>ye,rc_unknown:()=>te,rc_validator:()=>ke,snakeCase:()=>B});module.exports=H(Se);function J(e){return{...this,i:e}}function g(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function E(e,r){e.warnings.push(g(e,r))}function K(e,r){r.forEach(n=>E(e,n))}function u(e,r,n,s){if(e.c&&r===void 0)return[!0,r];if(e.u&&r==null)return[!0,r];if(e.y&&r===null)return[!0,r];let t=s();if(t&&(t===!0||!t.errors)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[`Predicate failed for type '${e.e}'`]]:[!0,o]}let a=e.i;if(a!==void 0)return E(n,`Fallback used, errors -> ${A(t,n,e,r)}`),[!0,Pe(a)?a():a];if(e.f&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[`Predicate failed for autofix in type '${e.e}'`]]:(E(n,`Autofixed from error "${A(t,n,e,r)}"`),[!0,o.fixed])}return[!1,t?t.errors:[e.l(r)]]}function A(e,r,n,s){return e?e.errors.map(t=>t.replace(r.path,"")).join("; "):n.l(s)}function L(e){return{...this,f:!0,o:e}}function Z(e){return{...this,t:e}}function G(){return{...this,c:!0}}function Q(e){return`Type '${P(e,!!this.p)}' is not assignable to '${this.e}'`}function X(){return{...this,y:!0,e:`${this.e}_or_null`}}function Y(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var i={withFallback:J,where:Z,optional:G,l:Q,orNullish:Y,withAutofix:L,orNull:X,i:void 0,t:void 0,c:!1,y:!1,u:!1,f:!1,p:!1,s:void 0,o:void 0,R:void 0,n:void 0,a:!1,T:!1,d:[]},ee={...i,r(e,r){return u(this,e,r,()=>e===void 0)},e:"undefined"},re={...i,r(e,r){return u(this,e,r,()=>e===null)},e:"null"},ne={...i,r(e,r){return u(this,e,r,()=>!0)},e:"any"},te={...i,r(e,r){return u(this,e,r,()=>!0)},e:"unknown"},se={...i,r(e,r){return u(this,e,r,()=>typeof e=="boolean")},e:"boolean"},oe={...i,r(e,r){return u(this,e,r,()=>typeof e=="string")},e:"string"},ae={...i,r(e,r){return u(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},ie={...i,r(e,r){return u(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function ce(e){return{...i,r(r,n){return u(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function ue(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return u(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},p:!0,e:e.length==1?P(e[0],!0):e.map(r=>P(r,!0)).join(" | ")}}var C=1;function ye(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return u(this,r,n,()=>{let s=n.path,t=[],a=0,o=!1,T=[],y=0;for(let c of e){y+=1,c.a&&(n.path=`${s}|union ${y}|`),n.objErrShortCircuit=!0,n.objErrKeyIndex=0;let[f,b]=c.r(r,n);n.objErrShortCircuit=!1;let l=n.objErrKeyIndex;if(n.objErrKeyIndex=0,f)return!0;c.a&&l!==-1?l>0?T.push(...b):(a<C&&t.push(...b),a+=1):o=!0}return n.path=s,T.length>0||t.length>0?((a>C||o)&&t.push("not matches any other union member"),{errors:[...T,...t],data:void 0}):!1})},e:e.map(r=>r.e).join(" | ")}}function V(e,r){return{...r,s:e}}var fe=V;function O(e,{normalizeKeysFrom:r}={}){return{...i,n:e,e:"object",a:!0,d:Object.entries(e),r(n,s){return u(this,n,s,()=>{if(!I(n))return s.objErrKeyIndex=-1,!1;let t=this.e==="strict_obj"?new Set(Object.keys(n)):void 0,a={},o=[],T=s.path,y=-1;for(let[c,f]of this.d){let b=c;y+=1;let l=`.${c}`;s.path=`${T}${l}`;let p,R=c;if(f.s&&(p=n[f.s],R=f.s),p===void 0&&(p=n[c],R=c),p===void 0&&r==="snake_case"){let h=B(c);p=n[h],R=h}t?.delete(R);let[j,x]=f.r(p,s);if(j)a[b]=x;else{for(let h of x)o.push(g(s,h));if(s.objErrShortCircuit){s.objErrKeyIndex=y;break}}}if(t&&t.size>0)for(let c of t)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o,data:void 0}:this.T?{errors:!1,data:{...n,...a}}:{errors:!1,data:a}})}}}function le(e){return{...O(e),e:"extends_object",T:!0}}function pe(e){return e.n}function Te(e){return{...O(e),e:"strict_obj"}}function de(...e){let r={};for(let n of e)Object.assign(r,n.n);return O(r)}function Re(e,r){let n={};if(!e.n)throw new Error("rc_obj_pick: obj must be an object type");for(let s of r){let t=e.n[s];t&&(n[s]=t)}return O(n)}function be(e,r){let n={};if(!e.n)throw new Error("rc_obj_omit: obj must be an object type");for(let s of Object.keys(e.n))r.includes(s)||(n[s]=e.n[s]);return O(n)}function q(e,{checkKey:r,looseCheck:n}={}){return{...i,e:`record<string, ${e.e}>`,r(s,t){return u(this,s,t,()=>{if(!I(s))return!1;let a={},o=[],T=t.path;for(let[y,c]of Object.entries(s)){let f=`.${y}`,b=`${T}${f}`;if(t.path=b,r&&!r(y)){o.push(g(t,`Key '${y}' is not allowed`));continue}let l=s[y],[p,R]=e.r(c,t);if(p)a[y]=l;else{let j=R;for(let x of j)o.push(g(t,x));if(t.objErrShortCircuit)break}}if(o.length>0)if(n)K(t,o);else return{errors:o,data:void 0};return{errors:!1,data:a}})}}}function he(e,{checkKey:r}={}){return q(e,{checkKey:r,looseCheck:!0})}function N(e,r){if(typeof r=="string"&&!e.n?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function S(e,r,n,s=!1,t){let a=[],o=[],T=new Set,y=n.path,c=Array.isArray(r),f=-1;for(let b of e){f++;let l=c?r[f]:r,p=`[${f}]`,R=`${y}${p}`;n.path=R;let j=l.r(b,n),[x,h]=j;n.path=R;let _=t?.unique;if(x&&_){let d=h,m=typeof _=="string";m?d=h[_]:typeof _=="function"&&(d=_(h)),T.has(d)?(m&&(n.path=`${y}${p}.${_}`),j=[!1,[g(n,m?`Type '${l.n?.[_]?.e}' with value "${d}" is not unique`:typeof _=="function"?`Type '${l.e}' unique fn return with value "${d}" is not unique`:`${l.e} value is not unique`)]]):T.add(d)}let[F,w]=j;if(F)o.push(w);else if(s){a.push(w.map(d=>g(n,d)));continue}else return{errors:w.map(d=>g(n,d)),data:void 0}}if(a.length>0){if(o.length===0)return{errors:a.slice(0,5).flat(),data:void 0};K(n,a.flat())}return{errors:!1,data:o}}function _e(e,r){return N(e,r?.unique),{...i,e:`${e.e}[]`,r(n,s){return u(this,n,s,()=>Array.isArray(n)?n.length===0?!0:S.call(this,n,e,s,!1,r):!1)}}}function je(e,r){return N(e,r?.unique),{...i,e:`${e.e}[]`,r(n,s){return u(this,n,s,()=>Array.isArray(n)?n.length===0?!0:S.call(this,n,e,s,!0,r):!1)}}}function ge(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return u(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:S.call(this,r,e,n))}}}function k(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0},[s,t]=r.r(e,n);return s?{error:!1,data:t,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:t}}function xe(e){return r=>k(r,e)}function Oe(e,r){let n=k(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function v(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0};return!!r.r(e,n)[0]}function ke(e){return r=>v(r,e)}function we(e){return{...i,e:"recursive",r(r,n){return e().r(r,n)}}}function me(e,r){return{...i,e:`transform_from_${e.e}`,r(n,s){let[t,a]=e.r(n,s);return t?[!0,r(a)]:[!1,a]}}}function P(e,r){let n=(()=>{if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e})();return r&&(n==="string"||n==="number"||n==="boolean")?`${n}(${e})`:n}function $e(e){if(e.error)throw new Error(`invalid input: ${e.errors.join(", ")}`)}function I(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function B(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("_")}function Ee(e,r){try{let n=JSON.parse(e);return k(n,r)}catch(n){return{error:!0,errors:[`json parse error: ${I(n)?n.message:""}`]}}}function Pe(e){return typeof e=="function"}0&&(module.exports={rc_any,rc_array,rc_assert_is_valid,rc_boolean,rc_date,rc_extends_obj,rc_get_obj_schema,rc_instanceof,rc_is_valid,rc_literals,rc_loose_array,rc_loose_parse,rc_loose_record,rc_null,rc_number,rc_obj_intersection,rc_obj_omit,rc_obj_pick,rc_object,rc_parse,rc_parse_json,rc_parser,rc_record,rc_recursive,rc_rename_from_key,rc_rename_key,rc_strict_obj,rc_string,rc_transform,rc_tuple,rc_undefined,rc_union,rc_unknown,rc_validator,snakeCase});
@@ -58,10 +58,14 @@ declare function rc_record<V extends RcType<any>>(valueType: V, { checkKey, loos
58
58
  checkKey?: (key: string) => boolean;
59
59
  looseCheck?: boolean;
60
60
  }): RcRecordType<V>;
61
+ /** instead of returning a general error, rejects invalid keys and returns warnings for these items */
62
+ declare function rc_loose_record<V extends RcType<any>>(valueType: V, { checkKey }?: {
63
+ checkKey?: (key: string) => boolean;
64
+ }): RcRecordType<V>;
61
65
  declare function rc_array<T extends RcType<any>>(type: T, options?: {
62
66
  unique: boolean | string | false | ((parsedItem: RcInferType<T>) => any);
63
67
  }): RcType<RcInferType<T>[]>;
64
- /** instead of returning a general erroro, rejects invalid array items and return warnings for these items */
68
+ /** instead of returning a general error, rejects invalid array items and returns warnings for these items */
65
69
  declare function rc_loose_array<T extends RcType<any>>(type: T, options?: {
66
70
  unique: boolean | string | false | ((parsedItem: RcInferType<T>) => any);
67
71
  }): RcType<RcInferType<T>[]>;
@@ -99,5 +103,10 @@ declare function rc_assert_is_valid<S>(result: RcParseResult<S>): asserts result
99
103
  warnings: string[] | false;
100
104
  };
101
105
  declare function rc_parse_json<T>(jsonString: string, schema: RcType<T>): RcParseResult<T>;
106
+ type Prettify<T> = T extends Record<string, any> ? {
107
+ [K in keyof T]: Prettify<T[K]>;
108
+ } : T;
109
+ type RcPrettyInferType<T extends RcType<any>> = Prettify<RcInferType<T>>;
110
+ type RcSchemaHasType<E, T extends E> = T;
102
111
 
103
- export { RcInferType, RcParseResult, RcParser, RcType, rc_any, rc_array, rc_assert_is_valid, rc_boolean, rc_date, rc_extends_obj, rc_get_obj_schema, rc_instanceof, rc_is_valid, rc_literals, rc_loose_array, rc_loose_parse, rc_null, rc_number, rc_obj_intersection, rc_obj_omit, rc_obj_pick, rc_object, rc_parse, rc_parse_json, rc_parser, rc_record, rc_recursive, rc_rename_from_key, rc_rename_key, rc_strict_obj, rc_string, rc_transform, rc_tuple, rc_undefined, rc_union, rc_unknown, rc_validator };
112
+ export { RcInferType, RcParseResult, RcParser, RcPrettyInferType, RcSchemaHasType, RcType, rc_any, rc_array, rc_assert_is_valid, rc_boolean, rc_date, rc_extends_obj, rc_get_obj_schema, rc_instanceof, rc_is_valid, rc_literals, rc_loose_array, rc_loose_parse, rc_loose_record, rc_null, rc_number, rc_obj_intersection, rc_obj_omit, rc_obj_pick, rc_object, rc_parse, rc_parse_json, rc_parser, rc_record, rc_recursive, rc_rename_from_key, rc_rename_key, rc_strict_obj, rc_string, rc_transform, rc_tuple, rc_undefined, rc_union, rc_unknown, rc_validator };
package/dist/runcheck.js CHANGED
@@ -1 +1 @@
1
- import{A,B,C,D,E,F,G,H,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}from"./chunk-YAXWQU2M.js";export{c as rc_any,v as rc_array,F as rc_assert_is_valid,e as rc_boolean,h as rc_date,o as rc_extends_obj,p as rc_get_obj_schema,i as rc_instanceof,B as rc_is_valid,j as rc_literals,w as rc_loose_array,A as rc_loose_parse,b as rc_null,g as rc_number,r as rc_obj_intersection,t as rc_obj_omit,s as rc_obj_pick,n as rc_object,y as rc_parse,H as rc_parse_json,z as rc_parser,u as rc_record,D as rc_recursive,l as rc_rename_from_key,m as rc_rename_key,q as rc_strict_obj,f as rc_string,E as rc_transform,x as rc_tuple,a as rc_undefined,k as rc_union,d as rc_unknown,C as rc_validator,G as snakeCase};
1
+ import{A,B,C,D,E,F,G,H,I,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}from"./chunk-3GG5XFSK.js";export{c as rc_any,w as rc_array,G as rc_assert_is_valid,e as rc_boolean,h as rc_date,o as rc_extends_obj,p as rc_get_obj_schema,i as rc_instanceof,C as rc_is_valid,j as rc_literals,x as rc_loose_array,B as rc_loose_parse,v as rc_loose_record,b as rc_null,g as rc_number,r as rc_obj_intersection,t as rc_obj_omit,s as rc_obj_pick,n as rc_object,z as rc_parse,I as rc_parse_json,A as rc_parser,u as rc_record,E as rc_recursive,l as rc_rename_from_key,m as rc_rename_key,q as rc_strict_obj,f as rc_string,F as rc_transform,y as rc_tuple,a as rc_undefined,k as rc_union,d as rc_unknown,D as rc_validator,H as snakeCase};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "runcheck",
3
3
  "description": "A tiny (less than 2 KiB Gzipped) and treeshakable! lib for typescript runtime type checks with autofix support",
4
- "version": "0.31.0",
4
+ "version": "0.33.0",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "test": "vitest --ui",
@@ -1 +0,0 @@
1
- function q(e){return{...this,i:e}}function g(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function m(e,r){e.warnings.push(g(e,r))}function C(e,r){r.forEach(n=>m(e,n))}function u(e,r,n,t){if(e.c&&r===void 0)return[!0,r];if(e.u&&r==null)return[!0,r];if(e.f&&r===null)return[!0,r];let s=t();if(s&&(s===!0||!s.errors)){let o=s===!0?r:s.data;return e.t&&!e.t(o)?[!1,[`Predicate failed for type '${e.e}'`]]:[!0,o]}let a=e.i;if(a!==void 0)return m(n,`Fallback used, errors -> ${A(s,n,e,r)}`),[!0,J(a)?a():a];if(e.l&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[`Predicate failed for autofix in type '${e.e}'`]]:(m(n,`Autofixed from error "${A(s,n,e,r)}"`),[!0,o.fixed])}return[!1,s?s.errors:[e.y(r)]]}function A(e,r,n,t){return e?e.errors.map(s=>s.replace(r.path,"")).join("; "):n.y(t)}function N(e){return{...this,l:!0,o:e}}function v(e){return{...this,t:e}}function B(){return{...this,c:!0}}function F(e){return`Type '${$(e,!!this.d)}' is not assignable to '${this.e}'`}function W(){return{...this,f:!0,e:`${this.e}_or_null`}}function D(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var i={withFallback:q,where:v,optional:B,y:F,orNullish:D,withAutofix:N,orNull:W,i:void 0,t:void 0,c:!1,f:!1,u:!1,l:!1,d:!1,s:void 0,o:void 0,R:void 0,n:void 0,a:!1,p:!1,T:[]},L={...i,r(e,r){return u(this,e,r,()=>e===void 0)},e:"undefined"},Z={...i,r(e,r){return u(this,e,r,()=>e===null)},e:"null"},G={...i,r(e,r){return u(this,e,r,()=>!0)},e:"any"},H={...i,r(e,r){return u(this,e,r,()=>!0)},e:"unknown"},Q={...i,r(e,r){return u(this,e,r,()=>typeof e=="boolean")},e:"boolean"},X={...i,r(e,r){return u(this,e,r,()=>typeof e=="string")},e:"string"},Y={...i,r(e,r){return u(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},ee={...i,r(e,r){return u(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function re(e){return{...i,r(r,n){return u(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function ne(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return u(this,r,n,()=>{for(let t of e)if(r===t)return!0;return!1})},d:!0,e:e.length==1?$(e[0],!0):e.map(r=>$(r,!0)).join(" | ")}}var I=1;function te(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return u(this,r,n,()=>{let t=n.path,s=[],a=0,o=!1,d=[],l=0;for(let c of e){l+=1,c.a&&(n.path=`${t}|union ${l}|`),n.objErrShortCircuit=!0,n.objErrKeyIndex=0;let[f,b]=c.r(r,n);n.objErrShortCircuit=!1;let y=n.objErrKeyIndex;if(n.objErrKeyIndex=0,f)return!0;c.a&&y!==-1?y>0?d.push(...b):(a<I&&s.push(...b),a+=1):o=!0}return n.path=t,d.length>0||s.length>0?((a>I||o)&&s.push("not matches any other union member"),{errors:[...d,...s],data:void 0}):!1})},e:e.map(r=>r.e).join(" | ")}}function U(e,r){return{...r,s:e}}var se=U;function O(e,{normalizeKeysFrom:r}={}){return{...i,n:e,e:"object",a:!0,T:Object.entries(e),r(n,t){return u(this,n,t,()=>{if(!S(n))return t.objErrKeyIndex=-1,!1;let s=this.e==="strict_obj"?new Set(Object.keys(n)):void 0,a={},o=[],d=t.path,l=-1;for(let[c,f]of this.T){let b=c;l+=1;let y=`.${c}`;t.path=`${d}${y}`;let p,R=c;if(f.s&&(p=n[f.s],R=f.s),p===void 0&&(p=n[c],R=c),p===void 0&&r==="snake_case"){let h=z(c);p=n[h],R=h}s?.delete(R);let[j,x]=f.r(p,t);if(j)a[b]=x;else{for(let h of x)o.push(g(t,h));if(t.objErrShortCircuit){t.objErrKeyIndex=l;break}}}if(s&&s.size>0)for(let c of s)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o,data:void 0}:this.p?{errors:!1,data:{...n,...a}}:{errors:!1,data:a}})}}}function oe(e){return{...O(e),e:"extends_object",p:!0}}function ae(e){return e.n}function ie(e){return{...O(e),e:"strict_obj"}}function ce(...e){let r={};for(let n of e)Object.assign(r,n.n);return O(r)}function ue(e,r){let n={};if(!e.n)throw new Error("rc_obj_pick: obj must be an object type");for(let t of r){let s=e.n[t];s&&(n[t]=s)}return O(n)}function le(e,r){let n={};if(!e.n)throw new Error("rc_obj_omit: obj must be an object type");for(let t of Object.keys(e.n))r.includes(t)||(n[t]=e.n[t]);return O(n)}function fe(e,{checkKey:r,looseCheck:n}={}){return{...i,e:`record<string, ${e.e}>`,r(t,s){return u(this,t,s,()=>{if(!S(t))return!1;let a={},o=[],d=s.path;for(let[l,c]of Object.entries(t)){let f=`.${l}`,b=`${d}${f}`;if(s.path=b,r&&!r(l)){o.push(g(s,`Key '${l}' is not allowed`));continue}let y=t[l],[p,R]=e.r(c,s);if(p)a[l]=y;else{let j=R;for(let x of j)o.push(g(s,x));if(s.objErrShortCircuit)break}}if(o.length>0)if(n)C(s,o);else return{errors:o,data:void 0};return{errors:!1,data:a}})}}}function K(e,r){if(typeof r=="string"&&!e.n?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function E(e,r,n,t=!1,s){let a=[],o=[],d=new Set,l=n.path,c=Array.isArray(r),f=-1;for(let b of e){f++;let y=c?r[f]:r,p=`[${f}]`,R=`${l}${p}`;n.path=R;let j=y.r(b,n),[x,h]=j;n.path=R;let _=s?.unique;if(x&&_){let T=h,k=typeof _=="string";k?T=h[_]:typeof _=="function"&&(T=_(h)),d.has(T)?(k&&(n.path=`${l}${p}.${_}`),j=[!1,[g(n,k?`Type '${y.n?.[_]?.e}' with value "${T}" is not unique`:typeof _=="function"?`Type '${y.e}' unique fn return with value "${T}" is not unique`:`${y.e} value is not unique`)]]):d.add(T)}let[V,w]=j;if(V)o.push(w);else if(t){a.push(w.map(T=>g(n,T)));continue}else return{errors:w.map(T=>g(n,T)),data:void 0}}if(a.length>0){if(o.length===0)return{errors:a.slice(0,5).flat(),data:void 0};C(n,a.flat())}return{errors:!1,data:o}}function ye(e,r){return K(e,r?.unique),{...i,e:`${e.e}[]`,r(n,t){return u(this,n,t,()=>Array.isArray(n)?n.length===0?!0:E.call(this,n,e,t,!1,r):!1)}}}function pe(e,r){return K(e,r?.unique),{...i,e:`${e.e}[]`,r(n,t){return u(this,n,t,()=>Array.isArray(n)?n.length===0?!0:E.call(this,n,e,t,!0,r):!1)}}}function de(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return u(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:E.call(this,r,e,n))}}}function P(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0},[t,s]=r.r(e,n);return t?{error:!1,data:s,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:s}}function Te(e){return r=>P(r,e)}function Re(e,r){let n=P(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function M(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0};return!!r.r(e,n)[0]}function be(e){return r=>M(r,e)}function he(e){return{...i,e:"recursive",r(r,n){return e().r(r,n)}}}function _e(e,r){return{...i,e:`transform_from_${e.e}`,r(n,t){let[s,a]=e.r(n,t);return s?[!0,r(a)]:[!1,a]}}}function $(e,r){let n=(()=>{if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e})();return r&&(n==="string"||n==="number"||n==="boolean")?`${n}(${e})`:n}function je(e){if(e.error)throw new Error(`invalid input: ${e.errors.join(", ")}`)}function S(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function z(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("_")}function ge(e,r){try{let n=JSON.parse(e);return P(n,r)}catch(n){return{error:!0,errors:[`json parse error: ${S(n)?n.message:""}`]}}}function J(e){return typeof e=="function"}export{L as a,Z as b,G as c,H as d,Q as e,X as f,Y as g,ee as h,re as i,ne as j,te as k,U as l,se as m,O as n,oe as o,ae as p,ie as q,ce as r,ue as s,le as t,fe as u,ye as v,pe as w,de as x,P as y,Te as z,Re as A,M as B,be as C,he as D,_e as E,je as F,z as G,ge as H};