js.spec: add missing spec functions (#22240)

* add missing spec.predicate function

* Added another missing method problemStr()
This commit is contained in:
Matt Bishop
2018-01-02 10:48:17 -10:00
committed by Mohamed Hegazy
parent a976fdc45b
commit 2fda7e47c4
2 changed files with 22 additions and 1 deletions

View File

@@ -103,6 +103,12 @@ export function explain(spec: spec.SpecInput, value: any): void;
*/
export function explainStr(spec: spec.SpecInput, value: any): string;
/**
* Returns a string with the problem statement from the given Problem.
* @param problem the problem
*/
export function problemStr(problem: Problem): string;
/**
* Tests if a value conforms to a spec, and if not, throws an Error.
* @param spec the spec to test with
@@ -110,6 +116,9 @@ export function explainStr(spec: spec.SpecInput, value: any): string;
*/
export function assert(spec: spec.SpecInput, value: any): void;
/**
* Symbols used
*/
export namespace symbol {
/**
* Returned by conform() to indicate a value does not conform to a spec.
@@ -210,6 +219,14 @@ export namespace spec {
*/
function oneOf(name: string, ...values: any[]): Spec;
/**
* Used to define a predicate function as a Spec.
* @param name the name of the spec
* @param predicate the predicate function
* @returns the constructed spec
*/
function predicate(name: string, predicate: PredFn): Spec;
// Predicates
/**
* Returns true if data is an integer.

View File

@@ -17,7 +17,9 @@ S.explainData((value) => true, "a value");
const {path, via, value, predicate}: {path: string[], via: string[], value: any, predicate: S.Predicate} = problems[0];
const problemStr: string = S.explainStr(S.spec.even, 3);
const problemStr: string = S.problemStr(problems[0]);
const explainStr: string = S.explainStr(S.spec.even, 3);
S.explainStr((value) => true, "a value");
// $ExpectType void
@@ -46,6 +48,8 @@ const mapSpec: S.Spec = S.spec.map("map test", { email: S.spec.string, [S.symbol
const oneOfSpec: S.Spec = S.spec.oneOf("oneOf test", "a", "b", "c");
const predicateSpec: S.Spec = S.spec.predicate("predicate test", (value) => true);
// Predicates
const intPred: S.Predicate = S.spec.int;