runcheck 0.8.0 → 0.10.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
@@ -27,6 +27,37 @@ pnpm add runcheck
27
27
  | `rc_array<T>(type: T)` | `T[]` |
28
28
  | `rc_tuple<T>(...types: T[])` | `[T, T]` |
29
29
 
30
+ # Array types:
31
+
32
+ ## Array loose check
33
+
34
+ You can also use `rc_loose_array` to reject the wrong elements of an array and return the valid ones.
35
+
36
+ ```ts
37
+ const shape = rc_loose_array(rc_string)
38
+
39
+ const input = ['hello', 1, 'world']
40
+
41
+ const result = rc_parse(input, shape)
42
+
43
+ // result.data will be ['hello', 'world']
44
+ // result.warnings will return the warnings about the invalid elements
45
+ ```
46
+
47
+ ## Checking unique values
48
+
49
+ With the `rc_array` or `rc_loose_array` type you can also use the `unique` option to check if the array has no duplicated values.
50
+
51
+ ```ts
52
+ const shape = rc_array(rc_string, { unique: true })
53
+ ```
54
+
55
+ For arrays of objects, you can provide a string to `unique` option to check if the array items has no duplicated values of a specific property.
56
+
57
+ ```ts
58
+ const shape = rc_array(rc_object({ id: rc_string }), { unique: 'id' })
59
+ ```
60
+
30
61
  # Object types:
31
62
 
32
63
  ## `rc_object`
@@ -192,3 +223,27 @@ You can use also modiers like `rc_string.optional()` to extend or modify the rc
192
223
  | `rc_[type].optional()` | `T \| undefined` |
193
224
  | `rc_[type].nullable()` | `T \| null` |
194
225
  | `rc_[type].nullish()` | `T \| null \| undefined` |
226
+
227
+ # Recursive types
228
+
229
+ You can use `rc_recursive` to create recursive types. But the types can't be inferred in this case. So you need to provide the type manually.
230
+
231
+ ```ts
232
+ type MenuTree = {
233
+ name: string
234
+ children: MenuTree[]
235
+ }
236
+
237
+ // the type should be provided manually to the variable in this case
238
+ const menuTreeSchema: RcType<MenuTree[]> = rc_recursive(() =>
239
+ rc_array(
240
+ rc_object({
241
+ name: rc_string,
242
+ // you can safely autorefence the schema here
243
+ children: menuTreeSchema,
244
+ }),
245
+ ),
246
+ )
247
+
248
+ const result = rc_parse(input, menuTreeSchema)
249
+ ```
@@ -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 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
+ "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 s in r)c(e,s,{get:r[s],enumerable:!0})},R=(e,r,s,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of T(r))!d.call(e,n)&&n!==s&&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:()=>k,rc_string_autofix:()=>A});module.exports=b(S);function h(e){return{...this,a:e}}function a(e,r,s,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.a!==void 0)return s.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.a];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 _(e){return{...this,l:!0,o: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 a(this,e,r,()=>e===void 0)},e:"undefined"},V={...o,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},q={...o,r(e,r){return a(this,e,r,()=>!0)},e:"any"},u={...o,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},l={...o,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},y={...o,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},E={...o,r(e,r){return a(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),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 +1 @@
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
+ import{d as f,e as t,f as o}from"./chunk-VZH5A46S.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 O(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 k(e){return{...this,l:!0,o:e}}function A(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function m(){return{...this,i:!0}}function S(e){return`Type '${w(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,()=>typeof e=="boolean")},e:"boolean"},K={...o,r(e,r){return i(this,e,r,()=>typeof e=="string")},e:"string"},F={...o,r(e,r){return i(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},C={...o,r(e,r){return i(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function U(e){return{...o,r(r,n){return i(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function z(...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?`${w(e[0])}_literal`:"literals"}}function B(...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 M(e){return{...b(e),e:"extends_object"}}function W(e){return{...b(e),e:"strict_obj"}}function D(e,r){return b({...e.s,...r.s})}function G(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 H(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 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,!0,r):!1)}}}function L(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 j(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 Q(e){return r=>j(r,e)}function X(e,r){let n=j(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 Y(e){return r=>q(r,e)}function Z(e){return{...o,e:"recursive",r(r,n){return e().r(r,n)}}}function w(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,b as k,M as l,W as m,D as n,G as o,H as p,J as q,L as r,j as s,Q as t,X as u,q as v,Y as w,Z as x};
package/dist/runcheck.cjs CHANGED
@@ -1 +1 @@
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});
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 ie={};P(ie,{rc_any:()=>B,rc_array:()=>ee,rc_boolean:()=>M,rc_date:()=>G,rc_extends_obj:()=>Q,rc_instanceof:()=>H,rc_is_valid:()=>$,rc_literals:()=>J,rc_loose_array:()=>re,rc_loose_parse:()=>se,rc_null:()=>z,rc_number:()=>D,rc_obj_intersection:()=>Y,rc_object:()=>h,rc_parse:()=>g,rc_parser:()=>te,rc_record:()=>Z,rc_recursive:()=>oe,rc_strict_obj:()=>X,rc_string:()=>W,rc_tuple:()=>ne,rc_undefined:()=>U,rc_union:()=>L,rc_validator:()=>ae});module.exports=q(ie);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,()=>typeof e=="boolean")},e:"boolean"},W={...o,r(e,r){return i(this,e,r,()=>typeof e=="string")},e:"string"},D={...o,r(e,r){return i(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},G={...o,r(e,r){return i(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function H(e){return{...o,r(r,n){return i(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function J(...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 L(...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 Q(e){return{...h(e),e:"extends_object"}}function X(e){return{...h(e),e:"strict_obj"}}function Y(e,r){return h({...e.s,...r.s})}function Z(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 w(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,j=typeof u=="string";j&&(T=l[u]),d.has(T)?f=[!1,[j?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 ee(e,r){return w(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 re(e,r){return w(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 ne(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 te(e){return r=>g(r,e)}function se(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 ae(e){return r=>$(r,e)}function oe(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_validator});
@@ -1,4 +1,4 @@
1
- declare type RcParseResult<T> = {
1
+ type RcParseResult<T> = {
2
2
  error: false;
3
3
  data: T;
4
4
  warnings: string[] | false;
@@ -6,13 +6,9 @@ declare type RcParseResult<T> = {
6
6
  error: true;
7
7
  errors: string[];
8
8
  };
9
- declare type RcInferType<T extends RcType<any>> = T extends RcType<infer U> ? U : never;
10
- declare type RcOptional<T> = RcType<T | undefined>;
11
- declare type ParseResultCtx = {
12
- warnings: string[];
13
- };
14
- declare type InternalParseResult<T> = [success: true, data: T] | [success: false, errors: string[]];
15
- declare type RcType<T> = {
9
+ type RcInferType<T extends RcType<any>> = T extends RcType<infer U> ? U : never;
10
+ type RcOptional<T> = RcType<T | undefined>;
11
+ type RcType<T> = {
16
12
  readonly withFallback: (fallback: T) => RcType<T>;
17
13
  readonly where: (predicate: (input: T) => boolean) => RcType<T>;
18
14
  readonly optional: () => RcOptional<T>;
@@ -32,20 +28,25 @@ declare const rc_date: RcType<Date>;
32
28
  declare function rc_instanceof<T extends Function>(classToCheck: T): RcType<T>;
33
29
  declare function rc_literals<T extends (string | number | boolean)[]>(...literals: T): RcType<T[number]>;
34
30
  declare function rc_union<T extends RcType<any>[]>(...types: T): RcType<RcInferType<T[number]>>;
35
- declare type RcObject = Record<string, RcType<any>>;
36
- declare type TypeOfObjectType<T extends RcObject> = {
31
+ type RcObject = Record<string, RcType<any>>;
32
+ type TypeOfObjectType<T extends RcObject> = {
37
33
  [K in keyof T]: RcInferType<T[K]>;
38
34
  };
39
- declare type RcObjType<T extends RcObject> = RcType<TypeOfObjectType<T>>;
35
+ type RcObjType<T extends RcObject> = RcType<TypeOfObjectType<T>>;
40
36
  declare function rc_object<T extends RcObject>(shape: T): RcObjType<T>;
41
37
  declare function rc_extends_obj<T extends RcObject>(shape: T): RcObjType<T>;
42
38
  declare function rc_strict_obj<T extends RcObject>(shape: T): RcObjType<T>;
43
39
  declare function rc_obj_intersection<A extends RcObject, B extends RcObject>(a: RcObjType<A>, b: RcObjType<B>): RcObjType<A & B>;
44
- declare type RcRecord<V extends RcType<any>> = Record<string, V>;
45
- declare type RcRecordType<V extends RcType<any>> = RcType<TypeOfObjectType<RcRecord<V>>>;
40
+ type RcRecord<V extends RcType<any>> = Record<string, V>;
41
+ type RcRecordType<V extends RcType<any>> = RcType<TypeOfObjectType<RcRecord<V>>>;
46
42
  declare function rc_record<V extends RcType<any>>(valueType: V): RcRecordType<V>;
47
- declare function rc_array<T extends RcType<any>>(type: T): RcType<RcInferType<T>[]>;
48
- declare type MapTupleToTypes<T extends readonly [...any[]]> = {
43
+ declare function rc_array<T extends RcType<any>>(type: T, options?: {
44
+ unique: boolean | string | false;
45
+ }): RcType<RcInferType<T>[]>;
46
+ declare function rc_loose_array<T extends RcType<any>>(type: T, options?: {
47
+ unique: boolean | string | false;
48
+ }): RcType<RcInferType<T>[]>;
49
+ type MapTupleToTypes<T extends readonly [...any[]]> = {
49
50
  -readonly [K in keyof T]: RcInferType<T[K]>;
50
51
  };
51
52
  /**
@@ -59,7 +60,7 @@ declare function rc_tuple<T extends readonly RcType<any>[]>(types: T): RcType<Ma
59
60
  * and fallback, or the errors if invalid
60
61
  */
61
62
  declare function rc_parse<S>(input: any, type: RcType<S>): RcParseResult<S>;
62
- declare type RcParser<T> = (input: any) => RcParseResult<T>;
63
+ type RcParser<T> = (input: any) => RcParseResult<T>;
63
64
  declare function rc_parser<S>(type: RcType<S>): RcParser<S>;
64
65
  declare function rc_loose_parse<S>(input: any, type: RcType<S>): {
65
66
  data: S | null;
@@ -68,5 +69,6 @@ declare function rc_loose_parse<S>(input: any, type: RcType<S>): {
68
69
  };
69
70
  declare function rc_is_valid<S>(input: any, type: RcType<S>): input is S;
70
71
  declare function rc_validator<S>(type: RcType<S>): (input: any) => input is S;
72
+ declare function rc_recursive(type: () => RcType<any>): RcType<any>;
71
73
 
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 };
74
+ 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_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}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};
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}from"./chunk-VZH5A46S.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,v as rc_is_valid,i as rc_literals,q as rc_loose_array,u as rc_loose_parse,b as rc_null,f as rc_number,n as rc_obj_intersection,k as rc_object,s as rc_parse,t as rc_parser,o as rc_record,x as rc_recursive,m as rc_strict_obj,e as rc_string,r as rc_tuple,a as rc_undefined,j as rc_union,w 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.8.0",
4
+ "version": "0.10.0",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "test": "vitest --ui",
@@ -9,7 +9,7 @@
9
9
  "benchmark": "tsm benchmarks/benchmark.ts",
10
10
  "benchmark:generate-profile": "tsm benchmarks/generateProfiles.ts",
11
11
  "build": "pnpm test:run && pnpm build:no-test",
12
- "build:no-test": "tsup --minify --dts && tsm scripts/removeInternalDeclarations.ts",
12
+ "build:no-test": "tsup --minify --dts",
13
13
  "npm-publish": "./scripts/check-if-is-sync.sh && pnpm build && npm publish"
14
14
  },
15
15
  "files": [
@@ -44,20 +44,20 @@
44
44
  "node": ">=14.6.0"
45
45
  },
46
46
  "devDependencies": {
47
- "@types/node": "^18.7.2",
48
- "@typescript-eslint/eslint-plugin": "^5.33.0",
49
- "@typescript-eslint/parser": "^5.33.0",
50
- "@vitest/ui": "^0.21.1",
51
- "eslint": "^8.22.0",
52
- "eslint-plugin-jest": "^26.8.2",
53
- "mitata": "^0.1.6",
47
+ "@lucasols/eslint-plugin-extended-lint": "^0.1.1",
48
+ "@types/node": "^18.11.18",
49
+ "@typescript-eslint/eslint-plugin": "^5.48.1",
50
+ "@typescript-eslint/parser": "^5.48.1",
51
+ "@vitest/ui": "^0.27.1",
52
+ "eslint": "^8.31.0",
53
+ "eslint-plugin-vitest": "^0.0.29",
54
54
  "myzod": "^1.8.8",
55
- "tsm": "^2.2.2",
56
- "tsup": "^6.2.2",
57
- "typescript": "^4.8.1-rc",
55
+ "tsm": "^2.3.0",
56
+ "tsup": "^6.5.0",
57
+ "typescript": "^4.9.4",
58
58
  "v8-profiler-next": "^1.9.0",
59
- "vite": "^3.0.7",
60
- "vitest": "^0.25.1",
61
- "zod": "^3.18.0"
59
+ "vite": "^4.0.4",
60
+ "vitest": "^0.27.1",
61
+ "zod": "^3.20.2"
62
62
  }
63
63
  }
@@ -1 +0,0 @@
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};