runcheck 0.30.0 → 0.32.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 +49 -17
- package/dist/autofixable.cjs +1 -1
- package/dist/autofixable.js +1 -1
- package/dist/chunk-WQBKLM3G.js +1 -0
- package/dist/runcheck.cjs +1 -1
- package/dist/runcheck.d.ts +7 -2
- package/dist/runcheck.js +1 -1
- package/package.json +3 -4
- package/dist/chunk-2VUA7VF5.js +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Runcheck
|
|
2
2
|
|
|
3
|
-
A lib for js/typescript runtime type checks with autofix support. Runcheck has the goal of being very lightweight and fast ⚡. Because of that, it has only around [
|
|
3
|
+
A lib for js/typescript runtime type checks with autofix support. Runcheck has the goal of being very lightweight and fast ⚡. Because of that, it has only around [2.9kb Gzipped](https://bundlephobia.com/package/runcheck) (at v0.30), has no dependencies and is tree-shakeable!
|
|
4
4
|
|
|
5
5
|
Benchmarks:
|
|
6
6
|
|
|
@@ -252,7 +252,7 @@ const positiveNumberType = rc_number.where((input) => input > 0)
|
|
|
252
252
|
|
|
253
253
|
# Infer types from schemas
|
|
254
254
|
|
|
255
|
-
You can use `RcInferType<typeof
|
|
255
|
+
You can use `RcInferType<typeof schema>` to infer the types from a schema.
|
|
256
256
|
|
|
257
257
|
```ts
|
|
258
258
|
const schema = rc_object({
|
|
@@ -264,15 +264,17 @@ const schema = rc_object({
|
|
|
264
264
|
export type Person = RcInferType<typeof schema>
|
|
265
265
|
```
|
|
266
266
|
|
|
267
|
+
You can also use the `RcPrettyInferType<typeof schema>` to get a more readable type.
|
|
268
|
+
|
|
267
269
|
# Type modifiers
|
|
268
270
|
|
|
269
|
-
You can use also modiers like `rc_string.optional()` to extend
|
|
271
|
+
You can use also modiers like `rc_string.optional()` to extend the rc types:
|
|
270
272
|
|
|
271
|
-
| runcheck modifier
|
|
272
|
-
|
|
|
273
|
-
| `rc_[type].optional()`
|
|
274
|
-
| `rc_[type].
|
|
275
|
-
| `rc_[type].
|
|
273
|
+
| runcheck modifier | ts type equivalent |
|
|
274
|
+
| ----------------------- | ------------------------ |
|
|
275
|
+
| `rc_[type].optional()` | `T \| undefined` |
|
|
276
|
+
| `rc_[type].orNull()` | `T \| null` |
|
|
277
|
+
| `rc_[type].orNullish()` | `T \| null \| undefined` |
|
|
276
278
|
|
|
277
279
|
# Recursive types
|
|
278
280
|
|
|
@@ -285,14 +287,12 @@ type MenuTree = {
|
|
|
285
287
|
}
|
|
286
288
|
|
|
287
289
|
// the type should be provided manually to the variable in this case
|
|
288
|
-
const menuTreeSchema: RcType<MenuTree[]> =
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}),
|
|
295
|
-
),
|
|
290
|
+
const menuTreeSchema: RcType<MenuTree[]> = rc_array(
|
|
291
|
+
rc_object({
|
|
292
|
+
name: rc_string,
|
|
293
|
+
// you can safely autorefence the schema here
|
|
294
|
+
children: rc_recursive(() => menuTreeSchema),
|
|
295
|
+
}),
|
|
296
296
|
)
|
|
297
297
|
|
|
298
298
|
const result = rc_parse(input, menuTreeSchema)
|
|
@@ -402,4 +402,36 @@ const shape = rc_object({
|
|
|
402
402
|
const baseSchema = rc_obj_omit(shape, ['isCool'])
|
|
403
403
|
```
|
|
404
404
|
|
|
405
|
-
#
|
|
405
|
+
# Investigage rc nested type errors
|
|
406
|
+
|
|
407
|
+
There are two type utils to help investigate deeply nested type errors, the `RcPrettyInferType` and the `RcSchemaHasType`
|
|
408
|
+
|
|
409
|
+
```ts
|
|
410
|
+
const schema = rc_object({
|
|
411
|
+
level1: {
|
|
412
|
+
level2: {
|
|
413
|
+
level3: {
|
|
414
|
+
level4: {
|
|
415
|
+
level5: rc_string,
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
type SchemaType = {
|
|
423
|
+
level1: {
|
|
424
|
+
level2: {
|
|
425
|
+
level3: {
|
|
426
|
+
level4: {
|
|
427
|
+
level5: number
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
type CheckType = RcSchemaHasType<SchemaType, RcPrettyInferType<typeof schema>>
|
|
435
|
+
// this will reporte better errors than:
|
|
436
|
+
// const schema: RcType<SchemaType> = rc_object...
|
|
437
|
+
```
|
package/dist/autofixable.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var u=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var
|
|
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 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 -> ${y(t,n,e,r)}`),[!0,S(c)?c():c];if(e.y&&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 "${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,o: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 o={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,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"},l={...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 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});
|
package/dist/autofixable.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{e as f,f as t,g as o}from"./chunk-
|
|
1
|
+
import{e as f,f as t,g as o}from"./chunk-WQBKLM3G.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,H(a)?a():a];if(e.y&&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 "${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,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,l:F,orNullish:D,withAutofix:N,orNull:W,i:void 0,t:void 0,c:!1,f:!1,u:!1,y:!1,d:!1,s:void 0,o:void 0,R:void 0,n:void 0,a:!1,p:!1,T:[]},J={...i,r(e,r){return u(this,e,r,()=>e===void 0)},e:"undefined"},L={...i,r(e,r){return u(this,e,r,()=>e===null)},e:"null"},Z={...i,r(e,r){return u(this,e,r,()=>!0)},e:"any"},G={...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 A=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,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,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=[],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.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=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 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 ye(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=[],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 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,k=typeof _=="string";k?d=h[_]:typeof _=="function"&&(d=_(h)),T.has(d)?(k&&(n.path=`${y}${p}.${_}`),j=[!1,[g(n,k?`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,w]=j;if(V)o.push(w);else if(t){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};C(n,a.flat())}return{errors:!1,data:o}}function le(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 Te(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 de(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 H(e){return typeof e=="function"}export{J as a,L as b,Z as c,G 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,ye as t,fe as u,le as v,pe as w,Te as x,P as y,de 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};
|
package/dist/runcheck.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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:()=>le,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:()=>Te,rc_obj_omit:()=>Re,rc_obj_pick:()=>de,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:()=>ye,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 H(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,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 "${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 J(e){return{...this,f:!0,o:e}}function L(e){return{...this,t:e}}function Z(){return{...this,c:!0}}function G(e){return`Type '${P(e,!!this.p)}' is not assignable to '${this.e}'`}function Q(){return{...this,y:!0,e:`${this.e}_or_null`}}function X(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var i={withFallback:H,where:L,optional:Z,l:G,orNullish:X,withAutofix:J,orNull:Q,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:[]},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,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 ye=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=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=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 fe(e){return{...O(e),e:"extends_object",T:!0}}function le(e){return e.n}function pe(e){return{...O(e),e:"strict_obj"}}function Te(...e){let r={};for(let n of e)Object.assign(r,n.n);return O(r)}function de(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(!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 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=[],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[B,k]=j;if(B)o.push(k);else if(s){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};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 I(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: ${I(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});
|
package/dist/runcheck.d.ts
CHANGED
|
@@ -90,7 +90,7 @@ declare function rc_loose_parse<S>(input: any, type: RcType<S>): {
|
|
|
90
90
|
};
|
|
91
91
|
declare function rc_is_valid<S>(input: any, type: RcType<S>): input is S;
|
|
92
92
|
declare function rc_validator<S>(type: RcType<S>): (input: any) => input is S;
|
|
93
|
-
declare function rc_recursive(type: () => RcType<
|
|
93
|
+
declare function rc_recursive<T>(type: () => RcType<T>): RcType<T>;
|
|
94
94
|
/** validate a input or subset of input and transform the valid result */
|
|
95
95
|
declare function rc_transform<Input, Transformed>(type: RcType<Input>, transform: (input: Input) => Transformed): RcType<Transformed>;
|
|
96
96
|
declare function rc_assert_is_valid<S>(result: RcParseResult<S>): asserts result is {
|
|
@@ -99,5 +99,10 @@ declare function rc_assert_is_valid<S>(result: RcParseResult<S>): asserts result
|
|
|
99
99
|
warnings: string[] | false;
|
|
100
100
|
};
|
|
101
101
|
declare function rc_parse_json<T>(jsonString: string, schema: RcType<T>): RcParseResult<T>;
|
|
102
|
+
type Prettify<T> = T extends Record<string, any> ? {
|
|
103
|
+
[K in keyof T]: Prettify<T[K]>;
|
|
104
|
+
} : T;
|
|
105
|
+
type RcPrettyInferType<T extends RcType<any>> = Prettify<RcInferType<T>>;
|
|
106
|
+
type RcSchemaHasType<E, T extends E> = T;
|
|
102
107
|
|
|
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 };
|
|
108
|
+
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_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-
|
|
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-WQBKLM3G.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};
|
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.
|
|
4
|
+
"version": "0.32.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "vitest --ui",
|
|
@@ -59,9 +59,8 @@
|
|
|
59
59
|
"tsm": "^2.3.0",
|
|
60
60
|
"tsup": "^6.7.0",
|
|
61
61
|
"typescript": "^5.0.3",
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"vitest": "^0.30.0",
|
|
62
|
+
"vite": "^4.3.9",
|
|
63
|
+
"vitest": "^0.31.0",
|
|
65
64
|
"zod": "^3.21.4"
|
|
66
65
|
}
|
|
67
66
|
}
|
package/dist/chunk-2VUA7VF5.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function C(e){return{...this,c:e}}function j(e,r){return r.startsWith("$[")||r.startsWith("$.")?r:`${e.path?`$${e.path}: `:""}${r}`}function k(e,r){e.warnings.push(j(e,r))}function P(e,r){r.forEach(n=>k(e,n))}function u(e,r,n,t){if(e.u&&r===void 0)return[!0,r];if(e.y&&r==null)return[!0,r];if(e.f&&r===null)return[!0,r];let s=t();if(s&&(s===!0||"data"in s)){let o=s===!0?r:s.data;return e.s&&!e.s(o)?[!1,[e.t(o)]]:[!0,o]}let a=e.c;if(a!==void 0)return k(n,`Fallback used, ${e.t(r)}`),[!0,U(a)?a():a];if(e.l&&e.a){let o=e.a(r);if(o)return e.s&&!e.s(o.fixed)?[!1,[e.t(o.fixed)]]:(k(n,`Autofixed from error "${e.t(r)}"`),[!0,o.fixed])}return[!1,s&&"errors"in s?s.errors:[e.t(r)]]}function K(e){return{...this,l:!0,a:e}}function q(e){return{...this,s:e,e:`${this.e}_with_predicate`}}function N(){return{...this,u:!0}}function V(e){return`Type '${m(e,!!this.p)}' is not assignable to '${this.e}'`}function v(){return{...this,f:!0,e:`${this.e}_or_null`}}function B(){return{...this,y:!0,e:`${this.e}_or_nullish`}}var i={withFallback:C,where:q,optional:N,t:V,orNullish:B,withAutofix:K,orNull:v},M={...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"},J={...i,r(e,r){return u(this,e,r,()=>!0)},e:"any"},L={...i,r(e,r){return u(this,e,r,()=>!0)},e:"unknown"},Z={...i,r(e,r){return u(this,e,r,()=>typeof e=="boolean")},e:"boolean"},G={...i,r(e,r){return u(this,e,r,()=>typeof e=="string")},e:"string"},H={...i,r(e,r){return u(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},Q={...i,r(e,r){return u(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function X(e){return{...i,r(r,n){return u(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function Y(...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})},p:!0,e:e.length==1?m(e[0],!0):e.map(r=>m(r,!0)).join(" | ")}}var A=1;function ee(...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,p=[],y=0;for(let c of e){y+=1,c.i&&(n.path=`${t}|union ${y}|`),n.objErrShortCircuit=!0,n.objErrKeyIndex=0;let[d,T]=c.r(r,n);n.objErrShortCircuit=!1;let b=n.objErrKeyIndex;if(n.objErrKeyIndex=0,d)return!0;c.i&&b!==-1?b>0?p.push(...T):(a<A&&s.push(...T),a+=1):o=!0}return n.path=t,p.length>0||s.length>0?((a>A||o)&&s.push("not matches any other union member"),{errors:[...p,...s]}):!1})},e:e.map(r=>r.e).join(" | ")}}function F(e,r){return{...r,o:e}}var re=F;function O(e,{normalizeKeysFrom:r}={}){return{...i,n:e,e:"object",i:!0,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=[],p=t.path,y=-1;for(let[c,d]of Object.entries(e)){let T=c;y+=1;let b=`.${c}`;t.path=`${p}${b}`;let f,h=c;if(d.o&&(f=n[d.o],h=d.o),f===void 0&&(f=n[c],h=c),f===void 0&&r==="snake_case"){let g=D(c);f=n[g],h=g}s?.delete(h);let[_,l]=d.r(f,t);if(_)a[T]=l;else{let g=l;for(let x of g)o.push(j(t,x));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}:this.e.startsWith("extends_object")?{data:{...n,...a}}:{data:a}})}}}function ne(e){return{...O(e),e:"extends_object"}}function te(e){return e.n}function se(e){return{...O(e),e:"strict_obj"}}function oe(...e){let r={};for(let n of e)Object.assign(r,n.n);return O(r)}function ae(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 ie(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 ce(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=[],p=s.path;for(let[y,c]of Object.entries(t)){let d=`.${y}`,T=`${p}${d}`;if(s.path=T,r&&!r(y)){o.push(j(s,`Key '${y}' is not allowed`));continue}let b=t[y],[f,h]=e.r(c,s);if(f)a[y]=b;else{let _=h;for(let l of _)o.push(j(s,l));if(s.objErrShortCircuit)break}}if(o.length>0)if(n)P(s,o);else return{errors:o};return{data:a}})}}}function I(e,r){if(typeof r=="string"&&!e.n?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function $(e,r,n,t=!1,s){let a=-1,o=[],p=[],y=new Set,c=n.path;for(let d of e){a++;let T=Array.isArray(r)?r[a]:r,b=`[${a}]`;n.path=`${c}${b}`;let f=T.r(d,n),[h,_]=f,l=s?.unique;if(h&&l){let R=_,w=typeof l=="string";w?R=_[l]:typeof l=="function"&&(R=l(_)),y.has(R)?(w&&(n.path=`${c}${b}.${l}`),f=[!1,[j(n,w?`Type '${T.n?.[l]?.e}' with value "${R}" is not unique`:typeof l=="function"?`Type '${T.e}' unique fn return with value "${R}" is not unique`:`${T.e} value is not unique`)]]):y.add(R)}let[g,x]=f;if(g)p.push(x);else if(t){o.push(x.map(R=>j(n,R)));continue}else return{errors:x.map(R=>j(n,R))}}if(o.length>0){if(p.length===0)return{errors:o.slice(0,5).flat()};P(n,o.flat())}return{data:p}}function ue(e,r){return I(e,r?.unique),{...i,e:`${e.e}[]`,r(n,t){return u(this,n,t,()=>Array.isArray(n)?n.length===0?!0:$.call(this,n,e,t,!1,r):!1)}}}function ye(e,r){return I(e,r?.unique),{...i,e:`${e.e}[]`,r(n,t){return u(this,n,t,()=>Array.isArray(n)?n.length===0?!0:$.call(this,n,e,t,!0,r):!1)}}}function le(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:$.call(this,r,e,n))}}}function E(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 fe(e){return r=>E(r,e)}function pe(e,r){let n=E(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function W(e,r){let n={warnings:[],path:"",objErrShortCircuit:!1,objErrKeyIndex:0};return!!r.r(e,n)[0]}function Te(e){return r=>W(r,e)}function de(e){return{...i,e:"recursive",r(r,n){return e().r(r,n)}}}function Re(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 m(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 be(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 D(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map(r=>r.toLowerCase()).join("_")}function he(e,r){try{let n=JSON.parse(e);return E(n,r)}catch(n){return{error:!0,errors:[`json parse error: ${S(n)?n.message:""}`]}}}function U(e){return typeof e=="function"}export{M as a,z as b,J as c,L as d,Z as e,G as f,H as g,Q as h,X as i,Y as j,ee as k,F as l,re as m,O as n,ne as o,te as p,se as q,oe as r,ae as s,ie as t,ce as u,ue as v,ye as w,le as x,E as y,fe as z,pe as A,W as B,Te as C,de as D,Re as E,be as F,D as G,he as H};
|