runcheck 0.11.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -104,6 +104,18 @@ const shape = rc_record(rc_number)
104
104
  // shape type is `Record<string, number>`
105
105
  ```
106
106
 
107
+ ## `rc_rename_key`
108
+
109
+ Allows to rename a key in a object. Example:
110
+
111
+ ```ts
112
+ const shape = rc_object({
113
+ name: rc_rename_key('oldName', rc_string), // oldName will be renamed to name
114
+ age: rc_number,
115
+ isCool: rc_boolean,
116
+ })
117
+ ```
118
+
107
119
  # Parsing
108
120
 
109
121
  ```ts
@@ -120,7 +132,7 @@ if (parseResult.error) {
120
132
 
121
133
  const result = parseResult.data
122
134
  // Do something with result
123
- ```
135
+ ````
124
136
 
125
137
  You can also use `rc_parser` to create a reusable parser.
126
138
 
@@ -247,3 +259,15 @@ const menuTreeSchema: RcType<MenuTree[]> = rc_recursive(() =>
247
259
 
248
260
  const result = rc_parse(input, menuTreeSchema)
249
261
  ```
262
+
263
+ # Transform types
264
+
265
+ You can use `rc_transform` to validate an input and transform it to another data.
266
+
267
+ ```ts
268
+ const input = 'hello'
269
+
270
+ const result = rc_parse(input, rc_transform(rc_string, (input) => input.length))
271
+
272
+
273
+ ```
@@ -1 +1 @@
1
- "use strict";var c=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(e,r)=>{for(var a in r)c(e,a,{get:r[a],enumerable:!0})},R=(e,r,a,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of T(r))!d.call(e,n)&&n!==a&&c(e,n,{get:()=>r[n],enumerable:!(i=f(r,n))||i.enumerable});return e};var b=e=>R(c({},"__esModule",{value:!0}),e);var S={};p(S,{rc_boolean_autofix:()=>O,rc_number_autofix:()=>A,rc_string_autofix:()=>k});module.exports=b(S);function h(e){return{...this,o:e}}function s(e,r,a,i){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let n=i();if(n&&(n===!0||"data"in n)){let t=n===!0?r:n.data;return e.t&&!e.t(t)?[!1,[e.n(t)]]:[!0,t]}if(e.o!==void 0)return a.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.o];if(e.l&&e.a){let t=e.a(r);if(t)return e.t&&!e.t(t.fixed)?[!1,[e.n(t.fixed)]]:(a.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,t.fixed])}return[!1,n&&"errors"in n?n.errors:[e.n(r)]]}function _(e){return{...this,l:!0,a:e}}function x(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function g(){return{...this,i:!0}}function w(e){return`Type '${m(e)}' is not assignable to '${this.e}'`}function j(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function $(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var o={withFallback:h,where:x,optional:g,n:w,nullable:j,withAutofix:_,nullish:$},P={...o,r(e,r){return s(this,e,r,()=>e===void 0)},e:"undefined"},V={...o,r(e,r){return s(this,e,r,()=>e===null)},e:"null"},q={...o,r(e,r){return s(this,e,r,()=>!0)},e:"any"},E={...o,r(e,r){return s(this,e,r,()=>!0)},e:"unknown"},u={...o,r(e,r){return s(this,e,r,()=>typeof e=="boolean")},e:"boolean"},l={...o,r(e,r){return s(this,e,r,()=>typeof e=="string")},e:"string"},y={...o,r(e,r){return s(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},I={...o,r(e,r){return s(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function m(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}var O=u.withAutofix(e=>e==null||e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),k=l.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),A=y.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 c=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(e,r)=>{for(var a in r)c(e,a,{get:r[a],enumerable:!0})},R=(e,r,a,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of T(r))!d.call(e,n)&&n!==a&&c(e,n,{get:()=>r[n],enumerable:!(i=f(r,n))||i.enumerable});return e};var _=e=>R(c({},"__esModule",{value:!0}),e);var S={};p(S,{rc_boolean_autofix:()=>k,rc_number_autofix:()=>A,rc_string_autofix:()=>O});module.exports=_(S);function h(e){return{...this,a:e}}function s(e,r,a,i){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 n=i();if(n&&(n===!0||"data"in n)){let t=n===!0?r:n.data;return e.t&&!e.t(t)?[!1,[e.n(t)]]:[!0,t]}if(e.a!==void 0)return a.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.a];if(e.y&&e.i){let t=e.i(r);if(t)return e.t&&!e.t(t.fixed)?[!1,[e.n(t.fixed)]]:(a.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,t.fixed])}return[!1,n&&"errors"in n?n.errors:[e.n(r)]]}function x(e){return{...this,y:!0,i:e}}function b(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function g(){return{...this,c:!0}}function w(e){return`Type '${$(e)}' is not assignable to '${this.e}'`}function m(){return{...this,l:!0,e:`${this.e}_or_nullable`}}function j(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var o={withFallback:h,where:b,optional:g,n:w,nullable:m,withAutofix:x,nullish:j},P={...o,r(e,r){return s(this,e,r,()=>e===void 0)},e:"undefined"},V={...o,r(e,r){return s(this,e,r,()=>e===null)},e:"null"},q={...o,r(e,r){return s(this,e,r,()=>!0)},e:"any"},E={...o,r(e,r){return s(this,e,r,()=>!0)},e:"unknown"},u={...o,r(e,r){return s(this,e,r,()=>typeof e=="boolean")},e:"boolean"},l={...o,r(e,r){return s(this,e,r,()=>typeof e=="string")},e:"string"},y={...o,r(e,r){return s(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},I={...o,r(e,r){return s(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function $(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}var k=u.withAutofix(e=>e==null||e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),O=l.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),A=y.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-NRGTHT24.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-FAF7WEYO.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 k(e){return{...this,a:e}}function i(e,r,n,t){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 a=t();if(a&&(a===!0||"data"in a)){let s=a===!0?r:a.data;return e.t&&!e.t(s)?[!1,[e.n(s)]]:[!0,s]}if(e.a!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.a];if(e.y&&e.i){let s=e.i(r);if(s)return e.t&&!e.t(s.fixed)?[!1,[e.n(s.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,s.fixed])}return[!1,a&&"errors"in a?a.errors:[e.n(r)]]}function O(e){return{...this,y:!0,i:e}}function m(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function A(){return{...this,c:!0}}function S(e){return`Type '${j(e)}' is not assignable to '${this.e}'`}function P(){return{...this,l:!0,e:`${this.e}_or_nullable`}}function V(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var o={withFallback:k,where:m,optional:A,n:S,nullable:P,withAutofix:O,nullish:V},E={...o,r(e,r){return i(this,e,r,()=>e===void 0)},e:"undefined"},I={...o,r(e,r){return i(this,e,r,()=>e===null)},e:"null"},N={...o,r(e,r){return i(this,e,r,()=>!0)},e:"any"},v={...o,r(e,r){return i(this,e,r,()=>!0)},e:"unknown"},K={...o,r(e,r){return i(this,e,r,()=>typeof e=="boolean")},e:"boolean"},F={...o,r(e,r){return i(this,e,r,()=>typeof e=="string")},e:"string"},C={...o,r(e,r){return i(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},U={...o,r(e,r){return i(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function z(e){return{...o,r(r,n){return i(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function B(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(r===t)return!0;return!1})},e:e.length==1?`${j(e[0])}_literal`:"literals"}}function D(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(t.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function M(e,r){return{...r,o:e}}function p(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",t]=e.split(": "),a=n.slice(1);return`$${r}${a}: ${t}`}return`$${r}: ${e}`}function g(e){return{...o,s:e,e:"object",f:!0,r(r,n){return i(this,r,n,()=>{if(!$(r))return!1;let t=new Set(Object.keys(r)),a={},s=[];for(let[c,l]of Object.entries(e)){let d=c,u,y=c;l.o?(u=r[l.o],y=l.o,u===void 0&&(u=r[c],y=c)):u=r[c],t.delete(y);let[f,R]=l.r(u,n);if(f)a[d]=R;else{let h=R;for(let T of h)s.push(p(T,`.${c}`))}}if(this.e==="strict_obj"&&t.size>0)for(let c of t)s.push(`Key '${c}' is not defined in the object shape`);return s.length>0?{errors:s}:{data:a}})}}}function W(e){return{...g(e),e:"strict_obj"}}function G(e,r){return g({...e.s,...r.s})}function H(e){return{...o,e:`record<string, ${e.e}>`,r(r,n){return i(this,r,n,()=>{if(!$(r))return!1;let t={},a=[];for(let[s,c]of Object.entries(r)){let l=r[s],[d,u]=e.r(c,n);if(d)t[s]=l;else{let y=u;for(let f of y)a.push(p(f,`.${s}`))}}return a.length>0?{errors:a}:{data:t}})}}}function x(e,r){if(typeof r=="string"&&!e.s?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function _(e,r,n,t=!1,a){let s=-1,c=[],l=new Set;for(let d of e){s++;let u=Array.isArray(r)?r[s]:r,y=u.r(d,n),f=a?.unique;if(y[0]&&f){let T=d,b=typeof f=="string";b&&(T=d[f]),l.has(T)?y=[!1,[b?p(`${u.s?.[f]?.e} value is not unique`,`.${f}`):`${u.e} value is not unique`]]:l.add(T)}let[R,h]=y;if(R)t&&c.push(d);else if(t){n.warnings.push(...h.map(T=>p(T,`[${s}]`)));continue}else return{errors:h.map(T=>p(T,`[${s}]`))}}return t?{data:c}:!0}function J(e,r){return x(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:_.call(this,n,e,t,!1,r):!1)}}}function L(e,r){return x(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:_.call(this,n,e,t,!0,r):!1)}}}function Q(e){return{...o,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return i(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:_.call(this,r,e,n))}}}function w(e,r){let n={warnings:[]},[t,a]=r.r(e,n);return t?{error:!1,data:a,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:a}}function X(e){return r=>w(r,e)}function Y(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 q(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function Z(e){return r=>q(r,e)}function ee(e){return{...o,e:"recursive",r(r,n){return e().r(r,n)}}}function re(e,r){return{...o,e:e.e,r(n,t){let[a,s]=e.r(n,t);return a?[!0,r(s)]:[!1,s]}}}function j(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function $(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}export{E as a,I as b,N as c,v as d,K as e,F as f,C as g,U as h,z as i,B as j,D as k,M as l,g as m,W as n,G as o,H as p,J as q,L as r,Q as s,w as t,X as u,Y as v,q as w,Z as x,ee as y,re as z};
package/dist/runcheck.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var _=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var P=(e,r)=>{for(var n in r)_(e,n,{get:r[n],enumerable:!0})},V=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of m(r))!S.call(e,s)&&s!==n&&_(e,s,{get:()=>r[s],enumerable:!(t=A(r,s))||t.enumerable});return e};var q=e=>V(_({},"__esModule",{value:!0}),e);var ce={};P(ce,{rc_any:()=>B,rc_array:()=>re,rc_boolean:()=>W,rc_date:()=>H,rc_extends_obj:()=>X,rc_instanceof:()=>J,rc_is_valid:()=>$,rc_literals:()=>L,rc_loose_array:()=>ne,rc_loose_parse:()=>ae,rc_null:()=>z,rc_number:()=>G,rc_obj_intersection:()=>Z,rc_object:()=>h,rc_parse:()=>g,rc_parser:()=>se,rc_record:()=>ee,rc_recursive:()=>ie,rc_strict_obj:()=>Y,rc_string:()=>D,rc_tuple:()=>te,rc_undefined:()=>U,rc_union:()=>Q,rc_unknown:()=>M,rc_validator:()=>oe});module.exports=q(ce);function E(e){return{...this,a:e}}function i(e,r,n,t){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let s=t();if(s&&(s===!0||"data"in s)){let a=s===!0?r:s.data;return e.t&&!e.t(a)?[!1,[e.n(a)]]:[!0,a]}if(e.a!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.a];if(e.l&&e.o){let a=e.o(r);if(a)return e.t&&!e.t(a.fixed)?[!1,[e.n(a.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,a.fixed])}return[!1,s&&"errors"in s?s.errors:[e.n(r)]]}function I(e){return{...this,l:!0,o:e}}function N(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function v(){return{...this,i:!0}}function K(e){return`Type '${O(e)}' is not assignable to '${this.e}'`}function F(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function C(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var o={withFallback:E,where:N,optional:v,n:K,nullable:F,withAutofix:I,nullish:C},U={...o,r(e,r){return i(this,e,r,()=>e===void 0)},e:"undefined"},z={...o,r(e,r){return i(this,e,r,()=>e===null)},e:"null"},B={...o,r(e,r){return i(this,e,r,()=>!0)},e:"any"},M={...o,r(e,r){return i(this,e,r,()=>!0)},e:"unknown"},W={...o,r(e,r){return i(this,e,r,()=>typeof e=="boolean")},e:"boolean"},D={...o,r(e,r){return i(this,e,r,()=>typeof e=="string")},e:"string"},G={...o,r(e,r){return i(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},H={...o,r(e,r){return i(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function J(e){return{...o,r(r,n){return i(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function L(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(r===t)return!0;return!1})},e:e.length==1?`${O(e[0])}_literal`:"literals"}}function Q(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(t.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function p(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",t]=e.split(": "),s=n.slice(1);return`$${r}${s}: ${t}`}return`$${r}: ${e}`}function h(e){return{...o,s:e,e:"object",y:!0,r(r,n){return i(this,r,n,()=>{if(!k(r))return!1;let t=new Set(Object.keys(r)),s={},a=[];for(let[c,d]of Object.entries(e)){let l=c,y=r[c];t.delete(c);let[f,u]=d.r(y,n);if(f)s[l]=y;else{let b=u;for(let R of b)a.push(p(R,`.${c}`))}}if(this.e==="strict_obj"&&t.size>0)for(let c of t)a.push(`Key '${c}' is not defined in the object shape`);return a.length>0?{errors:a}:this.e.startsWith("extends_object")?{data:r}:{data:s}})}}}function X(e){return{...h(e),e:"extends_object"}}function Y(e){return{...h(e),e:"strict_obj"}}function Z(e,r){return h({...e.s,...r.s})}function ee(e){return{...o,e:`record<string, ${e.e}>`,r(r,n){return i(this,r,n,()=>{if(!k(r))return!1;let t={},s=[];for(let[a,c]of Object.entries(r)){let d=r[a],[l,y]=e.r(c,n);if(l)t[a]=d;else{let f=y;for(let u of f)s.push(p(u,`.${a}`))}}return s.length>0?{errors:s}:{data:t}})}}}function j(e,r){if(typeof r=="string"&&!e.s?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function x(e,r,n,t=!1,s){let a=-1,c=[],d=new Set;for(let l of e){a++;let y=Array.isArray(r)?r[a]:r,f=y.r(l,n),u=s?.unique;if(f[0]&&u){let T=l,w=typeof u=="string";w&&(T=l[u]),d.has(T)?f=[!1,[w?p(`${y.s?.[u]?.e} value is not unique`,`.${u}`):`${y.e} value is not unique`]]:d.add(T)}let[b,R]=f;if(b)t&&c.push(l);else if(t){n.warnings.push(...R.map(T=>p(T,`[${a}]`)));continue}else return{errors:R.map(T=>p(T,`[${a}]`))}}return t?{data:c}:!0}function re(e,r){return j(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:x.call(this,n,e,t,!1,r):!1)}}}function ne(e,r){return j(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:x.call(this,n,e,t,!0,r):!1)}}}function te(e){return{...o,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return i(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:x.call(this,r,e,n))}}}function g(e,r){let n={warnings:[]},[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 se(e){return r=>g(r,e)}function ae(e,r){let n=g(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function $(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function oe(e){return r=>$(r,e)}function ie(e){return{...o,e:"recursive",r(r,n){return e().r(r,n)}}}function O(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function k(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}0&&(module.exports={rc_any,rc_array,rc_boolean,rc_date,rc_extends_obj,rc_instanceof,rc_is_valid,rc_literals,rc_loose_array,rc_loose_parse,rc_null,rc_number,rc_obj_intersection,rc_object,rc_parse,rc_parser,rc_record,rc_recursive,rc_strict_obj,rc_string,rc_tuple,rc_undefined,rc_union,rc_unknown,rc_validator});
1
+ "use strict";var _=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var P=(e,r)=>{for(var n in r)_(e,n,{get:r[n],enumerable:!0})},V=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of A(r))!S.call(e,s)&&s!==n&&_(e,s,{get:()=>r[s],enumerable:!(t=m(r,s))||t.enumerable});return e};var q=e=>V(_({},"__esModule",{value:!0}),e);var ue={};P(ue,{rc_any:()=>B,rc_array:()=>re,rc_boolean:()=>M,rc_date:()=>H,rc_instanceof:()=>J,rc_is_valid:()=>$,rc_literals:()=>L,rc_loose_array:()=>ne,rc_loose_parse:()=>ae,rc_null:()=>z,rc_number:()=>G,rc_obj_intersection:()=>Z,rc_object:()=>b,rc_parse:()=>x,rc_parser:()=>se,rc_record:()=>ee,rc_recursive:()=>ie,rc_rename_key:()=>X,rc_strict_obj:()=>Y,rc_string:()=>W,rc_transform:()=>ce,rc_tuple:()=>te,rc_undefined:()=>U,rc_union:()=>Q,rc_unknown:()=>D,rc_validator:()=>oe});module.exports=q(ue);function E(e){return{...this,o:e}}function i(e,r,n,t){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 s=t();if(s&&(s===!0||"data"in s)){let a=s===!0?r:s.data;return e.t&&!e.t(a)?[!1,[e.n(a)]]:[!0,a]}if(e.o!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.o];if(e.y&&e.i){let a=e.i(r);if(a)return e.t&&!e.t(a.fixed)?[!1,[e.n(a.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,a.fixed])}return[!1,s&&"errors"in s?s.errors:[e.n(r)]]}function I(e){return{...this,y:!0,i:e}}function N(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function v(){return{...this,c:!0}}function K(e){return`Type '${k(e)}' is not assignable to '${this.e}'`}function F(){return{...this,l:!0,e:`${this.e}_or_nullable`}}function C(){return{...this,u:!0,e:`${this.e}_or_nullish`}}var o={withFallback:E,where:N,optional:v,n:K,nullable:F,withAutofix:I,nullish:C},U={...o,r(e,r){return i(this,e,r,()=>e===void 0)},e:"undefined"},z={...o,r(e,r){return i(this,e,r,()=>e===null)},e:"null"},B={...o,r(e,r){return i(this,e,r,()=>!0)},e:"any"},D={...o,r(e,r){return i(this,e,r,()=>!0)},e:"unknown"},M={...o,r(e,r){return i(this,e,r,()=>typeof e=="boolean")},e:"boolean"},W={...o,r(e,r){return i(this,e,r,()=>typeof e=="string")},e:"string"},G={...o,r(e,r){return i(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},H={...o,r(e,r){return i(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function J(e){return{...o,r(r,n){return i(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function L(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(r===t)return!0;return!1})},e:e.length==1?`${k(e[0])}_literal`:"literals"}}function Q(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(t.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function X(e,r){return{...r,a:e}}function p(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",t]=e.split(": "),s=n.slice(1);return`$${r}${s}: ${t}`}return`$${r}: ${e}`}function b(e){return{...o,s:e,e:"object",f:!0,r(r,n){return i(this,r,n,()=>{if(!O(r))return!1;let t=new Set(Object.keys(r)),s={},a=[];for(let[c,l]of Object.entries(e)){let d=c,u,y=c;l.a?(u=r[l.a],y=l.a,u===void 0&&(u=r[c],y=c)):u=r[c],t.delete(y);let[f,R]=l.r(u,n);if(f)s[d]=R;else{let h=R;for(let T of h)a.push(p(T,`.${c}`))}}if(this.e==="strict_obj"&&t.size>0)for(let c of t)a.push(`Key '${c}' is not defined in the object shape`);return a.length>0?{errors:a}:{data:s}})}}}function Y(e){return{...b(e),e:"strict_obj"}}function Z(e,r){return b({...e.s,...r.s})}function ee(e){return{...o,e:`record<string, ${e.e}>`,r(r,n){return i(this,r,n,()=>{if(!O(r))return!1;let t={},s=[];for(let[a,c]of Object.entries(r)){let l=r[a],[d,u]=e.r(c,n);if(d)t[a]=l;else{let y=u;for(let f of y)s.push(p(f,`.${a}`))}}return s.length>0?{errors:s}:{data:t}})}}}function j(e,r){if(typeof r=="string"&&!e.s?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function g(e,r,n,t=!1,s){let a=-1,c=[],l=new Set;for(let d of e){a++;let u=Array.isArray(r)?r[a]:r,y=u.r(d,n),f=s?.unique;if(y[0]&&f){let T=d,w=typeof f=="string";w&&(T=d[f]),l.has(T)?y=[!1,[w?p(`${u.s?.[f]?.e} value is not unique`,`.${f}`):`${u.e} value is not unique`]]:l.add(T)}let[R,h]=y;if(R)t&&c.push(d);else if(t){n.warnings.push(...h.map(T=>p(T,`[${a}]`)));continue}else return{errors:h.map(T=>p(T,`[${a}]`))}}return t?{data:c}:!0}function re(e,r){return j(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:g.call(this,n,e,t,!1,r):!1)}}}function ne(e,r){return j(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:g.call(this,n,e,t,!0,r):!1)}}}function te(e){return{...o,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return i(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:g.call(this,r,e,n))}}}function x(e,r){let n={warnings:[]},[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 se(e){return r=>x(r,e)}function ae(e,r){let n=x(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function $(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function oe(e){return r=>$(r,e)}function ie(e){return{...o,e:"recursive",r(r,n){return e().r(r,n)}}}function ce(e,r){return{...o,e:e.e,r(n,t){let[s,a]=e.r(n,t);return s?[!0,r(a)]:[!1,a]}}}function k(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function O(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}0&&(module.exports={rc_any,rc_array,rc_boolean,rc_date,rc_instanceof,rc_is_valid,rc_literals,rc_loose_array,rc_loose_parse,rc_null,rc_number,rc_obj_intersection,rc_object,rc_parse,rc_parser,rc_record,rc_recursive,rc_rename_key,rc_strict_obj,rc_string,rc_transform,rc_tuple,rc_undefined,rc_union,rc_unknown,rc_validator});
@@ -29,13 +29,14 @@ declare const rc_date: RcType<Date>;
29
29
  declare function rc_instanceof<T extends Function>(classToCheck: T): RcType<T>;
30
30
  declare function rc_literals<T extends (string | number | boolean)[]>(...literals: T): RcType<T[number]>;
31
31
  declare function rc_union<T extends RcType<any>[]>(...types: T): RcType<RcInferType<T[number]>>;
32
+ declare function rc_rename_key<T extends RcType<any>>(alternativeNames: string, type: T): RcType<RcInferType<T>>;
32
33
  type RcObject = Record<string, RcType<any>>;
33
34
  type TypeOfObjectType<T extends RcObject> = {
34
35
  [K in keyof T]: RcInferType<T[K]>;
35
36
  };
36
37
  type RcObjType<T extends RcObject> = RcType<TypeOfObjectType<T>>;
37
38
  declare function rc_object<T extends RcObject>(shape: T): RcObjType<T>;
38
- declare function rc_extends_obj<T extends RcObject>(shape: T): RcObjType<T>;
39
+ /** return an error if the obj has more keys than the expected type */
39
40
  declare function rc_strict_obj<T extends RcObject>(shape: T): RcObjType<T>;
40
41
  declare function rc_obj_intersection<A extends RcObject, B extends RcObject>(a: RcObjType<A>, b: RcObjType<B>): RcObjType<A & B>;
41
42
  type RcRecord<V extends RcType<any>> = Record<string, V>;
@@ -71,5 +72,6 @@ declare function rc_loose_parse<S>(input: any, type: RcType<S>): {
71
72
  declare function rc_is_valid<S>(input: any, type: RcType<S>): input is S;
72
73
  declare function rc_validator<S>(type: RcType<S>): (input: any) => input is S;
73
74
  declare function rc_recursive(type: () => RcType<any>): RcType<any>;
75
+ declare function rc_transform<S, T>(type: RcType<S>, transform: (input: S) => T): RcType<T>;
74
76
 
75
- export { RcInferType, RcParseResult, RcParser, RcType, rc_any, rc_array, rc_boolean, rc_date, rc_extends_obj, rc_instanceof, rc_is_valid, rc_literals, rc_loose_array, rc_loose_parse, rc_null, rc_number, rc_obj_intersection, rc_object, rc_parse, rc_parser, rc_record, rc_recursive, rc_strict_obj, rc_string, rc_tuple, rc_undefined, rc_union, rc_unknown, rc_validator };
77
+ export { RcInferType, RcParseResult, RcParser, RcType, rc_any, rc_array, rc_boolean, rc_date, rc_instanceof, rc_is_valid, rc_literals, rc_loose_array, rc_loose_parse, rc_null, rc_number, rc_obj_intersection, rc_object, rc_parse, rc_parser, rc_record, rc_recursive, 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,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y}from"./chunk-NRGTHT24.js";export{c as rc_any,q as rc_array,e as rc_boolean,h as rc_date,m as rc_extends_obj,i as rc_instanceof,w as rc_is_valid,j as rc_literals,r as rc_loose_array,v as rc_loose_parse,b as rc_null,g as rc_number,o as rc_obj_intersection,l as rc_object,t as rc_parse,u as rc_parser,p as rc_record,y as rc_recursive,n as rc_strict_obj,f as rc_string,s as rc_tuple,a as rc_undefined,k as rc_union,d as rc_unknown,x as rc_validator};
1
+ import{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-FAF7WEYO.js";export{c as rc_any,q as rc_array,e as rc_boolean,h as rc_date,i as rc_instanceof,w as rc_is_valid,j as rc_literals,r as rc_loose_array,v as rc_loose_parse,b as rc_null,g as rc_number,o as rc_obj_intersection,m as rc_object,t as rc_parse,u as rc_parser,p as rc_record,y as rc_recursive,l as rc_rename_key,n as rc_strict_obj,f as rc_string,z as rc_transform,s as rc_tuple,a as rc_undefined,k as rc_union,d as rc_unknown,x as rc_validator};
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.11.0",
4
+ "version": "0.13.0",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "test": "vitest --ui",
@@ -1 +0,0 @@
1
- function O(e){return{...this,o:e}}function i(e,r,n,t){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let s=t();if(s&&(s===!0||"data"in s)){let a=s===!0?r:s.data;return e.t&&!e.t(a)?[!1,[e.n(a)]]:[!0,a]}if(e.o!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.o];if(e.l&&e.a){let a=e.a(r);if(a)return e.t&&!e.t(a.fixed)?[!1,[e.n(a.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,a.fixed])}return[!1,s&&"errors"in s?s.errors:[e.n(r)]]}function k(e){return{...this,l:!0,a:e}}function A(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function m(){return{...this,i:!0}}function S(e){return`Type '${j(e)}' is not assignable to '${this.e}'`}function P(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function V(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var o={withFallback:O,where:A,optional:m,n:S,nullable:P,withAutofix:k,nullish:V},E={...o,r(e,r){return i(this,e,r,()=>e===void 0)},e:"undefined"},I={...o,r(e,r){return i(this,e,r,()=>e===null)},e:"null"},N={...o,r(e,r){return i(this,e,r,()=>!0)},e:"any"},v={...o,r(e,r){return i(this,e,r,()=>!0)},e:"unknown"},K={...o,r(e,r){return i(this,e,r,()=>typeof e=="boolean")},e:"boolean"},F={...o,r(e,r){return i(this,e,r,()=>typeof e=="string")},e:"string"},C={...o,r(e,r){return i(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},U={...o,r(e,r){return i(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function z(e){return{...o,r(r,n){return i(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function B(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(r===t)return!0;return!1})},e:e.length==1?`${j(e[0])}_literal`:"literals"}}function M(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...o,r(r,n){return i(this,r,n,()=>{for(let t of e)if(t.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function p(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",t]=e.split(": "),s=n.slice(1);return`$${r}${s}: ${t}`}return`$${r}: ${e}`}function b(e){return{...o,s:e,e:"object",y:!0,r(r,n){return i(this,r,n,()=>{if(!$(r))return!1;let t=new Set(Object.keys(r)),s={},a=[];for(let[c,d]of Object.entries(e)){let l=c,y=r[c];t.delete(c);let[f,u]=d.r(y,n);if(f)s[l]=y;else{let h=u;for(let R of h)a.push(p(R,`.${c}`))}}if(this.e==="strict_obj"&&t.size>0)for(let c of t)a.push(`Key '${c}' is not defined in the object shape`);return a.length>0?{errors:a}:this.e.startsWith("extends_object")?{data:r}:{data:s}})}}}function W(e){return{...b(e),e:"extends_object"}}function D(e){return{...b(e),e:"strict_obj"}}function G(e,r){return b({...e.s,...r.s})}function H(e){return{...o,e:`record<string, ${e.e}>`,r(r,n){return i(this,r,n,()=>{if(!$(r))return!1;let t={},s=[];for(let[a,c]of Object.entries(r)){let d=r[a],[l,y]=e.r(c,n);if(l)t[a]=d;else{let f=y;for(let u of f)s.push(p(u,`.${a}`))}}return s.length>0?{errors:s}:{data:t}})}}}function g(e,r){if(typeof r=="string"&&!e.s?.[r])throw new Error(`${e.e} can't be used with unique key option`)}function _(e,r,n,t=!1,s){let a=-1,c=[],d=new Set;for(let l of e){a++;let y=Array.isArray(r)?r[a]:r,f=y.r(l,n),u=s?.unique;if(f[0]&&u){let T=l,x=typeof u=="string";x&&(T=l[u]),d.has(T)?f=[!1,[x?p(`${y.s?.[u]?.e} value is not unique`,`.${u}`):`${y.e} value is not unique`]]:d.add(T)}let[h,R]=f;if(h)t&&c.push(l);else if(t){n.warnings.push(...R.map(T=>p(T,`[${a}]`)));continue}else return{errors:R.map(T=>p(T,`[${a}]`))}}return t?{data:c}:!0}function J(e,r){return g(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:_.call(this,n,e,t,!1,r):!1)}}}function L(e,r){return g(e,r?.unique),{...o,e:`${e.e}[]`,r(n,t){return i(this,n,t,()=>Array.isArray(n)?n.length===0?!0:_.call(this,n,e,t,!0,r):!1)}}}function Q(e){return{...o,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return i(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:_.call(this,r,e,n))}}}function w(e,r){let n={warnings:[]},[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 X(e){return r=>w(r,e)}function Y(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 q(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function Z(e){return r=>q(r,e)}function ee(e){return{...o,e:"recursive",r(r,n){return e().r(r,n)}}}function j(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function $(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}export{E as a,I as b,N as c,v as d,K as e,F as f,C as g,U as h,z as i,B as j,M as k,b as l,W as m,D as n,G as o,H as p,J as q,L as r,Q as s,w as t,X as u,Y as v,q as w,Z as x,ee as y};