typescript-checker
Version:
Powerful data validation library enabling type safety
256 lines (255 loc) • 10.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkPair = exports.HasKeys = exports.Transform = exports.TypeParseDate = exports.ConvertDate = exports.parsesAs = exports.Items = exports.Keys2 = exports.Keys1 = exports.Keys = exports.checkInstanceOf = exports.TypeCheck = exports.RecordPartial = exports.Record = exports.ConvertParseBoolean = exports.ConvertParseFloat = exports.ConvertParseInt = exports.ConvertJSON = exports.TypeMatchesNot = exports.TypeMatches = exports.TypeParseBoolean = exports.TypeParseFloat = exports.TypeParseInt = exports.TypeEnumString = exports.TypeEnum = exports.TypeArray = exports.TypeObject = exports.TypeFunction = exports.TypeUnknown = exports.TypeNumber = exports.TypeBoolean = exports.TypeString = exports.TypeNull = exports.TypeUndefined = void 0;
var utils_1 = require("./utils");
var core_1 = require("./core");
exports.TypeUndefined = (0, core_1.Type)("undefined");
exports.TypeNull = (0, core_1.OneOf)(null);
exports.TypeString = (0, core_1.Type)("string");
exports.TypeBoolean = (0, core_1.Type)("boolean");
exports.TypeNumber = (0, core_1.Type)("number");
var TypeUnknown = function (value) { return [null, value]; };
exports.TypeUnknown = TypeUnknown;
exports.TypeFunction = (0, core_1.Type)("function");
exports.TypeObject = (0, core_1.AndNot)((0, core_1.Type)("object"), (0, core_1.OneOf)(null), " is null expected object");
var TypeArray = function (value) {
return Array.isArray(value) ? [null, value] : [["expected an array, found: " + value]];
};
exports.TypeArray = TypeArray;
var TypeEnum = function (_enum) {
return core_1.OneOf.apply(void 0, Object.values(_enum).filter(function (x) { return typeof x === "number"; }));
};
exports.TypeEnum = TypeEnum;
var TypeEnumString = function (_enum) {
return core_1.OneOf.apply(void 0, Object.values(_enum).filter(function (x) { return typeof x === "string"; }));
};
exports.TypeEnumString = TypeEnumString;
var TypeParseInt = function (value) {
var string = (0, exports.TypeString)(value);
if ((0, core_1.isCheckError)(string)) {
return string;
}
var number = parseInt(string[1], 10);
if (isNaN(number)) {
return [["expected a string containing an integer, found " + (0, utils_1.stringifyObject)(value)]];
}
return string;
};
exports.TypeParseInt = TypeParseInt;
var TypeParseFloat = function (value) {
var string = (0, exports.TypeString)(value);
if ((0, core_1.isCheckError)(string)) {
return string;
}
var number = parseFloat(string[1]);
if (isNaN(number)) {
return [["expected a string containing a float, found " + (0, utils_1.stringifyObject)(value)]];
}
return string;
};
exports.TypeParseFloat = TypeParseFloat;
var TypeParseBoolean = function (value) {
var string = (0, exports.TypeString)(value);
if ((0, core_1.isCheckError)(string)) {
return string;
}
if (string[1] !== "true" && string[1] !== "false") {
return [["expected a string containing a boolean, found " + (0, utils_1.stringifyObject)(value)]];
}
return string;
};
exports.TypeParseBoolean = TypeParseBoolean;
var TypeMatches = function (name, regexp) {
return function (value) {
if (value.match(regexp) === null) {
return [["value does not match " + name + ", found '" + value + "'"]];
}
return [null, value];
};
};
exports.TypeMatches = TypeMatches;
var TypeMatchesNot = function (name, regexp) {
return function (value) {
if (value.match(regexp) !== null) {
return [["value does match " + name + " but should not, found '" + value + "'"]];
}
return [null, value];
};
};
exports.TypeMatchesNot = TypeMatchesNot;
var ConvertJSON = function (value) {
try {
return [null, JSON.parse(value)];
}
catch (e) {
return [["failed parsing string as json, " + e.message]];
}
};
exports.ConvertJSON = ConvertJSON;
var ConvertParseInt = function (value) {
var string = (0, exports.TypeString)(value);
if ((0, core_1.isCheckError)(string)) {
return string;
}
var number = parseInt(string[1], 10);
if (isNaN(number)) {
return [["expected a string containing a number, found " + (0, utils_1.stringifyObject)(value)]];
}
return [null, number];
};
exports.ConvertParseInt = ConvertParseInt;
var ConvertParseFloat = function (value) {
var string = (0, exports.TypeString)(value);
if ((0, core_1.isCheckError)(string)) {
return string;
}
var number = parseFloat(string[1]);
if (isNaN(number)) {
return [["expected a string containing a number, found " + (0, utils_1.stringifyObject)(value)]];
}
return [null, number];
};
exports.ConvertParseFloat = ConvertParseFloat;
var ConvertParseBoolean = function (value) {
var string = (0, exports.TypeString)(value);
if ((0, core_1.isCheckError)(string)) {
return string;
}
if (string[1] !== "true" && string[1] !== "false") {
return [["expected a string representing a boolean, found " + (0, utils_1.stringifyObject)(value)]];
}
return [null, string[1] === "true"];
};
exports.ConvertParseBoolean = ConvertParseBoolean;
var Record = function (checkKey, checkItem) {
return (0, core_1.And)(exports.TypeObject, function (value) {
var obj = {};
var _loop_1 = function (key, item) {
var keyResult = checkKey(key);
if ((0, core_1.isCheckError)(keyResult)) {
return { value: [keyResult[0].map(function (error) { return "invalid key, " + error; })] };
}
var result = checkItem(item);
if ((0, core_1.isCheckError)(result)) {
return { value: [result[0].map(function (error) { return "." + key + " " + error; })] };
}
obj[keyResult[1]] = result[1];
};
for (var _i = 0, _a = Object.entries(value); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], item = _b[1];
var state_1 = _loop_1(key, item);
if (typeof state_1 === "object")
return state_1.value;
}
return [null, obj];
});
};
exports.Record = Record;
var RecordPartial = function (checkKey, checkItem) {
return (0, core_1.And)(exports.TypeObject, function (value) {
var obj = {};
var _loop_2 = function (key, item) {
var keyResult = checkKey(key);
if ((0, core_1.isCheckError)(keyResult)) {
return { value: [keyResult[0].map(function (error) { return "invalid key, " + error; })] };
}
var result = checkItem(item);
if ((0, core_1.isCheckError)(result)) {
return { value: [result[0].map(function (error) { return "." + key + " " + error; })] };
}
obj[keyResult[1]] = result[1];
};
for (var _i = 0, _a = Object.entries(value); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], item = _b[1];
var state_2 = _loop_2(key, item);
if (typeof state_2 === "object")
return state_2.value;
}
return [null, obj];
});
};
exports.RecordPartial = RecordPartial;
var TypeCheck = function (check, type) {
if (type === void 0) { type = "custom type"; }
return function (value) {
return check(value) ? [null, value] : [["expected " + type + ", found: " + value]];
};
};
exports.TypeCheck = TypeCheck;
var checkInstanceOf = function (constructor) {
return function (value) {
return value instanceof constructor ? [null, value] : [["expected " + constructor.name]];
};
};
exports.checkInstanceOf = checkInstanceOf;
exports.Keys = (function (schema, optional) {
if (optional === undefined) {
return (0, exports.Keys1)(schema);
}
return (0, exports.Keys2)(schema, optional);
});
var Keys1 = function (schema) { return (0, core_1.And)(exports.TypeObject, (0, core_1.KeysObject1)(schema)); };
exports.Keys1 = Keys1;
var Keys2 = function (schema, optional) {
return (0, core_1.And)(exports.TypeObject, (0, core_1.KeysObject2)(schema, optional));
};
exports.Keys2 = Keys2;
var Items = function (schema) { return (0, core_1.And)(exports.TypeArray, (0, core_1.ItemsPartial)(schema)); };
exports.Items = Items;
var parsesAs = function (check) {
return function (value) {
var result = check(value);
if ((0, core_1.isCheckError)(result)) {
return result;
}
return [null, value];
};
};
exports.parsesAs = parsesAs;
exports.ConvertDate = (0, core_1.And)(exports.TypeString, function (value) {
var date = new Date(value);
if (isNaN(date.valueOf())) {
return [["invalid date: " + value]];
}
return [null, date];
});
exports.TypeParseDate = (0, core_1.And)(exports.TypeString, (0, exports.parsesAs)(exports.ConvertDate));
var Transform = function (transformFn) {
return function (value) {
return [null, transformFn(value)];
};
};
exports.Transform = Transform;
var HasKeys = function (schema) {
var check = (0, core_1.And)(exports.TypeObject, (0, core_1.KeysObject1)(schema));
return function (value) {
var result = check(value);
if ((0, core_1.isCheckError)(result)) {
return result;
}
return [null, value];
};
};
exports.HasKeys = HasKeys;
var checkPair = function (firstChecker, secondChecker) {
return function (value) {
var checkArray = (0, exports.TypeArray)(value);
if ((0, core_1.isCheckError)(checkArray)) {
return checkArray;
}
var tuple = checkArray[1];
if (tuple.length !== 2) {
return [["array length !== 2"]];
}
var firstCheck = firstChecker(tuple[0]);
if ((0, core_1.isCheckError)(firstCheck)) {
return firstCheck;
}
var secondCheck = secondChecker(tuple[1]);
if ((0, core_1.isCheckError)(secondCheck)) {
return secondCheck;
}
return [null, [firstCheck[1], secondCheck[1]]];
};
};
exports.checkPair = checkPair;