runcheck 0.7.0 → 0.8.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
@@ -116,6 +116,20 @@ if (rc_is_valid(input, rc_array(rc_string))) {
116
116
  }
117
117
  ```
118
118
 
119
+ # Loose parsing
120
+
121
+ Use `rc_parse_loose` to parse a input and returning null if the input is invalid.
122
+
123
+ ```ts
124
+ import { rc_loose_parse } from 'runcheck'
125
+
126
+ const input = JSON.parse(jsonInput)
127
+
128
+ const result = rc_loose_parse(input, rc_array(rc_string)).data
129
+
130
+ result // string[] | null
131
+ ```
132
+
119
133
  # Autofixing and fallback values in parsing
120
134
 
121
135
  Values can be autofixed and fallback values can be provided for parsing. The checks will pass but the result will return warnings messages.
@@ -1 +1 @@
1
- "use strict";var i=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(e,r)=>{for(var s in r)i(e,s,{get:r[s],enumerable:!0})},R=(e,r,s,c)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of T(r))!d.call(e,n)&&n!==s&&i(e,n,{get:()=>r[n],enumerable:!(c=f(r,n))||c.enumerable});return e};var _=e=>R(i({},"__esModule",{value:!0}),e);var N={};p(N,{rc_boolean_autofix:()=>$,rc_number_autofix:()=>k,rc_string_autofix:()=>A});module.exports=_(N);function x(e){return{...this,s:e}}function o(e,r,s,c){if(e.c&&r===void 0)return[!0,r];if(e.i&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let n=c();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.s!==void 0)return s.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.y&&e.o){let t=e.o(r);if(t)return e.t&&!e.t(t.fixed)?[!1,[e.n(t.fixed)]]:(s.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,t.fixed])}return[!1,n&&"errors"in n?n.errors:[e.n(r)]]}function b(e){return{...this,y:!0,o:e}}function h(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function g(){return{...this,c:!0}}function j(e){return`Type '${O(e)}' is not assignable to '${this.e}'`}function w(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function m(){return{...this,i:!0,e:`${this.e}_or_nullish`}}var a={withFallback:x,where:h,optional:g,n:j,nullable:w,withAutofix:b,nullish:m},S={...a,r(e,r){return o(this,e,r,()=>e===void 0)},e:"undefined"},V={...a,r(e,r){return o(this,e,r,()=>e===null)},e:"null"},E={...a,r(e,r){return o(this,e,r,()=>!0)},e:"any"},u={...a,r(e,r){return o(this,e,r,()=>typeof e=="boolean")},e:"boolean"},y={...a,r(e,r){return o(this,e,r,()=>typeof e=="string")},e:"string"},l={...a,r(e,r){return o(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},I={...a,r(e,r){return o(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function O(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}var $=u.withAutofix(e=>e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),A=y.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),k=l.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 i=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(e,r)=>{for(var s in r)i(e,s,{get:r[s],enumerable:!0})},R=(e,r,s,c)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of T(r))!d.call(e,n)&&n!==s&&i(e,n,{get:()=>r[n],enumerable:!(c=f(r,n))||c.enumerable});return e};var _=e=>R(i({},"__esModule",{value:!0}),e);var S={};p(S,{rc_boolean_autofix:()=>$,rc_number_autofix:()=>k,rc_string_autofix:()=>A});module.exports=_(S);function x(e){return{...this,s:e}}function o(e,r,s,c){if(e.c&&r===void 0)return[!0,r];if(e.i&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let n=c();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.s!==void 0)return s.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.l&&e.o){let t=e.o(r);if(t)return e.t&&!e.t(t.fixed)?[!1,[e.n(t.fixed)]]:(s.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,t.fixed])}return[!1,n&&"errors"in n?n.errors:[e.n(r)]]}function b(e){return{...this,l:!0,o:e}}function h(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function g(){return{...this,c:!0}}function j(e){return`Type '${O(e)}' is not assignable to '${this.e}'`}function w(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function m(){return{...this,i:!0,e:`${this.e}_or_nullish`}}var a={withFallback:x,where:h,optional:g,n:j,nullable:w,withAutofix:b,nullish:m},P={...a,r(e,r){return o(this,e,r,()=>e===void 0)},e:"undefined"},V={...a,r(e,r){return o(this,e,r,()=>e===null)},e:"null"},E={...a,r(e,r){return o(this,e,r,()=>!0)},e:"any"},u={...a,r(e,r){return o(this,e,r,()=>typeof e=="boolean")},e:"boolean"},l={...a,r(e,r){return o(this,e,r,()=>typeof e=="string")},e:"string"},y={...a,r(e,r){return o(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},I={...a,r(e,r){return o(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function O(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}var $=u.withAutofix(e=>e==null||e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),A=l.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),k=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,7 +1,11 @@
1
1
  import { RcType } from './runcheck.js';
2
2
 
3
+ /** autofixes a boolean type if input is 0 | 1 | 'true' | 'false' | undefined | null,
4
+ * returning error otherwise */
3
5
  declare const rc_boolean_autofix: RcType<boolean>;
6
+ /** autofixes a string type if input is a number, returning error otherwise */
4
7
  declare const rc_string_autofix: RcType<string>;
8
+ /** autofixes a number type if input is a a valid number string, returning error otherwise */
5
9
  declare const rc_number_autofix: RcType<number>;
6
10
 
7
11
  export { rc_boolean_autofix, rc_number_autofix, rc_string_autofix };
@@ -1 +1 @@
1
- import{d as t,e as f,f as o}from"./chunk-Y6MMYOJL.js";var s=t.withAutofix(r=>r===0||r===1?{fixed:!!r}:r==="true"||r==="false"?{fixed:r==="true"}:!1),u=f.withAutofix(r=>typeof r=="number"&&!Number.isNaN(r)?{fixed:r.toString()}:!1),x=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,x as rc_number_autofix,u as rc_string_autofix};
1
+ import{d as f,e as t,f as o}from"./chunk-LQNEWNAC.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 +1 @@
1
- function g(e){return{...this,s:e}}function a(e,r,n,s){if(e.c&&r===void 0)return[!0,r];if(e.i&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.n(o)]]:[!0,o]}if(e.s!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.y&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.n(o.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,o.fixed])}return[!1,t&&"errors"in t?t.errors:[e.n(r)]]}function j(e){return{...this,y:!0,o:e}}function O(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function w(){return{...this,c:!0}}function $(e){return`Type '${_(e)}' is not assignable to '${this.e}'`}function m(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function k(){return{...this,i:!0,e:`${this.e}_or_nullish`}}var c={withFallback:g,where:O,optional:w,n:$,nullable:m,withAutofix:j,nullish:k},S={...c,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},V={...c,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},N={...c,r(e,r){return a(this,e,r,()=>!0)},e:"any"},E={...c,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},I={...c,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},K={...c,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},v={...c,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function F(e){return{...c,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function C(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${_(e[0])}_literal`:"literals"}}function z(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function d(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function p(e){return{...c,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!h(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[i,u]of Object.entries(e)){let y=i,l=r[i];s.delete(i);let[f,T]=u.r(l,n);if(f)t[y]=l;else{let b=T;for(let x of b)o.push(d(x,`.${i}`))}}if(this.e==="strict_obj"&&s.size>0)for(let i of s)o.push(`Key '${i}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function B(e){return{...p(e),e:"extends_object"}}function M(e){return{...p(e),e:"strict_obj"}}function U(e,r){return p({...e.a,...r.a})}function W(e){return{...c,e:`record<string, ${e.e}>`,r(r,n){return a(this,r,n,()=>{if(!h(r))return!1;let s={},t=[];for(let[o,i]of Object.entries(r)){let u=r[o],[y,l]=e.r(i,n);if(y)s[o]=u;else{let f=l;for(let T of f)t.push(d(T,`.${o}`))}}return t.length>0?{errors:t}:{data:s}})}}}function R(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[i,u]=o.r(t,n);if(!i)return{errors:u.map(y=>d(y,`[${s}]`))}}return!0}function D(e){return{...c,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:R.call(this,r,e,n):!1)}}}function q(e){return{...c,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:R.call(this,r,e,n))}}}function A(e,r){let n={warnings:[]},[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 G(e){return r=>A(r,e)}function P(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function H(e){return r=>P(r,e)}function _(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function h(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}export{S as a,V as b,N as c,E as d,I as e,K as f,v as g,F as h,C as i,z as j,p as k,B as l,M as m,U as n,W as o,D as p,q,A as r,G as s,P as t,H as u};
1
+ function j(e){return{...this,s:e}}function a(e,r,n,s){if(e.c&&r===void 0)return[!0,r];if(e.i&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.n(o)]]:[!0,o]}if(e.s!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.l&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.n(o.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,o.fixed])}return[!1,t&&"errors"in t?t.errors:[e.n(r)]]}function w(e){return{...this,l:!0,o:e}}function O(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function $(){return{...this,c:!0}}function m(e){return`Type '${h(e)}' is not assignable to '${this.e}'`}function k(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function A(){return{...this,i:!0,e:`${this.e}_or_nullish`}}var c={withFallback:j,where:O,optional:$,n:m,nullable:k,withAutofix:w,nullish:A},P={...c,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},V={...c,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},N={...c,r(e,r){return a(this,e,r,()=>!0)},e:"any"},E={...c,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},I={...c,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},K={...c,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},v={...c,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function F(e){return{...c,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function C(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${h(e[0])}_literal`:"literals"}}function z(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function d(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function p(e){return{...c,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!b(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[i,u]of Object.entries(e)){let y=i,l=r[i];s.delete(i);let[f,T]=u.r(l,n);if(f)t[y]=l;else{let x=T;for(let g of x)o.push(d(g,`.${i}`))}}if(this.e==="strict_obj"&&s.size>0)for(let i of s)o.push(`Key '${i}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function B(e){return{...p(e),e:"extends_object"}}function M(e){return{...p(e),e:"strict_obj"}}function U(e,r){return p({...e.a,...r.a})}function W(e){return{...c,e:`record<string, ${e.e}>`,r(r,n){return a(this,r,n,()=>{if(!b(r))return!1;let s={},t=[];for(let[o,i]of Object.entries(r)){let u=r[o],[y,l]=e.r(i,n);if(y)s[o]=u;else{let f=l;for(let T of f)t.push(d(T,`.${o}`))}}return t.length>0?{errors:t}:{data:s}})}}}function R(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[i,u]=o.r(t,n);if(!i)return{errors:u.map(y=>d(y,`[${s}]`))}}return!0}function D(e){return{...c,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:R.call(this,r,e,n):!1)}}}function q(e){return{...c,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:R.call(this,r,e,n))}}}function _(e,r){let n={warnings:[]},[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 G(e){return r=>_(r,e)}function H(e,r){let n=_(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function S(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function J(e){return r=>S(r,e)}function h(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function b(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}export{P as a,V as b,N as c,E as d,I as e,K as f,v as g,F as h,C as i,z as j,p as k,B as l,M as m,U as n,W as o,D as p,q,_ as r,G as s,H as t,S as u,J as v};
package/dist/runcheck.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var p=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var k=(e,r)=>{for(var n in r)p(e,n,{get:r[n],enumerable:!0})},A=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of $(r))!m.call(e,t)&&t!==n&&p(e,t,{get:()=>r[t],enumerable:!(s=w(r,t))||s.enumerable});return e};var P=e=>A(p({},"__esModule",{value:!0}),e);var re={};k(re,{rc_any:()=>z,rc_array:()=>X,rc_boolean:()=>B,rc_date:()=>W,rc_extends_obj:()=>H,rc_instanceof:()=>D,rc_is_valid:()=>b,rc_literals:()=>q,rc_null:()=>C,rc_number:()=>U,rc_obj_intersection:()=>L,rc_object:()=>f,rc_parse:()=>h,rc_parser:()=>Z,rc_record:()=>Q,rc_strict_obj:()=>J,rc_string:()=>M,rc_tuple:()=>Y,rc_undefined:()=>F,rc_union:()=>G,rc_validator:()=>ee});module.exports=P(re);function S(e){return{...this,s:e}}function a(e,r,n,s){if(e.c&&r===void 0)return[!0,r];if(e.i&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.n(o)]]:[!0,o]}if(e.s!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.y&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.n(o.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,o.fixed])}return[!1,t&&"errors"in t?t.errors:[e.n(r)]]}function V(e){return{...this,y:!0,o:e}}function N(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function E(){return{...this,c:!0}}function I(e){return`Type '${x(e)}' is not assignable to '${this.e}'`}function K(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function v(){return{...this,i:!0,e:`${this.e}_or_nullish`}}var c={withFallback:S,where:N,optional:E,n:I,nullable:K,withAutofix:V,nullish:v},F={...c,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},C={...c,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},z={...c,r(e,r){return a(this,e,r,()=>!0)},e:"any"},B={...c,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},M={...c,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},U={...c,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},W={...c,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function D(e){return{...c,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function q(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${x(e[0])}_literal`:"literals"}}function G(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function R(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function f(e){return{...c,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!g(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[i,u]of Object.entries(e)){let y=i,l=r[i];s.delete(i);let[T,d]=u.r(l,n);if(T)t[y]=l;else{let j=d;for(let O of j)o.push(R(O,`.${i}`))}}if(this.e==="strict_obj"&&s.size>0)for(let i of s)o.push(`Key '${i}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function H(e){return{...f(e),e:"extends_object"}}function J(e){return{...f(e),e:"strict_obj"}}function L(e,r){return f({...e.a,...r.a})}function Q(e){return{...c,e:`record<string, ${e.e}>`,r(r,n){return a(this,r,n,()=>{if(!g(r))return!1;let s={},t=[];for(let[o,i]of Object.entries(r)){let u=r[o],[y,l]=e.r(i,n);if(y)s[o]=u;else{let T=l;for(let d of T)t.push(R(d,`.${o}`))}}return t.length>0?{errors:t}:{data:s}})}}}function _(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[i,u]=o.r(t,n);if(!i)return{errors:u.map(y=>R(y,`[${s}]`))}}return!0}function X(e){return{...c,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:_.call(this,r,e,n):!1)}}}function Y(e){return{...c,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:_.call(this,r,e,n))}}}function h(e,r){let n={warnings:[]},[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 Z(e){return r=>h(r,e)}function b(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function ee(e){return r=>b(r,e)}function x(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function g(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_null,rc_number,rc_obj_intersection,rc_object,rc_parse,rc_parser,rc_record,rc_strict_obj,rc_string,rc_tuple,rc_undefined,rc_union,rc_validator});
1
+ "use strict";var p=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var k=(e,r)=>{for(var n in r)p(e,n,{get:r[n],enumerable:!0})},A=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of $(r))!m.call(e,t)&&t!==n&&p(e,t,{get:()=>r[t],enumerable:!(s=O(r,t))||s.enumerable});return e};var S=e=>A(p({},"__esModule",{value:!0}),e);var ne={};k(ne,{rc_any:()=>z,rc_array:()=>X,rc_boolean:()=>B,rc_date:()=>W,rc_extends_obj:()=>H,rc_instanceof:()=>D,rc_is_valid:()=>b,rc_literals:()=>q,rc_loose_parse:()=>ee,rc_null:()=>C,rc_number:()=>U,rc_obj_intersection:()=>L,rc_object:()=>f,rc_parse:()=>_,rc_parser:()=>Z,rc_record:()=>Q,rc_strict_obj:()=>J,rc_string:()=>M,rc_tuple:()=>Y,rc_undefined:()=>F,rc_union:()=>G,rc_validator:()=>re});module.exports=S(ne);function P(e){return{...this,s:e}}function a(e,r,n,s){if(e.c&&r===void 0)return[!0,r];if(e.i&&r==null)return[!0,r];if(e.u&&r===null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.n(o)]]:[!0,o]}if(e.s!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.y&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.n(o.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,o.fixed])}return[!1,t&&"errors"in t?t.errors:[e.n(r)]]}function V(e){return{...this,y:!0,o:e}}function N(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function E(){return{...this,c:!0}}function I(e){return`Type '${x(e)}' is not assignable to '${this.e}'`}function K(){return{...this,u:!0,e:`${this.e}_or_nullable`}}function v(){return{...this,i:!0,e:`${this.e}_or_nullish`}}var c={withFallback:P,where:N,optional:E,n:I,nullable:K,withAutofix:V,nullish:v},F={...c,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},C={...c,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},z={...c,r(e,r){return a(this,e,r,()=>!0)},e:"any"},B={...c,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},M={...c,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},U={...c,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},W={...c,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function D(e){return{...c,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function q(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${x(e[0])}_literal`:"literals"}}function G(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...c,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function R(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function f(e){return{...c,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!g(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[i,u]of Object.entries(e)){let y=i,l=r[i];s.delete(i);let[T,d]=u.r(l,n);if(T)t[y]=l;else{let j=d;for(let w of j)o.push(R(w,`.${i}`))}}if(this.e==="strict_obj"&&s.size>0)for(let i of s)o.push(`Key '${i}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function H(e){return{...f(e),e:"extends_object"}}function J(e){return{...f(e),e:"strict_obj"}}function L(e,r){return f({...e.a,...r.a})}function Q(e){return{...c,e:`record<string, ${e.e}>`,r(r,n){return a(this,r,n,()=>{if(!g(r))return!1;let s={},t=[];for(let[o,i]of Object.entries(r)){let u=r[o],[y,l]=e.r(i,n);if(y)s[o]=u;else{let T=l;for(let d of T)t.push(R(d,`.${o}`))}}return t.length>0?{errors:t}:{data:s}})}}}function h(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[i,u]=o.r(t,n);if(!i)return{errors:u.map(y=>R(y,`[${s}]`))}}return!0}function X(e){return{...c,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:h.call(this,r,e,n):!1)}}}function Y(e){return{...c,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:h.call(this,r,e,n))}}}function _(e,r){let n={warnings:[]},[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 Z(e){return r=>_(r,e)}function ee(e,r){let n=_(e,r);return n.error?{data:null,errors:n.errors,warnings:!1}:{data:n.data,errors:!1,warnings:n.warnings}}function b(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function re(e){return r=>b(r,e)}function x(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function g(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_parse,rc_null,rc_number,rc_obj_intersection,rc_object,rc_parse,rc_parser,rc_record,rc_strict_obj,rc_string,rc_tuple,rc_undefined,rc_union,rc_validator});
@@ -16,8 +16,8 @@ declare type RcType<T> = {
16
16
  readonly withFallback: (fallback: T) => RcType<T>;
17
17
  readonly where: (predicate: (input: T) => boolean) => RcType<T>;
18
18
  readonly optional: () => RcOptional<T>;
19
- readonly nullable: () => RcType<T | null | undefined>;
20
- readonly nullish: () => RcType<T | null>;
19
+ readonly nullable: () => RcType<T | null>;
20
+ readonly nullish: () => RcType<T | null | undefined>;
21
21
  readonly withAutofix: (customAutofix: (input: unknown) => false | {
22
22
  fixed: T;
23
23
  }) => RcType<T>;
@@ -61,7 +61,12 @@ declare function rc_tuple<T extends readonly RcType<any>[]>(types: T): RcType<Ma
61
61
  declare function rc_parse<S>(input: any, type: RcType<S>): RcParseResult<S>;
62
62
  declare type RcParser<T> = (input: any) => RcParseResult<T>;
63
63
  declare function rc_parser<S>(type: RcType<S>): RcParser<S>;
64
+ declare function rc_loose_parse<S>(input: any, type: RcType<S>): {
65
+ data: S | null;
66
+ errors: string[] | false;
67
+ warnings: string[] | false;
68
+ };
64
69
  declare function rc_is_valid<S>(input: any, type: RcType<S>): input is S;
65
70
  declare function rc_validator<S>(type: RcType<S>): (input: any) => input is S;
66
71
 
67
- export { RcInferType, RcParseResult, RcParser, RcType, rc_any, rc_array, rc_boolean, rc_date, rc_extends_obj, rc_instanceof, rc_is_valid, rc_literals, rc_null, rc_number, rc_obj_intersection, rc_object, rc_parse, rc_parser, rc_record, rc_strict_obj, rc_string, rc_tuple, rc_undefined, rc_union, rc_validator };
72
+ 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_parse, rc_null, rc_number, rc_obj_intersection, rc_object, rc_parse, rc_parser, rc_record, rc_strict_obj, rc_string, rc_tuple, rc_undefined, rc_union, 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}from"./chunk-Y6MMYOJL.js";export{c as rc_any,p as rc_array,d as rc_boolean,g as rc_date,l as rc_extends_obj,h as rc_instanceof,t as rc_is_valid,i as rc_literals,b as rc_null,f as rc_number,n as rc_obj_intersection,k as rc_object,r as rc_parse,s as rc_parser,o as rc_record,m as rc_strict_obj,e as rc_string,q as rc_tuple,a as rc_undefined,j as rc_union,u 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}from"./chunk-LQNEWNAC.js";export{c as rc_any,p as rc_array,d as rc_boolean,g as rc_date,l as rc_extends_obj,h as rc_instanceof,u as rc_is_valid,i as rc_literals,t as rc_loose_parse,b as rc_null,f as rc_number,n as rc_obj_intersection,k as rc_object,r as rc_parse,s as rc_parser,o as rc_record,m as rc_strict_obj,e as rc_string,q as rc_tuple,a as rc_undefined,j as rc_union,v 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.7.0",
4
+ "version": "0.8.0",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "test": "vitest --ui",
@@ -57,7 +57,7 @@
57
57
  "typescript": "^4.8.1-rc",
58
58
  "v8-profiler-next": "^1.9.0",
59
59
  "vite": "^3.0.7",
60
- "vitest": "^0.21.1",
60
+ "vitest": "^0.25.1",
61
61
  "zod": "^3.18.0"
62
62
  }
63
63
  }