Comparing version 3.0.0 to 3.0.1
{ | ||
"index.js": { | ||
"bundled": 25214, | ||
"minified": 16299, | ||
"gzipped": 5093 | ||
"bundled": 25249, | ||
"minified": 16327, | ||
"gzipped": 5099 | ||
}, | ||
"index.mjs": { | ||
"bundled": 24829, | ||
"minified": 16189, | ||
"gzipped": 5062, | ||
"bundled": 24842, | ||
"minified": 16199, | ||
"gzipped": 5064, | ||
"treeshaked": { | ||
@@ -12,0 +12,0 @@ "rollup": { |
@@ -5,3 +5,4 @@ export { AsyncContract } from './asynccontract'; | ||
export type { Runtype, Codec, Static } from './runtype'; | ||
export type { Success, Failure, Result, showError } from './result'; | ||
export type { Success, Failure, Result } from './result'; | ||
export { showError } from './result'; | ||
export { ValidationError } from './errors'; | ||
@@ -8,0 +9,0 @@ export { default as showType } from './show'; |
265
lib/index.js
@@ -11,3 +11,3 @@ "use strict"; | ||
{ | ||
p: (r, n, a) => | ||
p: (r, n, s) => | ||
((e, t, r) => { | ||
@@ -25,3 +25,3 @@ if (!e.success) return e; | ||
); | ||
})(a(e, r), e => t.parse(e), t.test), | ||
})(s(e, r), e => t.parse(e), t.test), | ||
t(r, n) { | ||
@@ -32,3 +32,3 @@ return t.test | ||
}, | ||
s(r, n, a) { | ||
s(r, n, s) { | ||
if (!t.serialize) | ||
@@ -38,6 +38,6 @@ return failure( | ||
); | ||
const s = t.test ? innerGuard(t.test, r, createGuardVisitedState()) : void 0; | ||
if (s) return s; | ||
const a = t.test ? innerGuard(t.test, r, createGuardVisitedState()) : void 0; | ||
if (a) return a; | ||
const o = t.serialize(r); | ||
return o.success ? a(e, o.value) : o; | ||
return o.success ? s(e, o.value) : o; | ||
}, | ||
@@ -60,12 +60,12 @@ }, | ||
function safeParse(e) { | ||
return innerValidate(a, e, createVisitedState()); | ||
return innerValidate(s, e, createVisitedState()); | ||
} | ||
function safeSerialize(e) { | ||
return innerSerialize(a, e, createVisitedState()); | ||
return innerSerialize(s, e, createVisitedState()); | ||
} | ||
const a = { | ||
const s = { | ||
...n, | ||
tag: e, | ||
assert(e) { | ||
const t = innerGuard(a, e, createGuardVisitedState()); | ||
const t = innerGuard(s, e, createGuardVisitedState()); | ||
if (t) throw new ValidationError(t); | ||
@@ -79,3 +79,3 @@ }, | ||
safeParse, | ||
test: e => void 0 === innerGuard(a, e, createGuardVisitedState()), | ||
test: e => void 0 === innerGuard(s, e, createGuardVisitedState()), | ||
serialize(e) { | ||
@@ -87,12 +87,12 @@ const t = safeSerialize(e); | ||
safeSerialize, | ||
Or: e => Union(a, e), | ||
And: e => Intersect(a, e), | ||
withConstraint: (e, t) => Constraint(a, e, t), | ||
withGuard: (e, t) => Constraint(a, e, t), | ||
withBrand: e => Brand(e, a), | ||
withParser: e => ParsedValue(a, e), | ||
toString: () => `Runtype<${show(a)}>`, | ||
Or: e => Union(s, e), | ||
And: e => Intersect(s, e), | ||
withConstraint: (e, t) => Constraint(s, e, t), | ||
withGuard: (e, t) => Constraint(s, e, t), | ||
withBrand: e => Brand(e, s), | ||
withParser: e => ParsedValue(s, e), | ||
toString: () => `Runtype<${show(s)}>`, | ||
[t]: "function" == typeof r ? { p: r } : r, | ||
}; | ||
return a; | ||
return s; | ||
} | ||
@@ -103,4 +103,4 @@ function createValidationPlaceholder(e, t) { | ||
function innerMapValidationPlaceholder(e, t, r, n) { | ||
let a, | ||
s = !1; | ||
let s, | ||
a = !1; | ||
const o = { | ||
@@ -111,8 +111,8 @@ success: !0, | ||
unwrap() { | ||
if (a) return (s = !0), a; | ||
a = success(e); | ||
if (s) return (a = !0), s; | ||
s = success(e); | ||
const i = t(), | ||
l = i.success && r ? r(i.value) : i; | ||
if (!l.success) return l; | ||
if (s) { | ||
if (a) { | ||
const e = ((e, t) => | ||
@@ -136,10 +136,10 @@ e === t | ||
}" when it contains cycles.`, | ||
))(a.value, l.value), | ||
))(s.value, l.value), | ||
t = e.success && n && innerGuard(n, e.value, createGuardVisitedState()); | ||
a = t || e; | ||
s = t || e; | ||
} else { | ||
const e = n && innerGuard(n, l.value, createGuardVisitedState()); | ||
a = e || l; | ||
s = e || l; | ||
} | ||
return a.success && (o.placeholder = a.value), a; | ||
return s.success && (o.placeholder = s.value), s; | ||
}, | ||
@@ -160,6 +160,6 @@ }; | ||
function innerValidateToPlaceholder(e, r, n) { | ||
var a; | ||
const s = n, | ||
var s; | ||
const a = n, | ||
o = e[t], | ||
i = null === (a = s.get(e)) || void 0 === a ? void 0 : a.get(r); | ||
i = null === (s = a.get(e)) || void 0 === s ? void 0 : s.get(r); | ||
if (void 0 !== i) return i; | ||
@@ -171,3 +171,3 @@ const l = o.p( | ||
); | ||
return l.cycle ? (s.set(e, (s.get(e) || new Map()).set(r, l)), l) : l; | ||
return l.cycle ? (a.set(e, (a.get(e) || new Map()).set(r, l)), l) : l; | ||
} | ||
@@ -179,6 +179,6 @@ function innerSerialize(e, t, r) { | ||
function innerSerializeToPlaceholder(e, r, n) { | ||
var a; | ||
const s = n, | ||
var s; | ||
const a = n, | ||
o = e[t], | ||
i = null === (a = s.get(e)) || void 0 === a ? void 0 : a.get(r); | ||
i = null === (s = a.get(e)) || void 0 === s ? void 0 : s.get(r); | ||
if (void 0 !== i) return i; | ||
@@ -190,11 +190,11 @@ let l = (o.s || o.p)( | ||
); | ||
return l.cycle ? (s.set(e, (s.get(e) || new Map()).set(r, l)), l) : l; | ||
return l.cycle ? (a.set(e, (a.get(e) || new Map()).set(r, l)), l) : l; | ||
} | ||
function innerGuard(e, r, n) { | ||
var a; | ||
const s = n, | ||
var s; | ||
const a = n, | ||
o = e[t]; | ||
if (r && ("object" == typeof r || "function" == typeof r)) { | ||
if (null === (a = s.get(e)) || void 0 === a ? void 0 : a.has(r)) return; | ||
s.set(e, (s.get(e) || new Set()).add(r)); | ||
if (null === (s = a.get(e)) || void 0 === s ? void 0 : s.has(r)) return; | ||
a.set(e, (a.get(e) || new Set()).add(r)); | ||
} | ||
@@ -223,6 +223,6 @@ if (o.t) return o.t(r, (e, t) => innerGuard(e, t, n)); | ||
let t = "[", | ||
a = 0; | ||
for (a = 0; a < e.length && n > t.length; a++) | ||
0 !== a && (t += ", "), (t += showValue(e[a], r - 1, n - t.length)); | ||
return a < e.length && (t += " ... "), (t += "]"), t; | ||
s = 0; | ||
for (s = 0; s < e.length && n > t.length; s++) | ||
0 !== s && (t += ", "), (t += showValue(e[s], r - 1, n - t.length)); | ||
return s < e.length && (t += " ... "), (t += "]"), t; | ||
} | ||
@@ -234,10 +234,10 @@ } | ||
const t = Object.entries(e); | ||
let a = "{", | ||
s = 0; | ||
for (s = 0; s < t.length && n > a.length; s++) { | ||
0 !== s && (a += ", "); | ||
const [e, o] = t[s]; | ||
a += `${/\s/.test(e) ? JSON.stringify(e) : e}: ${showValue(o, r - 1, n - a.length)}`; | ||
let s = "{", | ||
a = 0; | ||
for (a = 0; a < t.length && n > s.length; a++) { | ||
0 !== a && (s += ", "); | ||
const [e, o] = t[a]; | ||
s += `${/\s/.test(e) ? JSON.stringify(e) : e}: ${showValue(o, r - 1, n - s.length)}`; | ||
} | ||
return s < t.length && (a += " ... "), (a += "}"), a; | ||
return a < t.length && (s += " ... "), (s += "}"), s; | ||
} | ||
@@ -284,4 +284,4 @@ case "function": | ||
Array.isArray(t) | ||
? createValidationPlaceholder([...t], a => { | ||
let s, | ||
? createValidationPlaceholder([...t], s => { | ||
let a, | ||
o = void 0; | ||
@@ -291,7 +291,7 @@ for (let i = 0; i < t.length; i++) { | ||
l.success | ||
? (a[i] = l.value) | ||
? (s[i] = l.value) | ||
: (o || (o = unableToAssign(t, r)), | ||
o.push(typesAreNotCompatible(`[${i}]`, l)), | ||
(s = | ||
s || | ||
(a = | ||
a || | ||
failure(l.message, { | ||
@@ -302,3 +302,3 @@ key: l.key ? `[${i}].${l.key}` : `[${i}]`, | ||
} | ||
return s; | ||
return a; | ||
}) | ||
@@ -320,10 +320,10 @@ : expected("an Array", t), | ||
"constraint", | ||
(a, s) => { | ||
(s, a) => { | ||
const o = r && r.name, | ||
i = s(e, a); | ||
i = a(e, s); | ||
if (!i.success) return i; | ||
const l = t(i.value); | ||
if (!l || "string" == typeof l) { | ||
const e = "string" == typeof l ? l : `${showValue(a)} failed ${o || "constraint"} check`; | ||
return failure(e, { fullError: unableToAssign(a, n, e) }); | ||
const e = "string" == typeof l ? l : `${showValue(s)} failed ${o || "constraint"} check`; | ||
return failure(e, { fullError: unableToAssign(s, n, e) }); | ||
} | ||
@@ -366,4 +366,4 @@ return success(i.value); | ||
return createValidationPlaceholder({}, n => { | ||
for (const a of e) { | ||
let e = r(a, t); | ||
for (const s of e) { | ||
let e = r(s, t); | ||
if (!e.success) return e; | ||
@@ -373,3 +373,3 @@ if (!e.value || "object" != typeof e.value) | ||
`The validator ${show( | ||
a, | ||
s, | ||
)} attempted to convert the type of this value from an object to something else. That conversion is not valid as the child of an intersect`, | ||
@@ -433,3 +433,3 @@ ); | ||
"object", | ||
(r, a) => | ||
(r, s) => | ||
null == r || "object" != typeof r | ||
@@ -439,3 +439,3 @@ ? expected(n, r) | ||
? failure(`Expected ${show(n)}, but was an Array`) | ||
: createValidationPlaceholder({}, s => { | ||
: createValidationPlaceholder({}, a => { | ||
let o, | ||
@@ -446,5 +446,5 @@ i = void 0; | ||
const u = t || hasKey(l, r) ? r[l] : void 0; | ||
let c = a(e[l], u); | ||
let c = s(e[l], u); | ||
c.success | ||
? (s[l] = c.value) | ||
? (a[l] = c.value) | ||
: (i || (i = unableToAssign(r, n)), | ||
@@ -464,10 +464,10 @@ i.push(typesAreNotCompatible(`"${l}"`, c)), | ||
pick(...n) { | ||
const a = {}; | ||
for (const t of n) a[t] = e[t]; | ||
return InternalObject(a, t, r); | ||
const s = {}; | ||
for (const t of n) s[t] = e[t]; | ||
return InternalObject(s, t, r); | ||
}, | ||
omit(...n) { | ||
const a = { ...e }; | ||
for (const e of n) e in a && delete a[e]; | ||
return InternalObject(a, t, r); | ||
const s = { ...e }; | ||
for (const e of n) e in s && delete s[e]; | ||
return InternalObject(s, t, r); | ||
}, | ||
@@ -539,3 +539,3 @@ show() { | ||
? resolveUnderlyingType(e.config.test, t) | ||
: s | ||
: a | ||
: "s" === t && isParsedValueRuntype(e) | ||
@@ -546,3 +546,3 @@ ? e.config.serialize | ||
: e | ||
: s | ||
: a | ||
: e; | ||
@@ -552,7 +552,7 @@ } | ||
function validateWithKey(e, t) { | ||
return (n, a) => { | ||
return (n, s) => { | ||
if (!n || "object" != typeof n) return expected(r, n); | ||
const s = t.get(n[e]); | ||
if (s) { | ||
const t = a(s, n); | ||
const a = t.get(n[e]); | ||
if (a) { | ||
const t = s(a, n); | ||
return t.success | ||
@@ -566,3 +566,3 @@ ? t | ||
{ | ||
const a = expected( | ||
const s = expected( | ||
Array.from(t.keys()) | ||
@@ -575,8 +575,8 @@ .map(e => ("string" == typeof e ? `'${e}'` : e)) | ||
return ( | ||
(a.fullError = unableToAssign( | ||
(s.fullError = unableToAssign( | ||
n, | ||
r, | ||
typesAreNotCompatible(0 === e ? "[0]" : `"${e}"`, a.message), | ||
typesAreNotCompatible(0 === e ? "[0]" : `"${e}"`, s.message), | ||
)), | ||
a | ||
s | ||
); | ||
@@ -594,7 +594,7 @@ } | ||
const e = {}; | ||
for (let a = 0; a < n.length; a++) | ||
for (const s in r[a].fields) { | ||
const o = resolveUnderlyingType(r[a].fields[s], t); | ||
for (let s = 0; s < n.length; s++) | ||
for (const a in r[s].fields) { | ||
const o = resolveUnderlyingType(r[s].fields[a], t); | ||
isLiteralRuntype(o) && | ||
(e[s] || (e[s] = new Map()), e[s].has(o.value) || e[s].set(o.value, n[a])); | ||
(e[a] || (e[a] = new Map()), e[a].has(o.value) || e[a].set(o.value, n[s])); | ||
} | ||
@@ -604,8 +604,8 @@ for (const t of ["type", "kind", "tag", ...Object.keys(e)]) | ||
} | ||
const a = e.filter(isTupleRuntype); | ||
if (a.length === n.length) { | ||
const s = e.filter(isTupleRuntype); | ||
if (s.length === n.length) { | ||
const e = new Map(); | ||
for (let r = 0; r < n.length; r++) { | ||
const s = resolveUnderlyingType(a[r].components[0], t); | ||
isLiteralRuntype(s) && (e.has(s.value) || e.set(s.value, n[r])); | ||
const a = resolveUnderlyingType(s[r].components[0], t); | ||
isLiteralRuntype(a) && (e.has(a.value) || e.set(a.value, n[r])); | ||
} | ||
@@ -616,4 +616,4 @@ if (e.size === n.length) return validateWithKey(0, e); | ||
return (t, n) => { | ||
let a, | ||
s, | ||
let s, | ||
a, | ||
o, | ||
@@ -627,6 +627,6 @@ i = 0; | ||
: (o = unableToAssign(t, r, e.fullError || unableToAssign(t, l, e))), | ||
e.key && (i++, (a = e), (s = l)); | ||
e.key && (i++, (s = e), (a = l)); | ||
} | ||
return a && s && 1 === i | ||
? failure(a.message, { key: `<${show(s)}>.${a.key}`, fullError: o }) | ||
return s && a && 1 === i | ||
? failure(s.message, { key: `<${show(a)}>.${s.key}`, fullError: o }) | ||
: expected(r, t, { fullError: o }); | ||
@@ -649,6 +649,6 @@ }; | ||
match: (...t) => n => { | ||
const a = createVisitedState(); | ||
const s = createVisitedState(); | ||
for (let r = 0; r < e.length; r++) { | ||
const s = innerValidate(e[r], n, a); | ||
if (s.success) return t[r](s.value); | ||
const a = innerValidate(e[r], n, s); | ||
if (a.success) return t[r](a.value); | ||
} | ||
@@ -691,4 +691,4 @@ r.assert(n); | ||
n = Literal(void 0), | ||
a = Literal(null), | ||
s = create("never", e => expected("nothing", e), {}), | ||
s = Literal(null), | ||
a = create("never", e => expected("nothing", e), {}), | ||
o = createPrimative("boolean"), | ||
@@ -708,11 +708,11 @@ i = createPrimative("function"), | ||
); | ||
const a = createVisitedState(); | ||
const s = createVisitedState(); | ||
for (let t = 0; t < e.length; t++) { | ||
const r = innerValidate(e[t], n[t], a); | ||
const r = innerValidate(e[t], n[t], s); | ||
if (!r.success) return Promise.reject(new ValidationError(r)); | ||
n[t] = r.value; | ||
} | ||
const s = r(...n); | ||
return s instanceof Promise | ||
? s.then(e => { | ||
const a = r(...n); | ||
return a instanceof Promise | ||
? a.then(e => { | ||
const r = innerGuard(t, e, createGuardVisitedState()); | ||
@@ -724,3 +724,3 @@ if (r) throw new ValidationError(r); | ||
new ValidationError({ | ||
message: "Expected function to return a promise, but instead got " + s, | ||
message: "Expected function to return a promise, but instead got " + a, | ||
}), | ||
@@ -739,12 +739,12 @@ ); | ||
}); | ||
const a = createVisitedState(); | ||
const s = createVisitedState(); | ||
for (let t = 0; t < e.length; t++) { | ||
const r = innerValidate(e[t], n[t], a); | ||
const r = innerValidate(e[t], n[t], s); | ||
if (!r.success) throw new ValidationError(r); | ||
n[t] = r.value; | ||
} | ||
const s = r(...n), | ||
o = innerGuard(t, s, createGuardVisitedState()); | ||
const a = r(...n), | ||
o = innerGuard(t, a, createGuardVisitedState()); | ||
if (o) throw new ValidationError(o); | ||
return s; | ||
return a; | ||
}, | ||
@@ -780,4 +780,4 @@ })), | ||
(exports.Literal = Literal), | ||
(exports.Never = s), | ||
(exports.Null = a), | ||
(exports.Never = a), | ||
(exports.Null = s), | ||
(exports.Number = l), | ||
@@ -793,21 +793,21 @@ (exports.Object = e => InternalObject(e, !1, !1)), | ||
"record", | ||
(a, s) => | ||
null == a || "object" != typeof a | ||
? expected(n, a) | ||
: Object.getPrototypeOf(a) !== Object.prototype | ||
? Array.isArray(a) | ||
(s, a) => | ||
null == s || "object" != typeof s | ||
? expected(n, s) | ||
: Object.getPrototypeOf(s) !== Object.prototype | ||
? Array.isArray(s) | ||
? failure("Expected Record, but was Array") | ||
: failure(`Expected ${show(n)}, but was ${Object.getPrototypeOf(a)}`) | ||
: failure(`Expected ${show(n)}, but was ${Object.getPrototypeOf(s)}`) | ||
: createValidationPlaceholder({}, n => { | ||
for (const o in a) { | ||
for (const o in s) { | ||
let i = null; | ||
if ("number" === r()) { | ||
if (isNaN(+o)) return expected("record key to be a number", o); | ||
i = s(e, +o); | ||
i = a(e, +o); | ||
} else | ||
"string" === r() | ||
? (i = s(e, o)) | ||
: ((i = s(e, o)), i.success || isNaN(+o) || (i = s(e, +o))); | ||
? (i = a(e, o)) | ||
: ((i = a(e, o)), i.success || isNaN(+o) || (i = a(e, +o))); | ||
if (!i.success) return expected("record key to be " + show(e), o); | ||
const l = s(t, a[o]); | ||
const l = a(t, s[o]); | ||
if (!l.success) return failure(l.message, { key: l.key ? `${o}.${l.key}` : o }); | ||
@@ -837,4 +837,4 @@ n[i.value] = l.value; | ||
? expected("an array of length " + e.length, r.length) | ||
: createValidationPlaceholder([...r], a => { | ||
let s, | ||
: createValidationPlaceholder([...r], s => { | ||
let a, | ||
o = void 0; | ||
@@ -844,7 +844,7 @@ for (let i = 0; i < e.length; i++) { | ||
l.success | ||
? (a[i] = l.value) | ||
? (s[i] = l.value) | ||
: (o || (o = unableToAssign(r, t)), | ||
o.push(typesAreNotCompatible(`[${i}]`, l)), | ||
(s = | ||
s || | ||
(a = | ||
a || | ||
failure(l.message, { | ||
@@ -855,3 +855,3 @@ key: l.key ? `[${i}].${l.key}` : `[${i}]`, | ||
} | ||
return s; | ||
return a; | ||
}) | ||
@@ -875,3 +875,4 @@ : expected("tuple to be an array", r), | ||
}), | ||
(exports.showError = showError), | ||
(exports.showType = show), | ||
(exports.showValue = showValue); |
{ | ||
"name": "funtypes", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Runtime validation for static types", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -5,3 +5,4 @@ export { AsyncContract } from './asynccontract'; | ||
export type { Runtype, Codec, Static } from './runtype'; | ||
export type { Success, Failure, Result, showError } from './result'; | ||
export type { Success, Failure, Result } from './result'; | ||
export { showError } from './result'; | ||
export { ValidationError } from './errors'; | ||
@@ -8,0 +9,0 @@ export { default as showType } from './show'; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
243869
6766