ts-interface-checker
Advanced tools
Comparing version 0.1.10 to 0.1.11
@@ -6,3 +6,3 @@ import { ITypeSuite, TType } from "./types"; | ||
*/ | ||
export { TArray, TEnumType, TEnumLiteral, TFunc, TIface, TLiteral, TName, TOptional, TParam, TParamList, TProp, TTuple, TType, TUnion, array, enumlit, enumtype, func, iface, lit, name, opt, param, tuple, union, 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, BasicType, ITypeSuite, } from "./types"; | ||
export interface ICheckerSuite { | ||
@@ -100,5 +100,5 @@ [name: string]: Checker; | ||
*/ | ||
private _doCheck(checkerFunc, value); | ||
private _doValidate(checkerFunc, value); | ||
private _getMethod(methodName); | ||
private _doCheck; | ||
private _doValidate; | ||
private _getMethod; | ||
} |
@@ -23,2 +23,3 @@ "use strict"; | ||
exports.TUnion = types_2.TUnion; | ||
exports.TIntersection = types_2.TIntersection; | ||
exports.array = types_2.array; | ||
@@ -35,2 +36,3 @@ exports.enumlit = types_2.enumlit; | ||
exports.union = types_2.union; | ||
exports.intersection = types_2.intersection; | ||
exports.BasicType = types_2.BasicType; | ||
@@ -37,0 +39,0 @@ /** |
@@ -8,3 +8,3 @@ /** | ||
export declare abstract class TType { | ||
abstract getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
abstract getChecker(suite: ITypeSuite, strict: boolean, allowedProps?: Set<string>): CheckerFunc; | ||
} | ||
@@ -31,3 +31,3 @@ /** | ||
constructor(name: string); | ||
getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
getChecker(suite: ITypeSuite, strict: boolean, allowedProps?: Set<string>): CheckerFunc; | ||
} | ||
@@ -74,2 +74,11 @@ /** | ||
/** | ||
* Defines an intersection type, e.g. intersection('number', 'null'). | ||
*/ | ||
export declare function intersection(...typeSpec: TypeSpec[]): TIntersection; | ||
export declare class TIntersection extends TType { | ||
ttypes: TType[]; | ||
constructor(ttypes: TType[]); | ||
getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
} | ||
/** | ||
* Defines an enum type, e.g. enum({'A': 1, 'B': 2}). | ||
@@ -114,3 +123,3 @@ */ | ||
constructor(bases: string[], props: TProp[]); | ||
getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
getChecker(suite: ITypeSuite, strict: boolean, allowedProps?: Set<string>): CheckerFunc; | ||
} | ||
@@ -117,0 +126,0 @@ /** |
@@ -50,6 +50,6 @@ "use strict"; | ||
} | ||
TName.prototype.getChecker = function (suite, strict) { | ||
TName.prototype.getChecker = function (suite, strict, allowedProps) { | ||
var _this = this; | ||
var ttype = getNamedType(suite, this.name); | ||
var checker = ttype.getChecker(suite, strict); | ||
var checker = ttype.getChecker(suite, strict, allowedProps); | ||
if (ttype instanceof BasicType || ttype instanceof TName) { | ||
@@ -210,2 +210,34 @@ return checker; | ||
/** | ||
* Defines an intersection type, e.g. intersection('number', 'null'). | ||
*/ | ||
function intersection() { | ||
var typeSpec = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
typeSpec[_i] = arguments[_i]; | ||
} | ||
return new TIntersection(typeSpec.map(function (t) { return parseSpec(t); })); | ||
} | ||
exports.intersection = intersection; | ||
var TIntersection = /** @class */ (function (_super) { | ||
__extends(TIntersection, _super); | ||
function TIntersection(ttypes) { | ||
var _this = _super.call(this) || this; | ||
_this.ttypes = ttypes; | ||
return _this; | ||
} | ||
TIntersection.prototype.getChecker = function (suite, strict) { | ||
var allowedProps = new Set(); | ||
var itemCheckers = this.ttypes.map(function (t) { return t.getChecker(suite, strict, allowedProps); }); | ||
return function (value, ctx) { | ||
var ok = itemCheckers.every(function (checker) { return checker(value, ctx); }); | ||
if (ok) { | ||
return true; | ||
} | ||
return ctx.fail(null, null, 0); | ||
}; | ||
}; | ||
return TIntersection; | ||
}(TType)); | ||
exports.TIntersection = TIntersection; | ||
/** | ||
* Defines an enum type, e.g. enum({'A': 1, 'B': 2}). | ||
@@ -292,3 +324,3 @@ */ | ||
} | ||
TIface.prototype.getChecker = function (suite, strict) { | ||
TIface.prototype.getChecker = function (suite, strict, allowedProps) { | ||
var _this = this; | ||
@@ -331,2 +363,7 @@ var baseCheckers = this.bases.map(function (b) { return getNamedType(suite, b).getChecker(suite, strict); }); | ||
} | ||
var propSet = this.propSet; | ||
if (allowedProps) { | ||
this.propSet.forEach(function (prop) { return allowedProps.add(prop); }); | ||
propSet = allowedProps; | ||
} | ||
// In strict mode, check also for unknown enumerable properties. | ||
@@ -338,3 +375,3 @@ return function (value, ctx) { | ||
for (var prop in value) { | ||
if (!_this.propSet.has(prop)) { | ||
if (!propSet.has(prop)) { | ||
return ctx.fail(prop, "is extraneous", 2); | ||
@@ -341,0 +378,0 @@ } |
@@ -66,6 +66,7 @@ "use strict"; | ||
DetailContext.prototype.resolveUnion = function (unionResolver) { | ||
var _a, _b; | ||
var u = unionResolver; | ||
var best = null; | ||
for (var _i = 0, _a = u.contexts; _i < _a.length; _i++) { | ||
var ctx = _a[_i]; | ||
for (var _i = 0, _c = u.contexts; _i < _c.length; _i++) { | ||
var ctx = _c[_i]; | ||
if (!best || ctx._score >= best._score) { | ||
@@ -76,6 +77,5 @@ best = ctx; | ||
if (best && best._score > 0) { | ||
(_b = this._propNames).push.apply(_b, best._propNames); | ||
(_c = this._messages).push.apply(_c, best._messages); | ||
(_a = this._propNames).push.apply(_a, best._propNames); | ||
(_b = this._messages).push.apply(_b, best._messages); | ||
} | ||
var _b, _c; | ||
}; | ||
@@ -82,0 +82,0 @@ DetailContext.prototype.getError = function (path) { |
{ | ||
"name": "ts-interface-checker", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"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
62065
1238