Comparing version 4.0.0 to 4.1.0
{ | ||
"index.js": { | ||
"bundled": 26712, | ||
"minified": 17240, | ||
"gzipped": 5403 | ||
"bundled": 27001, | ||
"minified": 17436, | ||
"gzipped": 5470 | ||
}, | ||
"index.mjs": { | ||
"bundled": 26318, | ||
"minified": 17122, | ||
"gzipped": 5364, | ||
"bundled": 26579, | ||
"minified": 17323, | ||
"gzipped": 5427, | ||
"treeshaked": { | ||
@@ -12,0 +12,0 @@ "rollup": { |
@@ -23,2 +23,3 @@ export { AsyncContract } from './asynccontract'; | ||
export { Boolean, Function, Number, String, Symbol } from './types/primative'; | ||
export { Readonly } from './types/Readonly'; | ||
export { Record } from './types/Record'; | ||
@@ -25,0 +26,0 @@ export { Tuple } from './types/tuple'; |
@@ -106,4 +106,4 @@ "use strict"; | ||
const i = t(), | ||
u = i.success && r ? r(i.value) : i; | ||
if (!u.success) return (s = u); | ||
l = i.success && r ? r(i.value) : i; | ||
if (!l.success) return (s = l); | ||
if (a) { | ||
@@ -128,8 +128,8 @@ const e = ((e, t) => | ||
}" when it contains cycles.`, | ||
))(s.value, u.value), | ||
))(s.value, l.value), | ||
t = e.success && n && innerGuard(n, e.value, createGuardVisitedState()); | ||
s = t || e; | ||
} else { | ||
const e = n && innerGuard(n, u.value, createGuardVisitedState()); | ||
s = e || u; | ||
const e = n && innerGuard(n, l.value, createGuardVisitedState()); | ||
s = e || l; | ||
} | ||
@@ -157,3 +157,3 @@ return s.success && (o.placeholder = s.value), s; | ||
if (void 0 !== i) return i; | ||
const u = o.p( | ||
const l = o.p( | ||
r, | ||
@@ -163,3 +163,3 @@ (e, t) => innerValidate(e, t, n), | ||
); | ||
return u.cycle ? (a.set(e, (a.get(e) || new Map()).set(r, u)), u) : u; | ||
return l.cycle ? (a.set(e, (a.get(e) || new Map()).set(r, l)), l) : l; | ||
} | ||
@@ -176,3 +176,3 @@ function innerSerialize(e, t, r) { | ||
if (void 0 !== i) return i; | ||
let u = (o.s || o.p)( | ||
let l = (o.s || o.p)( | ||
r, | ||
@@ -182,3 +182,3 @@ (e, t) => innerSerialize(e, t, n), | ||
); | ||
return u.cycle ? (a.set(e, (a.get(e) || new Map()).set(r, u)), u) : u; | ||
return l.cycle ? (a.set(e, (a.get(e) || new Map()).set(r, l)), l) : l; | ||
} | ||
@@ -281,11 +281,11 @@ function innerGuard(e, r, n) { | ||
for (let i = 0; i < t.length; i++) { | ||
const u = n(e, t[i]); | ||
u.success | ||
? (s[i] = u.value) | ||
const l = n(e, t[i]); | ||
l.success | ||
? (s[i] = l.value) | ||
: (o || (o = unableToAssign(t, r)), | ||
o.push(typesAreNotCompatible(`[${i}]`, u)), | ||
o.push(typesAreNotCompatible(`[${i}]`, l)), | ||
(a = | ||
a || | ||
failure(u.message, { | ||
key: u.key ? `[${i}].${u.key}` : `[${i}]`, | ||
failure(l.message, { | ||
key: l.key ? `[${i}].${l.key}` : `[${i}]`, | ||
fullError: o, | ||
@@ -315,5 +315,5 @@ }))); | ||
if (!i.success) return i; | ||
const u = t(i.value); | ||
if (!u || "string" == typeof u) { | ||
const e = "string" == typeof u ? u : `${showValue(s)} failed ${o || "constraint"} check`; | ||
const l = t(i.value); | ||
if (!l || "string" == typeof l) { | ||
const e = "string" == typeof l ? l : `${showValue(s)} failed ${o || "constraint"} check`; | ||
return failure(e, { fullError: unableToAssign(s, n, e) }); | ||
@@ -424,12 +424,12 @@ } | ||
i = void 0; | ||
for (const u in e) | ||
if (!t || (hasKey(u, r) && void 0 !== r[u])) { | ||
const l = t || hasKey(u, r) ? r[u] : void 0; | ||
let c = s(e[u], l); | ||
for (const l in e) | ||
if (!t || (hasKey(l, r) && void 0 !== r[l])) { | ||
const u = t || hasKey(l, r) ? r[l] : void 0; | ||
let c = s(e[l], u); | ||
c.success | ||
? (a[u] = c.value) | ||
? (a[l] = c.value) | ||
: (i || (i = unableToAssign(r, n)), | ||
i.push(typesAreNotCompatible(`"${u}"`, c)), | ||
i.push(typesAreNotCompatible(`"${l}"`, c)), | ||
(o = | ||
o || failure(c.message, { key: c.key ? `${u}.${c.key}` : u, fullError: i }))); | ||
o || failure(c.message, { key: c.key ? `${l}.${c.key}` : l, fullError: i }))); | ||
} | ||
@@ -712,4 +712,4 @@ return o; | ||
i = createPrimative("function"), | ||
u = createPrimative("number"), | ||
l = createPrimative("string"), | ||
l = createPrimative("number"), | ||
u = createPrimative("string"), | ||
c = createPrimative("symbol"); | ||
@@ -806,6 +806,13 @@ (exports.Array = e => InternalArr(e, !1)), | ||
(exports.Null = s), | ||
(exports.Number = u), | ||
(exports.Number = l), | ||
(exports.Object = e => InternalObject(e, !1, !1)), | ||
(exports.ParsedValue = ParsedValue), | ||
(exports.Partial = e => InternalObject(e, !0, !1)), | ||
(exports.Readonly = function Readonly(e) { | ||
const t = { ...e }; | ||
t.isReadonly = !0; | ||
for (const r of ["asPartial", "pick", "omit"]) | ||
"function" == typeof e[r] && (t[r] = (...t) => Readonly(e[r](...t))); | ||
return t; | ||
}), | ||
(exports.ReadonlyArray = e => InternalArr(e, !0)), | ||
@@ -835,5 +842,5 @@ (exports.Record = (e, t) => { | ||
if (!i.success) return expected("record key to be " + show(e), o); | ||
const u = a(t, s[o]); | ||
if (!u.success) return failure(u.message, { key: u.key ? `${o}.${u.key}` : o }); | ||
n[i.value] = u.value; | ||
const l = a(t, s[o]); | ||
if (!l.success) return failure(l.message, { key: l.key ? `${o}.${l.key}` : o }); | ||
n[i.value] = l.value; | ||
} | ||
@@ -844,2 +851,3 @@ }), | ||
value: t, | ||
isReadonly: !1, | ||
show() { | ||
@@ -852,3 +860,3 @@ return `{ [_: ${show(e, !1)}]: ${show(t, !1)} }`; | ||
}), | ||
(exports.String = l), | ||
(exports.String = u), | ||
(exports.Symbol = c), | ||
@@ -867,11 +875,11 @@ (exports.Tuple = (...e) => { | ||
for (let i = 0; i < e.length; i++) { | ||
let u = n(e[i], r[i]); | ||
u.success | ||
? (s[i] = u.value) | ||
let l = n(e[i], r[i]); | ||
l.success | ||
? (s[i] = l.value) | ||
: (o || (o = unableToAssign(r, t)), | ||
o.push(typesAreNotCompatible(`[${i}]`, u)), | ||
o.push(typesAreNotCompatible(`[${i}]`, l)), | ||
(a = | ||
a || | ||
failure(u.message, { | ||
key: u.key ? `[${i}].${u.key}` : `[${i}]`, | ||
failure(l.message, { | ||
key: l.key ? `[${i}].${l.key}` : `[${i}]`, | ||
fullError: o, | ||
@@ -885,2 +893,3 @@ }))); | ||
components: e, | ||
isReadonly: !1, | ||
show() { | ||
@@ -887,0 +896,0 @@ return `[${e.map(e => show(e, !1)).join(", ")}]`; |
@@ -14,3 +14,12 @@ import { Static, RuntypeBase, Codec } from '../runtype'; | ||
readonly value: V; | ||
readonly isReadonly: false; | ||
} | ||
export interface ReadonlyRecord<K extends KeyRuntypeBase, V extends RuntypeBase<unknown>> extends Codec<{ | ||
readonly [_ in Static<K>]?: Static<V>; | ||
}> { | ||
readonly tag: 'record'; | ||
readonly key: K; | ||
readonly value: V; | ||
readonly isReadonly: true; | ||
} | ||
/** | ||
@@ -17,0 +26,0 @@ * Construct a runtype for arbitrary dictionaries. |
@@ -5,6 +5,15 @@ import { RuntypeBase, Codec } from '../runtype'; | ||
}; | ||
export declare type ReadonlyStaticTuple<TElements extends readonly RuntypeBase<unknown>[]> = { | ||
readonly [key in keyof TElements]: TElements[key] extends RuntypeBase<infer E> ? E : unknown; | ||
}; | ||
export interface Tuple<TElements extends readonly RuntypeBase<unknown>[] = readonly RuntypeBase<unknown>[]> extends Codec<StaticTuple<TElements>> { | ||
readonly tag: 'tuple'; | ||
readonly components: TElements; | ||
readonly isReadonly: false; | ||
} | ||
export interface ReadonlyTuple<TElements extends readonly RuntypeBase<unknown>[] = readonly RuntypeBase<unknown>[]> extends Codec<ReadonlyStaticTuple<TElements>> { | ||
readonly tag: 'tuple'; | ||
readonly components: TElements; | ||
readonly isReadonly: true; | ||
} | ||
export declare function isTupleRuntype(runtype: RuntypeBase): runtype is Tuple<readonly RuntypeBase[]>; | ||
@@ -11,0 +20,0 @@ /** |
{ | ||
"name": "funtypes", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Runtime validation for static types", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -25,2 +25,3 @@ export { AsyncContract } from './asynccontract'; | ||
export { Boolean, Function, Number, String, Symbol } from './types/primative'; | ||
export { Readonly } from './types/Readonly'; | ||
export { Record } from './types/Record'; | ||
@@ -27,0 +28,0 @@ export { Tuple } from './types/tuple'; |
@@ -48,4 +48,13 @@ import { | ||
readonly value: V; | ||
readonly isReadonly: false; | ||
} | ||
export interface ReadonlyRecord<K extends KeyRuntypeBase, V extends RuntypeBase<unknown>> | ||
extends Codec<{ readonly [_ in Static<K>]?: Static<V> }> { | ||
readonly tag: 'record'; | ||
readonly key: K; | ||
readonly value: V; | ||
readonly isReadonly: true; | ||
} | ||
/** | ||
@@ -108,2 +117,3 @@ * Construct a runtype for arbitrary dictionaries. | ||
value, | ||
isReadonly: false, | ||
show() { | ||
@@ -110,0 +120,0 @@ return `{ [_: ${show(key, false)}]: ${show(value, false)} }`; |
@@ -15,2 +15,5 @@ import { | ||
}; | ||
export type ReadonlyStaticTuple<TElements extends readonly RuntypeBase<unknown>[]> = { | ||
readonly [key in keyof TElements]: TElements[key] extends RuntypeBase<infer E> ? E : unknown; | ||
}; | ||
@@ -22,4 +25,13 @@ export interface Tuple< | ||
readonly components: TElements; | ||
readonly isReadonly: false; | ||
} | ||
export interface ReadonlyTuple< | ||
TElements extends readonly RuntypeBase<unknown>[] = readonly RuntypeBase<unknown>[] | ||
> extends Codec<ReadonlyStaticTuple<TElements>> { | ||
readonly tag: 'tuple'; | ||
readonly components: TElements; | ||
readonly isReadonly: true; | ||
} | ||
export function isTupleRuntype(runtype: RuntypeBase): runtype is Tuple<readonly RuntypeBase[]> { | ||
@@ -73,2 +85,3 @@ return 'tag' in runtype && (runtype as Tuple<readonly RuntypeBase[]>).tag === 'tuple'; | ||
components, | ||
isReadonly: false, | ||
show() { | ||
@@ -75,0 +88,0 @@ return `[${(components as readonly RuntypeBase<unknown>[]) |
Sorry, the diff of this file is not supported yet
271690
89
7498