ts-interface-checker
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -0,1 +1,4 @@ | ||
## v1.0.1 (2021-10-11) | ||
- Add support for rest types in tuples, and for the type `unknown`. | ||
## v1.0.0 (2021-04-14) | ||
@@ -2,0 +5,0 @@ - Support reporting of multiple nested errors. |
@@ -6,3 +6,3 @@ import { ITypeSuite, TType } from "./types"; | ||
*/ | ||
export { TArray, TEnumType, TEnumLiteral, TFunc, TIface, TLiteral, TName, TOptional, TParam, TParamList, TProp, TTuple, TType, TUnion, TIntersection, array, enumlit, enumtype, func, iface, lit, name, opt, param, tuple, union, intersection, indexKey, BasicType, ITypeSuite, } from "./types"; | ||
export { TArray, TEnumType, TEnumLiteral, TFunc, TIface, TLiteral, TName, TOptional, TParam, TParamList, TProp, TTuple, TType, TUnion, TIntersection, array, enumlit, enumtype, func, iface, lit, name, opt, param, tuple, union, intersection, rest, indexKey, BasicType, ITypeSuite, } from "./types"; | ||
export { VError, IErrorDetail } from './util'; | ||
@@ -9,0 +9,0 @@ export interface ICheckerSuite { |
@@ -44,2 +44,3 @@ "use strict"; | ||
Object.defineProperty(exports, "intersection", { enumerable: true, get: function () { return types_2.intersection; } }); | ||
Object.defineProperty(exports, "rest", { enumerable: true, get: function () { return types_2.rest; } }); | ||
Object.defineProperty(exports, "indexKey", { enumerable: true, get: function () { return types_2.indexKey; } }); | ||
@@ -46,0 +47,0 @@ Object.defineProperty(exports, "BasicType", { enumerable: true, get: function () { return types_2.BasicType; } }); |
@@ -51,2 +51,3 @@ /** | ||
ttype: TType; | ||
name?: string; | ||
constructor(ttype: TType); | ||
@@ -56,2 +57,13 @@ getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
/** | ||
* Defines a rest type, e.g. tuple('string', rest(array('number'))). | ||
*/ | ||
export declare function rest(typeSpec: TypeSpec): RestType; | ||
export declare class RestType extends TType { | ||
typeSpec: TypeSpec; | ||
private _start?; | ||
constructor(typeSpec: TypeSpec); | ||
setStart(start: number): void; | ||
getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
} | ||
/** | ||
* Defines a tuple type, e.g. tuple('string', 'number'). | ||
@@ -62,2 +74,3 @@ */ | ||
ttypes: TType[]; | ||
private _restType?; | ||
constructor(ttypes: TType[]); | ||
@@ -64,0 +77,0 @@ getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; |
@@ -20,3 +20,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.basicTypes = exports.BasicType = exports.TParamList = exports.TParam = exports.param = exports.TFunc = exports.func = exports.TProp = exports.TOptional = exports.opt = exports.TIface = exports.iface = exports.indexKey = exports.TEnumLiteral = exports.enumlit = exports.TEnumType = exports.enumtype = exports.TIntersection = exports.intersection = exports.TUnion = exports.union = exports.TTuple = exports.tuple = exports.TArray = exports.array = exports.TLiteral = exports.lit = exports.TName = exports.name = exports.TType = void 0; | ||
exports.basicTypes = exports.BasicType = exports.TParamList = exports.TParam = exports.param = exports.TFunc = exports.func = exports.TProp = exports.TOptional = exports.opt = exports.TIface = exports.iface = exports.indexKey = exports.TEnumLiteral = exports.enumlit = exports.TEnumType = exports.enumtype = exports.TIntersection = exports.intersection = exports.TUnion = exports.union = exports.TTuple = exports.tuple = exports.RestType = exports.rest = exports.TArray = exports.array = exports.TLiteral = exports.lit = exports.TName = exports.name = exports.TType = void 0; | ||
var util_1 = require("./util"); | ||
@@ -121,2 +121,6 @@ /** Node that represents a type. */ | ||
_this.ttype = ttype; | ||
var elementTypeName = getTypeName(ttype); | ||
if (elementTypeName) { | ||
_this.name = elementTypeName + "[]"; | ||
} | ||
return _this; | ||
@@ -143,2 +147,38 @@ } | ||
/** | ||
* Defines a rest type, e.g. tuple('string', rest(array('number'))). | ||
*/ | ||
function rest(typeSpec) { | ||
return new RestType(typeSpec); | ||
} | ||
exports.rest = rest; | ||
var RestType = /** @class */ (function (_super) { | ||
__extends(RestType, _super); | ||
function RestType(typeSpec) { | ||
var _this = _super.call(this) || this; | ||
_this.typeSpec = typeSpec; | ||
return _this; | ||
} | ||
RestType.prototype.setStart = function (start) { | ||
this._start = start; | ||
}; | ||
RestType.prototype.getChecker = function (suite, strict) { | ||
var arrType = typeof this.typeSpec === "string" ? getNamedType(suite, this.typeSpec) : this.typeSpec; | ||
if (!(arrType instanceof TArray)) { | ||
throw new Error("Rest type must be an array"); | ||
} | ||
var itemChecker = arrType.ttype.getChecker(suite, strict); | ||
var start = this._start; | ||
return function (value, ctx) { | ||
for (var i = start; i < value.length; i++) { | ||
if (!itemChecker(value[i], ctx)) { | ||
return ctx.fail(i, null, 1); | ||
} | ||
} | ||
return true; | ||
}; | ||
}; | ||
return RestType; | ||
}(TType)); | ||
exports.RestType = RestType; | ||
/** | ||
* Defines a tuple type, e.g. tuple('string', 'number'). | ||
@@ -159,2 +199,8 @@ */ | ||
_this.ttypes = ttypes; | ||
var last = ttypes[ttypes.length - 1]; | ||
if (last instanceof RestType) { | ||
ttypes.pop(); | ||
_this._restType = last; | ||
_this._restType.setStart(ttypes.length); | ||
} | ||
return _this; | ||
@@ -176,2 +222,8 @@ } | ||
}; | ||
if (this._restType) { | ||
var restChecker_1 = this._restType.getChecker(suite, strict); | ||
return function (value, ctx) { | ||
return checker(value, ctx) && restChecker_1(value, ctx); | ||
}; | ||
} | ||
if (!strict) { | ||
@@ -207,3 +259,3 @@ return checker; | ||
_this.ttypes = ttypes; | ||
var names = ttypes.map(function (t) { return t instanceof TName || t instanceof TLiteral ? t.name : null; }) | ||
var names = ttypes.map(getTypeName) | ||
.filter(function (n) { return n; }); | ||
@@ -588,2 +640,3 @@ var otherTypes = ttypes.length - names.length; | ||
any: new BasicType(function (v) { return true; }, "is invalid"), | ||
unknown: new BasicType(function (v) { return true; }, "is invalid"), | ||
number: new BasicType(function (v) { return (typeof v === "number"); }, "is not a number"), | ||
@@ -620,1 +673,6 @@ object: new BasicType(function (v) { return (typeof v === "object" && v); }, "is not an object"), | ||
} | ||
function getTypeName(t) { | ||
if (t instanceof TName || t instanceof TLiteral || t instanceof TArray) { | ||
return t.name; | ||
} | ||
} |
{ | ||
"name": "ts-interface-checker", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Runtime library to validate data against TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "main": "dist/index", |
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
83294
1636