ts-interface-checker
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -72,2 +72,30 @@ /** | ||
/** | ||
* Defines an enum type, e.g. enum({'A': 1, 'B': 2}). | ||
*/ | ||
export declare function enumtype(values: { | ||
[name: string]: string | number; | ||
}): TEnumType; | ||
export declare class TEnumType extends TType { | ||
members: { | ||
[name: string]: string | number; | ||
}; | ||
private _failMsg; | ||
readonly validValues: Set<string | number>; | ||
constructor(members: { | ||
[name: string]: string | number; | ||
}); | ||
getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
} | ||
/** | ||
* Defines a literal enum value, such as Direction.Up, specified as enumlit("Direction", "Up"). | ||
*/ | ||
export declare function enumlit(name: string, prop: string): TEnumLiteral; | ||
export declare class TEnumLiteral extends TType { | ||
enumName: string; | ||
prop: string; | ||
private _failMsg; | ||
constructor(enumName: string, prop: string); | ||
getChecker(suite: ITypeSuite, strict: boolean): CheckerFunc; | ||
} | ||
/** | ||
* Defines an interface. The first argument is an array of interfaces that it extends, and the | ||
@@ -74,0 +102,0 @@ * second is an array of properties. |
@@ -169,2 +169,49 @@ "use strict"; | ||
exports.TUnion = TUnion; | ||
/** | ||
* Defines an enum type, e.g. enum({'A': 1, 'B': 2}). | ||
*/ | ||
function enumtype(values) { | ||
return new TEnumType(values); | ||
} | ||
exports.enumtype = enumtype; | ||
class TEnumType extends TType { | ||
constructor(members) { | ||
super(); | ||
this.members = members; | ||
this._failMsg = "is not a valid enum value"; | ||
this.validValues = new Set(); | ||
this.validValues = new Set(Object.keys(members).map((name) => members[name])); | ||
} | ||
getChecker(suite, strict) { | ||
return (value, ctx) => (this.validValues.has(value) ? true : ctx.fail(null, this._failMsg, 0)); | ||
} | ||
} | ||
exports.TEnumType = TEnumType; | ||
/** | ||
* Defines a literal enum value, such as Direction.Up, specified as enumlit("Direction", "Up"). | ||
*/ | ||
function enumlit(name, prop) { | ||
return new TEnumLiteral(name, prop); | ||
} | ||
exports.enumlit = enumlit; | ||
class TEnumLiteral extends TType { | ||
constructor(enumName, prop) { | ||
super(); | ||
this.enumName = enumName; | ||
this.prop = prop; | ||
this._failMsg = `is not ${enumName}.${prop}`; | ||
} | ||
getChecker(suite, strict) { | ||
const ttype = getNamedType(suite, this.enumName); | ||
if (!(ttype instanceof TEnumType)) { | ||
throw new Error(`Type ${this.enumName} used in enumlit is not an enum type`); | ||
} | ||
const val = ttype.members[this.prop]; | ||
if (!ttype.members.hasOwnProperty(this.prop)) { | ||
throw new Error(`Unknown value ${this.enumName}.${this.prop} used in enumlit`); | ||
} | ||
return (value, ctx) => (value === val) ? true : ctx.fail(null, this._failMsg, -1); | ||
} | ||
} | ||
exports.TEnumLiteral = TEnumLiteral; | ||
function makeIfaceProps(props) { | ||
@@ -171,0 +218,0 @@ return Object.keys(props).map((name) => makeIfaceProp(name, props[name])); |
{ | ||
"name": "ts-interface-checker", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Runtime library to validate data against TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "main": "dist/index", |
52192
1047