ts-interface-checker
Advanced tools
Comparing version 0.1.3 to 0.1.4
import { ITypeSuite, TType } from "./types"; | ||
import { IErrorDetail } from "./util"; | ||
/** | ||
@@ -38,2 +39,7 @@ * Export functions used to define interfaces. | ||
/** | ||
* Returns an error object describing the errors if the given value does not satisfy this | ||
* Checker's type, or null if it does. | ||
*/ | ||
validate(value: any): IErrorDetail | null; | ||
/** | ||
* Check that the given value satisfies this checker's type strictly. This checks that objects | ||
@@ -50,2 +56,7 @@ * and tuples have no extra members. Note that this prevents backward compatibility, so usually | ||
/** | ||
* Returns an error object describing the errors if the given value does not satisfy this | ||
* Checker's type strictly, or null if it does. | ||
*/ | ||
strictValidate(value: any): IErrorDetail | null; | ||
/** | ||
* If this checker is for an interface, returns a Checker for the type required for the given | ||
@@ -85,3 +96,4 @@ * property of this interface. | ||
private _doCheck(checkerFunc, value); | ||
private _doValidate(checkerFunc, value); | ||
private _getMethod(methodName); | ||
} |
@@ -79,2 +79,9 @@ "use strict"; | ||
/** | ||
* Returns an error object describing the errors if the given value does not satisfy this | ||
* Checker's type, or null if it does. | ||
*/ | ||
validate(value) { | ||
return this._doValidate(this.checkerPlain, value); | ||
} | ||
/** | ||
* Check that the given value satisfies this checker's type strictly. This checks that objects | ||
@@ -93,2 +100,9 @@ * and tuples have no extra members. Note that this prevents backward compatibility, so usually | ||
/** | ||
* Returns an error object describing the errors if the given value does not satisfy this | ||
* Checker's type strictly, or null if it does. | ||
*/ | ||
strictValidate(value) { | ||
return this._doValidate(this.checkerStrict, value); | ||
} | ||
/** | ||
* If this checker is for an interface, returns a Checker for the type required for the given | ||
@@ -159,2 +173,11 @@ * property of this interface. | ||
} | ||
_doValidate(checkerFunc, value) { | ||
const noopCtx = new util_1.NoopContext(); | ||
if (checkerFunc(value, noopCtx)) { | ||
return null; | ||
} | ||
const detailCtx = new util_1.DetailContext(); | ||
checkerFunc(value, detailCtx); | ||
return detailCtx.getErrorDetail(); | ||
} | ||
_getMethod(methodName) { | ||
@@ -161,0 +184,0 @@ const ttype = this.props.get(methodName); |
@@ -196,2 +196,3 @@ "use strict"; | ||
const testCtx = new util_1.NoopContext(); | ||
// Consider a prop required if it's not optional AND does not allow for undefined as a value. | ||
const isPropRequired = this.props.map((prop, i) => !prop.isOpt && !propCheckers[i](undefined, testCtx)); | ||
@@ -198,0 +199,0 @@ const checker = (value, ctx) => { |
@@ -25,2 +25,10 @@ /** | ||
/** | ||
* IErrorDetail describes errors as returned by the validate() and validateStrict() methods. | ||
*/ | ||
export interface IErrorDetail { | ||
path: string; | ||
message: string; | ||
nested?: IErrorDetail[]; | ||
} | ||
/** | ||
* Fast implementation of IContext used for first-pass validation. If that fails, we can validate | ||
@@ -47,2 +55,3 @@ * using DetailContext to collect error messages. That's faster for the common case when messages | ||
getError(): VError; | ||
getErrorDetail(): IErrorDetail | null; | ||
} |
@@ -75,2 +75,22 @@ "use strict"; | ||
} | ||
getErrorDetail() { | ||
let path = "value"; | ||
const details = []; | ||
for (let i = this._propNames.length - 1; i >= 0; i--) { | ||
const p = this._propNames[i]; | ||
path += (typeof p === "number") ? `[${p}]` : (p ? `.${p}` : ""); | ||
const message = this._messages[i]; | ||
if (message) { | ||
details.push({ path, message }); | ||
} | ||
} | ||
let detail = null; | ||
for (let i = details.length - 1; i >= 0; i--) { | ||
if (detail) { | ||
details[i].nested = [detail]; | ||
} | ||
detail = details[i]; | ||
} | ||
return detail; | ||
} | ||
} | ||
@@ -77,0 +97,0 @@ exports.DetailContext = DetailContext; |
{ | ||
"name": "ts-interface-checker", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Runtime library to validate data against TypeScript interfaces", | ||
@@ -13,3 +13,3 @@ "main": "dist/index", | ||
"preversion": "npm test", | ||
"version": "npm run build && git add dist/" | ||
"version": "npm run build" | ||
}, | ||
@@ -16,0 +16,0 @@ "keywords": [ |
49697
972