New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@badrap/valita

Package Overview
Dependencies
Maintainers
2
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@badrap/valita - npm Package Compare versions

Comparing version 0.0.19 to 0.0.20

1056

dist/cjs/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.err = exports.ok = exports.lazy = exports.undefined = exports.null = exports.union = exports.literal = exports.tuple = exports.array = exports.record = exports.object = exports.boolean = exports.string = exports.bigint = exports.number = exports.unknown = exports.never = exports.ValitaError = void 0;
var tslib_1 = require("tslib");
function _collectIssues(tree, path, issues) {

@@ -17,5 +16,5 @@ var _a;

else {
var finalPath = path.slice();
const finalPath = path.slice();
if (tree.path) {
finalPath.push.apply(finalPath, tree.path);
finalPath.push(...tree.path);
}

@@ -25,10 +24,10 @@ if (tree.code === "custom_error" &&

((_a = tree.error) === null || _a === void 0 ? void 0 : _a.path)) {
finalPath.push.apply(finalPath, tree.error.path);
finalPath.push(...tree.error.path);
}
issues.push(tslib_1.__assign(tslib_1.__assign({}, tree), { path: finalPath }));
issues.push(Object.assign(Object.assign({}, tree), { path: finalPath }));
}
}
function collectIssues(tree) {
var issues = [];
var path = [];
const issues = [];
const path = [];
_collectIssues(tree, path, issues);

@@ -41,14 +40,13 @@ return issues;

}
var last = list[list.length - 1];
const last = list[list.length - 1];
if (list.length < 2) {
return last;
}
return list.slice(0, -1).join(", ") + " or " + last;
return `${list.slice(0, -1).join(", ")} or ${last}`;
}
function formatLiteral(value) {
return typeof value === "bigint" ? value + "n" : JSON.stringify(value);
return typeof value === "bigint" ? `${value}n` : JSON.stringify(value);
}
function findOneIssue(tree, path) {
function findOneIssue(tree, path = []) {
var _a;
if (path === void 0) { path = []; }
if (tree.code === "join") {

@@ -63,3 +61,3 @@ return findOneIssue(tree.left, path);

if (tree.path) {
path.push.apply(path, tree.path);
path.push(...tree.path);
}

@@ -69,5 +67,5 @@ if (tree.code === "custom_error" &&

((_a = tree.error) === null || _a === void 0 ? void 0 : _a.path)) {
path.push.apply(path, tree.error.path);
path.push(...tree.error.path);
}
return tslib_1.__assign(tslib_1.__assign({}, tree), { path: path });
return Object.assign(Object.assign({}, tree), { path });
}

@@ -87,40 +85,40 @@ }

function formatIssueTree(issueTree) {
var count = countIssues(issueTree);
var issue = findOneIssue(issueTree);
var path = issue.path || [];
var message = "validation failed";
const count = countIssues(issueTree);
const issue = findOneIssue(issueTree);
const path = issue.path || [];
let message = "validation failed";
if (issue.code === "invalid_type") {
message = "expected " + orList(issue.expected);
message = `expected ${orList(issue.expected)}`;
}
else if (issue.code === "invalid_literal") {
message = "expected " + orList(issue.expected.map(formatLiteral));
message = `expected ${orList(issue.expected.map(formatLiteral))}`;
}
else if (issue.code === "missing_key") {
message = "missing key " + formatLiteral(issue.key);
message = `missing key ${formatLiteral(issue.key)}`;
}
else if (issue.code === "unrecognized_key") {
message = "unrecognized key " + formatLiteral(issue.key);
message = `unrecognized key ${formatLiteral(issue.key)}`;
}
else if (issue.code === "invalid_length") {
var min = issue.minLength;
var max = issue.maxLength;
message = "expected an array with ";
const min = issue.minLength;
const max = issue.maxLength;
message = `expected an array with `;
if (min > 0) {
if (max === min) {
message += "" + min;
message += `${min}`;
}
else if (max < Infinity) {
message += "between " + min + " and " + max;
message += `between ${min} and ${max}`;
}
else {
message += "at least " + min;
message += `at least ${min}`;
}
}
else {
message += "at most " + max;
message += `at most ${max}`;
}
message += " item(s)";
message += ` item(s)`;
}
else if (issue.code === "custom_error") {
var error = issue.error;
const error = issue.error;
if (typeof error === "string") {

@@ -133,75 +131,58 @@ message = error;

}
var msg = issue.code + " at ." + path.join(".") + " (" + message + ")";
let msg = `${issue.code} at .${path.join(".")} (${message})`;
if (count === 2) {
msg += " (+ 1 other issue)";
msg += ` (+ 1 other issue)`;
}
else if (count > 2) {
msg += " (+ " + (count - 1) + " other issues)";
msg += ` (+ ${count - 1} other issues)`;
}
return msg;
}
var ValitaFailure = /** @class */ (function () {
function ValitaFailure(issueTree) {
class ValitaFailure {
constructor(issueTree) {
this.issueTree = issueTree;
this.ok = false;
}
Object.defineProperty(ValitaFailure.prototype, "issues", {
get: function () {
var issues = collectIssues(this.issueTree);
Object.defineProperty(this, "issues", {
value: issues,
writable: false,
});
return issues;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ValitaFailure.prototype, "message", {
get: function () {
var message = formatIssueTree(this.issueTree);
Object.defineProperty(this, "message", {
value: message,
writable: false,
});
return message;
},
enumerable: false,
configurable: true
});
ValitaFailure.prototype.throw = function () {
get issues() {
const issues = collectIssues(this.issueTree);
Object.defineProperty(this, "issues", {
value: issues,
writable: false,
});
return issues;
}
get message() {
const message = formatIssueTree(this.issueTree);
Object.defineProperty(this, "message", {
value: message,
writable: false,
});
return message;
}
throw() {
throw new ValitaError(this.issueTree);
};
return ValitaFailure;
}());
var ValitaError = /** @class */ (function (_super) {
tslib_1.__extends(ValitaError, _super);
function ValitaError(issueTree) {
var _newTarget = this.constructor;
var _this = _super.call(this, formatIssueTree(issueTree)) || this;
_this.issueTree = issueTree;
Object.setPrototypeOf(_this, _newTarget.prototype);
_this.name = _newTarget.name;
return _this;
}
Object.defineProperty(ValitaError.prototype, "issues", {
get: function () {
var issues = collectIssues(this.issueTree);
Object.defineProperty(this, "issues", {
value: issues,
writable: false,
});
return issues;
},
enumerable: false,
configurable: true
});
return ValitaError;
}(Error));
}
class ValitaError extends Error {
constructor(issueTree) {
super(formatIssueTree(issueTree));
this.issueTree = issueTree;
Object.setPrototypeOf(this, new.target.prototype);
this.name = new.target.name;
}
get issues() {
const issues = collectIssues(this.issueTree);
Object.defineProperty(this, "issues", {
value: issues,
writable: false,
});
return issues;
}
}
exports.ValitaError = ValitaError;
function joinIssues(left, right) {
return left ? { code: "join", left: left, right: right } : right;
return left ? { code: "join", left, right } : right;
}
function prependPath(key, tree) {
return { code: "prepend", key: key, tree: tree };
return { code: "prepend", key, tree };
}

@@ -212,3 +193,3 @@ function isObject(v) {

function toTerminals(type) {
var result = [];
const result = [];
type.toTerminals(result);

@@ -218,5 +199,5 @@ return result;

function hasTerminal(type, name) {
return toTerminals(type).some(function (t) { return t.name === name; });
return toTerminals(type).some((t) => t.name === name);
}
var Nothing = Symbol();
const Nothing = Symbol();
var FuncMode;

@@ -229,26 +210,20 @@ (function (FuncMode) {

function ok(value) {
return { ok: true, value: value };
return { ok: true, value };
}
exports.ok = ok;
function err(error) {
return { ok: false, error: error };
return { ok: false, error };
}
exports.err = err;
var AbstractType = /** @class */ (function () {
function AbstractType() {
class AbstractType {
get func() {
const f = this.genFunc();
Object.defineProperty(this, "func", {
value: f,
writable: false,
});
return f;
}
Object.defineProperty(AbstractType.prototype, "func", {
get: function () {
var f = this.genFunc();
Object.defineProperty(this, "func", {
value: f,
writable: false,
});
return f;
},
enumerable: false,
configurable: true
});
AbstractType.prototype.parse = function (v, options) {
var mode = FuncMode.PASS;
parse(v, options) {
let mode = FuncMode.PASS;
if (options && options.mode === "strict") {

@@ -260,3 +235,3 @@ mode = FuncMode.STRICT;

}
var r = this.func(v, mode);
const r = this.func(v, mode);
if (r === true) {

@@ -271,5 +246,5 @@ return v;

}
};
AbstractType.prototype.try = function (v, options) {
var mode = FuncMode.PASS;
}
try(v, options) {
let mode = FuncMode.PASS;
if (options && options.mode === "strict") {

@@ -281,3 +256,3 @@ mode = FuncMode.STRICT;

}
var r = this.func(v, mode);
const r = this.func(v, mode);
if (r === true) {

@@ -292,22 +267,22 @@ return { ok: true, value: v };

}
};
AbstractType.prototype.optional = function () {
}
optional() {
return new Optional(this);
};
AbstractType.prototype.default = function (defaultValue) {
}
default(defaultValue) {
return new DefaultType(this, defaultValue);
};
AbstractType.prototype.assert = function (func, error) {
var err = { code: "custom_error", error: error };
return new TransformType(this, function (v) { return (func(v) ? true : err); });
};
AbstractType.prototype.map = function (func) {
return new TransformType(this, function (v) { return ({
}
assert(func, error) {
const err = { code: "custom_error", error };
return new TransformType(this, (v) => (func(v) ? true : err));
}
map(func) {
return new TransformType(this, (v) => ({
code: "ok",
value: func(v),
}); });
};
AbstractType.prototype.chain = function (func) {
return new TransformType(this, function (v) {
var r = func(v);
}));
}
chain(func) {
return new TransformType(this, (v) => {
const r = func(v);
if (r.ok) {

@@ -320,52 +295,41 @@ return { code: "ok", value: r.value };

});
};
return AbstractType;
}());
var Type = /** @class */ (function (_super) {
tslib_1.__extends(Type, _super);
function Type() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Type;
}(AbstractType));
var Optional = /** @class */ (function (_super) {
tslib_1.__extends(Optional, _super);
function Optional(type) {
var _this = _super.call(this) || this;
_this.type = type;
_this.name = "optional";
return _this;
}
class Type extends AbstractType {
}
class Optional extends AbstractType {
constructor(type) {
super();
this.type = type;
this.name = "optional";
}
Optional.prototype.genFunc = function () {
var func = this.type.func;
return function (v, mode) {
genFunc() {
const func = this.type.func;
return (v, mode) => {
return v === undefined || v === Nothing ? true : func(v, mode);
};
};
Optional.prototype.toTerminals = function (into) {
}
toTerminals(into) {
into.push(this);
into.push(new UndefinedType());
this.type.toTerminals(into);
};
return Optional;
}(AbstractType));
var DefaultType = /** @class */ (function (_super) {
tslib_1.__extends(DefaultType, _super);
function DefaultType(type, defaultValue) {
var _this = _super.call(this) || this;
_this.type = type;
_this.defaultValue = defaultValue;
_this.name = "default";
return _this;
}
DefaultType.prototype.genFunc = function () {
var func = this.type.func;
var undefinedOutput = this.defaultValue === undefined
}
class DefaultType extends Type {
constructor(type, defaultValue) {
super();
this.type = type;
this.defaultValue = defaultValue;
this.name = "default";
}
genFunc() {
const func = this.type.func;
const undefinedOutput = this.defaultValue === undefined
? true
: { code: "ok", value: this.defaultValue };
var nothingOutput = {
const nothingOutput = {
code: "ok",
value: this.defaultValue,
};
return function (v, mode) {
return (v, mode) => {
if (v === undefined) {

@@ -378,3 +342,3 @@ return undefinedOutput;

else {
var result = func(v, mode);
const result = func(v, mode);
if (result !== true &&

@@ -388,41 +352,39 @@ result.code === "ok" &&

};
};
DefaultType.prototype.toTerminals = function (into) {
}
toTerminals(into) {
into.push(this.type.optional());
this.type.toTerminals(into);
};
return DefaultType;
}(Type));
var ObjectType = /** @class */ (function (_super) {
tslib_1.__extends(ObjectType, _super);
function ObjectType(shape, restType, checks) {
var _this = _super.call(this) || this;
_this.shape = shape;
_this.restType = restType;
_this.checks = checks;
_this.name = "object";
return _this;
}
ObjectType.prototype.toTerminals = function (into) {
}
class ObjectType extends Type {
constructor(shape, restType, checks) {
super();
this.shape = shape;
this.restType = restType;
this.checks = checks;
this.name = "object";
}
toTerminals(into) {
into.push(this);
};
ObjectType.prototype.check = function (func, error) {
}
check(func, error) {
var _a;
var issue = { code: "custom_error", error: error };
return new ObjectType(this.shape, this.restType, tslib_1.__spreadArray(tslib_1.__spreadArray([], ((_a = this.checks) !== null && _a !== void 0 ? _a : [])), [
const issue = { code: "custom_error", error };
return new ObjectType(this.shape, this.restType, [
...((_a = this.checks) !== null && _a !== void 0 ? _a : []),
{
func: func,
issue: issue,
issue,
},
]));
};
ObjectType.prototype.genFunc = function () {
var shape = this.shape;
var rest = this.restType ? this.restType.func : undefined;
var invalidType = { code: "invalid_type", expected: ["object"] };
var checks = this.checks;
var required = [];
var optional = [];
var knownKeys = Object.create(null);
for (var key in shape) {
]);
}
genFunc() {
const shape = this.shape;
const rest = this.restType ? this.restType.func : undefined;
const invalidType = { code: "invalid_type", expected: ["object"] };
const checks = this.checks;
const required = [];
const optional = [];
const knownKeys = Object.create(null);
for (const key in shape) {
if (hasTerminal(shape[key], "optional")) {

@@ -436,20 +398,20 @@ optional.push(key);

}
var keys = tslib_1.__spreadArray(tslib_1.__spreadArray([], required), optional);
var funcs = keys.map(function (key) { return shape[key].func; });
var requiredCount = required.length;
return function (obj, mode) {
const keys = [...required, ...optional];
const funcs = keys.map((key) => shape[key].func);
const requiredCount = required.length;
return (obj, mode) => {
if (!isObject(obj)) {
return invalidType;
}
var strict = mode === FuncMode.STRICT;
var strip = mode === FuncMode.STRIP;
var issueTree = undefined;
var output = obj;
var setKeys = false;
var copied = false;
const strict = mode === FuncMode.STRICT;
const strip = mode === FuncMode.STRIP;
let issueTree = undefined;
let output = obj;
let setKeys = false;
let copied = false;
if (strict || strip || rest) {
for (var key in obj) {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(knownKeys, key)) {
if (rest) {
var r = rest(obj[key], mode);
const r = rest(obj[key], mode);
if (r !== true) {

@@ -461,3 +423,3 @@ if (r.code !== "ok") {

if (!copied) {
output = tslib_1.__assign({}, obj);
output = Object.assign({}, obj);
copied = true;

@@ -470,3 +432,3 @@ }

else if (strict) {
return { code: "unrecognized_key", key: key };
return { code: "unrecognized_key", key };
}

@@ -482,9 +444,9 @@ else if (strip) {

}
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = obj[key];
var found = true;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let value = obj[key];
let found = true;
if (value === undefined && !(key in obj)) {
if (i < requiredCount) {
return { code: "missing_key", key: key };
return { code: "missing_key", key };
}

@@ -494,3 +456,3 @@ value = Nothing;

}
var r = funcs[i](value, mode);
const r = funcs[i](value, mode);
if (r === true) {

@@ -506,3 +468,3 @@ if (setKeys && found) {

if (!copied) {
output = tslib_1.__assign({}, obj);
output = Object.assign({}, obj);
copied = true;

@@ -514,3 +476,3 @@ }

if (checks && !issueTree) {
for (var i = 0; i < checks.length; i++) {
for (let i = 0; i < checks.length; i++) {
if (!checks[i].func(output)) {

@@ -526,81 +488,68 @@ return checks[i].issue;

};
};
ObjectType.prototype.rest = function (restType) {
}
rest(restType) {
return new ObjectType(this.shape, restType);
};
ObjectType.prototype.extend = function (shape) {
return new ObjectType(tslib_1.__assign(tslib_1.__assign({}, this.shape), shape), this.restType);
};
ObjectType.prototype.pick = function () {
var _this = this;
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
var shape = {};
keys.forEach(function (key) {
shape[key] = _this.shape[key];
}
extend(shape) {
return new ObjectType(Object.assign(Object.assign({}, this.shape), shape), this.restType);
}
pick(...keys) {
const shape = {};
keys.forEach((key) => {
shape[key] = this.shape[key];
});
return new ObjectType(shape, undefined);
};
ObjectType.prototype.omit = function () {
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
var shape = tslib_1.__assign({}, this.shape);
keys.forEach(function (key) {
}
omit(...keys) {
const shape = Object.assign({}, this.shape);
keys.forEach((key) => {
delete shape[key];
});
return new ObjectType(shape, this.restType);
};
ObjectType.prototype.partial = function () {
var _this = this;
}
partial() {
var _a;
var shape = {};
Object.keys(this.shape).forEach(function (key) {
shape[key] = _this.shape[key].optional();
const shape = {};
Object.keys(this.shape).forEach((key) => {
shape[key] = this.shape[key].optional();
});
var rest = (_a = this.restType) === null || _a === void 0 ? void 0 : _a.optional();
const rest = (_a = this.restType) === null || _a === void 0 ? void 0 : _a.optional();
return new ObjectType(shape, rest);
};
return ObjectType;
}(Type));
var ArrayType = /** @class */ (function (_super) {
tslib_1.__extends(ArrayType, _super);
function ArrayType(head, rest) {
var _this = _super.call(this) || this;
_this.head = head;
_this.rest = rest;
_this.name = "array";
return _this;
}
ArrayType.prototype.toTerminals = function (into) {
}
class ArrayType extends Type {
constructor(head, rest) {
super();
this.head = head;
this.rest = rest;
this.name = "array";
}
toTerminals(into) {
into.push(this);
};
ArrayType.prototype.genFunc = function () {
}
genFunc() {
var _a;
var headFuncs = this.head.map(function (t) { return t.func; });
var restFunc = ((_a = this.rest) !== null && _a !== void 0 ? _a : never()).func;
var minLength = headFuncs.length;
var maxLength = this.rest ? Infinity : minLength;
var invalidType = { code: "invalid_type", expected: ["array"] };
var invalidLength = {
const headFuncs = this.head.map((t) => t.func);
const restFunc = ((_a = this.rest) !== null && _a !== void 0 ? _a : never()).func;
const minLength = headFuncs.length;
const maxLength = this.rest ? Infinity : minLength;
const invalidType = { code: "invalid_type", expected: ["array"] };
const invalidLength = {
code: "invalid_length",
minLength: minLength,
maxLength: maxLength,
minLength,
maxLength,
};
return function (arr, mode) {
return (arr, mode) => {
if (!Array.isArray(arr)) {
return invalidType;
}
var length = arr.length;
const length = arr.length;
if (length < minLength || length > maxLength) {
return invalidLength;
}
var issueTree = undefined;
var output = arr;
for (var i = 0; i < arr.length; i++) {
var func = i < minLength ? headFuncs[i] : restFunc;
var r = func(arr[i], mode);
let issueTree = undefined;
let output = arr;
for (let i = 0; i < arr.length; i++) {
const func = i < minLength ? headFuncs[i] : restFunc;
const r = func(arr[i], mode);
if (r !== true) {

@@ -628,7 +577,6 @@ if (r.code === "ok") {

};
};
return ArrayType;
}(Type));
}
}
function toBaseType(v) {
var type = typeof v;
const type = typeof v;
if (type !== "object") {

@@ -648,5 +596,5 @@ return type;

function dedup(arr) {
var output = [];
var seen = new Set();
for (var i = 0; i < arr.length; i++) {
const output = [];
const seen = new Set();
for (let i = 0; i < arr.length; i++) {
if (!seen.has(arr[i])) {

@@ -660,10 +608,10 @@ output.push(arr[i]);

function findCommonKeys(rs) {
var map = new Map();
rs.forEach(function (r) {
for (var key in r) {
const map = new Map();
rs.forEach((r) => {
for (const key in r) {
map.set(key, (map.get(key) || 0) + 1);
}
});
var result = [];
map.forEach(function (count, key) {
const result = [];
map.forEach((count, key) => {
if (count === rs.length) {

@@ -676,24 +624,20 @@ result.push(key);

function createObjectMatchers(t) {
var objects = [];
t.forEach(function (_a) {
var root = _a.root, terminal = _a.terminal;
const objects = [];
t.forEach(({ root, terminal }) => {
if (terminal.name === "object") {
objects.push({ root: root, terminal: terminal });
objects.push({ root, terminal });
}
});
var shapes = objects.map(function (_a) {
var terminal = _a.terminal;
return terminal.shape;
});
var common = findCommonKeys(shapes);
var discriminants = common.filter(function (key) {
var types = new Map();
var literals = new Map();
var optionals = [];
var unknowns = [];
for (var i = 0; i < shapes.length; i++) {
var shape = shapes[i];
var terminals = toTerminals(shape[key]);
for (var j = 0; j < terminals.length; j++) {
var terminal = terminals[j];
const shapes = objects.map(({ terminal }) => terminal.shape);
const common = findCommonKeys(shapes);
const discriminants = common.filter((key) => {
const types = new Map();
const literals = new Map();
let optionals = [];
let unknowns = [];
for (let i = 0; i < shapes.length; i++) {
const shape = shapes[i];
const terminals = toTerminals(shape[key]);
for (let j = 0; j < terminals.length; j++) {
const terminal = terminals[j];
if (terminal.name === "never") {

@@ -709,3 +653,3 @@ // skip

else if (terminal.name === "literal") {
var options = literals.get(terminal.value) || [];
const options = literals.get(terminal.value) || [];
options.push(i);

@@ -715,3 +659,3 @@ literals.set(terminal.value, options);

else {
var options = types.get(terminal.name) || [];
const options = types.get(terminal.name) || [];
options.push(i);

@@ -730,11 +674,11 @@ types.set(terminal.name, options);

}
literals.forEach(function (found, value) {
var options = types.get(toBaseType(value));
literals.forEach((found, value) => {
const options = types.get(toBaseType(value));
if (options) {
options.push.apply(options, found);
options.push(...found);
literals.delete(value);
}
});
var success = true;
literals.forEach(function (found) {
let success = true;
literals.forEach((found) => {
if (dedup(found.concat(unknowns)).length > 1) {

@@ -744,3 +688,3 @@ success = false;

});
types.forEach(function (found) {
types.forEach((found) => {
if (dedup(found.concat(unknowns)).length > 1) {

@@ -752,13 +696,10 @@ success = false;

});
return discriminants.map(function (key) {
var flattened = flatten(objects.map(function (_a) {
var root = _a.root, terminal = _a.terminal;
return ({
root: root,
type: terminal.shape[key],
});
}));
var optional = undefined;
for (var i = 0; i < flattened.length; i++) {
var _a = flattened[i], root = _a.root, terminal = _a.terminal;
return discriminants.map((key) => {
const flattened = flatten(objects.map(({ root, terminal }) => ({
root,
type: terminal.shape[key],
})));
let optional = undefined;
for (let i = 0; i < flattened.length; i++) {
const { root, terminal } = flattened[i];
if (terminal.name === "optional") {

@@ -770,4 +711,4 @@ optional = root;

return {
key: key,
optional: optional,
key,
optional,
matcher: createUnionMatcher(flattened, [key]),

@@ -778,19 +719,17 @@ };

function createUnionMatcher(t, path) {
var order = new Map();
t.forEach(function (_a, i) {
var _b;
var root = _a.root;
order.set(root, (_b = order.get(root)) !== null && _b !== void 0 ? _b : i);
const order = new Map();
t.forEach(({ root }, i) => {
var _a;
order.set(root, (_a = order.get(root)) !== null && _a !== void 0 ? _a : i);
});
var byOrder = function (a, b) {
const byOrder = (a, b) => {
var _a, _b;
return ((_a = order.get(a)) !== null && _a !== void 0 ? _a : 0) - ((_b = order.get(b)) !== null && _b !== void 0 ? _b : 0);
};
var expectedTypes = [];
var literals = new Map();
var types = new Map();
var unknowns = [];
var optionals = [];
t.forEach(function (_a) {
var root = _a.root, terminal = _a.terminal;
const expectedTypes = [];
const literals = new Map();
const types = new Map();
let unknowns = [];
let optionals = [];
t.forEach(({ root, terminal }) => {
if (terminal.name === "never") {

@@ -806,3 +745,3 @@ // skip

else if (terminal.name === "literal") {
var roots = literals.get(terminal.value) || [];
const roots = literals.get(terminal.value) || [];
roots.push(root);

@@ -813,3 +752,3 @@ literals.set(terminal.value, roots);

else {
var roots = types.get(terminal.name) || [];
const roots = types.get(terminal.name) || [];
roots.push(root);

@@ -820,6 +759,6 @@ types.set(terminal.name, roots);

});
literals.forEach(function (roots, value) {
var options = types.get(toBaseType(value));
literals.forEach((roots, value) => {
const options = types.get(toBaseType(value));
if (options) {
options.push.apply(options, roots);
options.push(...roots);
literals.delete(value);

@@ -830,29 +769,25 @@ }

optionals = dedup(optionals).sort(byOrder);
types.forEach(function (roots, type) {
return types.set(type, dedup(roots.concat(unknowns).sort(byOrder)));
});
literals.forEach(function (roots, value) {
return literals.set(value, dedup(roots.concat(unknowns)).sort(byOrder));
});
var expectedLiterals = [];
literals.forEach(function (_, value) {
types.forEach((roots, type) => types.set(type, dedup(roots.concat(unknowns).sort(byOrder))));
literals.forEach((roots, value) => literals.set(value, dedup(roots.concat(unknowns)).sort(byOrder)));
const expectedLiterals = [];
literals.forEach((_, value) => {
expectedLiterals.push(value);
});
var invalidType = {
const invalidType = {
code: "invalid_type",
path: path,
path,
expected: dedup(expectedTypes),
};
var invalidLiteral = {
const invalidLiteral = {
code: "invalid_literal",
path: path,
path,
expected: expectedLiterals,
};
var literalTypes = new Set(expectedLiterals.map(toBaseType));
return function (rootValue, value, mode) {
var count = 0;
var issueTree;
const literalTypes = new Set(expectedLiterals.map(toBaseType));
return (rootValue, value, mode) => {
let count = 0;
let issueTree;
if (value === Nothing) {
for (var i = 0; i < optionals.length; i++) {
var r = optionals[i].func(rootValue, mode);
for (let i = 0; i < optionals.length; i++) {
const r = optionals[i].func(rootValue, mode);
if (r === true || r.code === "ok") {

@@ -874,6 +809,6 @@ return r;

}
var type = toBaseType(value);
var options = literals.get(value) || types.get(type) || unknowns;
for (var i = 0; i < options.length; i++) {
var r = options[i].func(rootValue, mode);
const type = toBaseType(value);
const options = literals.get(value) || types.get(type) || unknowns;
for (let i = 0; i < options.length; i++) {
const r = options[i].func(rootValue, mode);
if (r === true || r.code === "ok") {

@@ -897,31 +832,26 @@ return r;

function flatten(t) {
var result = [];
t.forEach(function (_a) {
var root = _a.root, type = _a.type;
return toTerminals(type).forEach(function (terminal) {
result.push({ root: root, terminal: terminal });
});
});
const result = [];
t.forEach(({ root, type }) => toTerminals(type).forEach((terminal) => {
result.push({ root, terminal });
}));
return result;
}
var UnionType = /** @class */ (function (_super) {
tslib_1.__extends(UnionType, _super);
function UnionType(options) {
var _this = _super.call(this) || this;
_this.options = options;
_this.name = "union";
return _this;
class UnionType extends Type {
constructor(options) {
super();
this.options = options;
this.name = "union";
}
UnionType.prototype.toTerminals = function (into) {
this.options.forEach(function (o) { return o.toTerminals(into); });
};
UnionType.prototype.genFunc = function () {
var flattened = flatten(this.options.map(function (root) { return ({ root: root, type: root }); }));
var hasUnknown = hasTerminal(this, "unknown");
var objects = createObjectMatchers(flattened);
var base = createUnionMatcher(flattened);
return function (v, mode) {
toTerminals(into) {
this.options.forEach((o) => o.toTerminals(into));
}
genFunc() {
const flattened = flatten(this.options.map((root) => ({ root, type: root })));
const hasUnknown = hasTerminal(this, "unknown");
const objects = createObjectMatchers(flattened);
const base = createUnionMatcher(flattened);
return (v, mode) => {
if (!hasUnknown && objects.length > 0 && isObject(v)) {
var item = objects[0];
var value = v[item.key];
const item = objects[0];
const value = v[item.key];
if (value === undefined && !(item.key in v)) {

@@ -937,166 +867,136 @@ if (item.optional) {

};
};
UnionType.prototype.optional = function () {
}
optional() {
return new Optional(this);
};
return UnionType;
}(Type));
var NeverType = /** @class */ (function (_super) {
tslib_1.__extends(NeverType, _super);
function NeverType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "never";
return _this;
}
NeverType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: [] };
return function (v, _mode) { return (v === Nothing ? true : issue); };
};
NeverType.prototype.toTerminals = function (into) {
}
class NeverType extends Type {
constructor() {
super(...arguments);
this.name = "never";
}
genFunc() {
const issue = { code: "invalid_type", expected: [] };
return (v, _mode) => (v === Nothing ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return NeverType;
}(Type));
var UnknownType = /** @class */ (function (_super) {
tslib_1.__extends(UnknownType, _super);
function UnknownType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "unknown";
return _this;
}
UnknownType.prototype.genFunc = function () {
return function (_v, _mode) { return true; };
};
UnknownType.prototype.toTerminals = function (into) {
}
class UnknownType extends Type {
constructor() {
super(...arguments);
this.name = "unknown";
}
genFunc() {
return (_v, _mode) => true;
}
toTerminals(into) {
into.push(this);
};
return UnknownType;
}(Type));
var NumberType = /** @class */ (function (_super) {
tslib_1.__extends(NumberType, _super);
function NumberType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "number";
return _this;
}
NumberType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: ["number"] };
return function (v, _mode) { return (typeof v === "number" ? true : issue); };
};
NumberType.prototype.toTerminals = function (into) {
}
class NumberType extends Type {
constructor() {
super(...arguments);
this.name = "number";
}
genFunc() {
const issue = { code: "invalid_type", expected: ["number"] };
return (v, _mode) => (typeof v === "number" ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return NumberType;
}(Type));
var StringType = /** @class */ (function (_super) {
tslib_1.__extends(StringType, _super);
function StringType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "string";
return _this;
}
StringType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: ["string"] };
return function (v, _mode) { return (typeof v === "string" ? true : issue); };
};
StringType.prototype.toTerminals = function (into) {
}
class StringType extends Type {
constructor() {
super(...arguments);
this.name = "string";
}
genFunc() {
const issue = { code: "invalid_type", expected: ["string"] };
return (v, _mode) => (typeof v === "string" ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return StringType;
}(Type));
var BigIntType = /** @class */ (function (_super) {
tslib_1.__extends(BigIntType, _super);
function BigIntType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "bigint";
return _this;
}
BigIntType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: ["bigint"] };
return function (v, _mode) { return (typeof v === "bigint" ? true : issue); };
};
BigIntType.prototype.toTerminals = function (into) {
}
class BigIntType extends Type {
constructor() {
super(...arguments);
this.name = "bigint";
}
genFunc() {
const issue = { code: "invalid_type", expected: ["bigint"] };
return (v, _mode) => (typeof v === "bigint" ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return BigIntType;
}(Type));
var BooleanType = /** @class */ (function (_super) {
tslib_1.__extends(BooleanType, _super);
function BooleanType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "boolean";
return _this;
}
BooleanType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: ["boolean"] };
return function (v, _mode) { return (typeof v === "boolean" ? true : issue); };
};
BooleanType.prototype.toTerminals = function (into) {
}
class BooleanType extends Type {
constructor() {
super(...arguments);
this.name = "boolean";
}
genFunc() {
const issue = { code: "invalid_type", expected: ["boolean"] };
return (v, _mode) => (typeof v === "boolean" ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return BooleanType;
}(Type));
var UndefinedType = /** @class */ (function (_super) {
tslib_1.__extends(UndefinedType, _super);
function UndefinedType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "undefined";
return _this;
}
UndefinedType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: ["undefined"] };
return function (v, _mode) { return (v === undefined ? true : issue); };
};
UndefinedType.prototype.toTerminals = function (into) {
}
class UndefinedType extends Type {
constructor() {
super(...arguments);
this.name = "undefined";
}
genFunc() {
const issue = { code: "invalid_type", expected: ["undefined"] };
return (v, _mode) => (v === undefined ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return UndefinedType;
}(Type));
var NullType = /** @class */ (function (_super) {
tslib_1.__extends(NullType, _super);
function NullType() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = "null";
return _this;
}
NullType.prototype.genFunc = function () {
var issue = { code: "invalid_type", expected: ["null"] };
return function (v, _mode) { return (v === null ? true : issue); };
};
NullType.prototype.toTerminals = function (into) {
}
class NullType extends Type {
constructor() {
super(...arguments);
this.name = "null";
}
genFunc() {
const issue = { code: "invalid_type", expected: ["null"] };
return (v, _mode) => (v === null ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return NullType;
}(Type));
var LiteralType = /** @class */ (function (_super) {
tslib_1.__extends(LiteralType, _super);
function LiteralType(value) {
var _this = _super.call(this) || this;
_this.value = value;
_this.name = "literal";
return _this;
}
LiteralType.prototype.genFunc = function () {
var value = this.value;
var issue = { code: "invalid_literal", expected: [value] };
return function (v, _) { return (v === value ? true : issue); };
};
LiteralType.prototype.toTerminals = function (into) {
}
class LiteralType extends Type {
constructor(value) {
super();
this.value = value;
this.name = "literal";
}
genFunc() {
const value = this.value;
const issue = { code: "invalid_literal", expected: [value] };
return (v, _) => (v === value ? true : issue);
}
toTerminals(into) {
into.push(this);
};
return LiteralType;
}(Type));
var TransformType = /** @class */ (function (_super) {
tslib_1.__extends(TransformType, _super);
function TransformType(transformed, transform) {
var _this = _super.call(this) || this;
_this.transformed = transformed;
_this.transform = transform;
_this.name = "transform";
return _this;
}
TransformType.prototype.genFunc = function () {
var chain = [];
}
class TransformType extends Type {
constructor(transformed, transform) {
super();
this.transformed = transformed;
this.transform = transform;
this.name = "transform";
}
genFunc() {
const chain = [];
// eslint-disable-next-line @typescript-eslint/no-this-alias
var next = this;
let next = this;
while (next instanceof TransformType) {

@@ -1107,10 +1007,10 @@ chain.push(next.transform);

chain.reverse();
var func = next.func;
var undef = { code: "ok", value: undefined };
return function (v, mode) {
var result = func(v, mode);
const func = next.func;
const undef = { code: "ok", value: undefined };
return (v, mode) => {
let result = func(v, mode);
if (result !== true && result.code !== "ok") {
return result;
}
var current;
let current;
if (result !== true) {

@@ -1126,4 +1026,4 @@ current = result.value;

}
for (var i = 0; i < chain.length; i++) {
var r = chain[i](current, mode);
for (let i = 0; i < chain.length; i++) {
const r = chain[i](current, mode);
if (r !== true) {

@@ -1139,36 +1039,28 @@ if (r.code !== "ok") {

};
};
TransformType.prototype.toTerminals = function (into) {
}
toTerminals(into) {
this.transformed.toTerminals(into);
};
return TransformType;
}(Type));
var LazyType = /** @class */ (function (_super) {
tslib_1.__extends(LazyType, _super);
function LazyType(definer) {
var _this = _super.call(this) || this;
_this.definer = definer;
_this.name = "lazy";
return _this;
}
Object.defineProperty(LazyType.prototype, "type", {
get: function () {
var type = this.definer();
Object.defineProperty(this, "type", {
value: type,
writable: false,
});
return type;
},
enumerable: false,
configurable: true
});
LazyType.prototype.genFunc = function () {
}
class LazyType extends Type {
constructor(definer) {
super();
this.definer = definer;
this.name = "lazy";
}
get type() {
const type = this.definer();
Object.defineProperty(this, "type", {
value: type,
writable: false,
});
return type;
}
genFunc() {
return this.type.genFunc();
};
LazyType.prototype.toTerminals = function (into) {
}
toTerminals(into) {
this.type.toTerminals(into);
};
return LazyType;
}(Type));
}
}
function never() {

@@ -1226,7 +1118,3 @@ return new NeverType();

exports.tuple = tuple;
function union() {
var options = [];
for (var _i = 0; _i < arguments.length; _i++) {
options[_i] = arguments[_i];
}
function union(...options) {
return new UnionType(options);

@@ -1233,0 +1121,0 @@ }

{
"name": "@badrap/valita",
"version": "0.0.19",
"version": "0.0.20",
"description": "A validation & parsing library for TypeScript",

@@ -26,4 +26,4 @@ "main": "./dist/cjs/index.js",

"build:types": "tsc -p ./tsconfig.build.json --emitDeclarationOnly --declaration --declarationMap --declarationDir ./dist/types",
"build:cjs": "tsc -p ./tsconfig.build.json --target es5 --module commonjs --outDir ./dist/cjs",
"build:mjs": "tsc -p ./tsconfig.build.json --target es5 --module es2015 --outDir ./dist/mjs && mv ./dist/mjs/index.js ./dist/mjs/index.mjs",
"build:cjs": "tsc -p ./tsconfig.build.json --target es2015 --module commonjs --outDir ./dist/cjs",
"build:mjs": "tsc -p ./tsconfig.build.json --target es2015 --module es2015 --outDir ./dist/mjs && mv ./dist/mjs/index.js ./dist/mjs/index.mjs",
"build:node-mjs": "tsc -p ./tsconfig.build.json --target es2019 --module es2015 --outDir ./dist/node-mjs && mv ./dist/node-mjs/index.js ./dist/node-mjs/index.mjs",

@@ -33,5 +33,2 @@ "build:node-cjs": "tsc -p ./tsconfig.build.json --target es2019 --module commonjs --outDir ./dist/node-cjs",

},
"dependencies": {
"tslib": "^2.3.0"
},
"devDependencies": {

@@ -38,0 +35,0 @@ "@types/chai": "^4.2.19",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc