0.2.3 • Published 6 years ago

rest-in-contract-dsl v0.2.3

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
6 years ago

rest-in-contract-dsl

Contract script DSL for rest-in-contract

npm version node Codecov branch Build

GitHub issues GitHub forks GitHub stars GitHub License dependencies Status devDependencies Status

Base Types

Middlewares

The base abstract type of all dsl functions.

Feature

The base abstract type of all features implemented by a dsl function.

Evaluator

A dsl function feature which allow evaluating a value from the context.

Comparable

A dsl function feature which allow comparing or validating with a target value.

Mockable

A dsl function feature which allow mocking value.

DSL functions

float

Representing a float number value.

Implementing features

Comparable, Mockable

Options

Option keyDescriptionRequiredExample
ltIt defines the "less than" criterion. This criterion is used for comparing values and mocking values.optional0.1
lteIt defines the "less than equal" criterion. This criterion is used for comparing values and mocking values.optional0.1
gtIt defines the "greater than" criterion. This criterion is used for comparing values and mocking values.optional0.9
gteIt defines the "greater than equal" criterion. This criterion is used for comparing values and mocking values.optional0.9

Sample

var fnInstance = float({ gt: 0.5, lt: 0.6 });

fnInstance.compareFunc("abc");//false;
fnInstance.compareFunc(0.4);//false;
fnInstance.compareFunc(0.5);//false
fnInstance.compareFunc(0.55);//true
fnInstance.compareFunc(0.6);//false
fnInstance.compareFunc(0.7);//false

fnInstance.mock();//0.5800135823905961

integer

Representing an integer number value.

Implementing features

Comparable, Mockable

Options

Option keyDescriptionRequiredExample
ltIt defines the "less than" criterion. This criterion is used for comparing values and mocking values.optional1
lteIt defines the "less than equal" criterion. This criterion is used for comparing values and mocking values.optional1
gtIt defines the "greater than" criterion. This criterion is used for comparing values and mocking values.optional10
gteIt defines the "greater than equal" criterion. This criterion is used for comparing values and mocking values.optional10

Sample

var fnInstance = integer({ gt: 1, lt: 5 });

fnInstance.compareFunc("abc");//false;
fnInstance.compareFunc(0);//false;
fnInstance.compareFunc(1);//false
fnInstance.compareFunc(3);//true
fnInstance.compareFunc(5);//false
fnInstance.compareFunc(7);//false

fnInstance.mock();//2
fnInstance.mock();//3
fnInstance.mock();//3
fnInstance.mock();//4

var fnInstance = integer({ gte: 1, lte: 5 });

fnInstance.compareFunc("abc");//false;
fnInstance.compareFunc(0);//false;
fnInstance.compareFunc(1);//true
fnInstance.compareFunc(3);//true
fnInstance.compareFunc(5);//true
fnInstance.compareFunc(7);//false

fnInstance.mock();//5
fnInstance.mock();//2
fnInstance.mock();//4
fnInstance.mock();//4
fnInstance.mock();//1

date

Representing a date value.

Implementing features

Comparable, Mockable

Options

Option keyDescriptionRequiredExample
formatIt defines the date format which is used for comparing values and mocking valuesoptional'YYYY-MM-DD HH:mm:ss'
typeIt may either be "past", "future" or "betwen". Like the worings said, it defines how it would behave for comparing values and mocking values. "past" would only allow date in the past. "future" would only allow date in the future. "between" allow date value between a range.optional"past", "future" or "betwen"
fromIt defines the "from" criterion, which is the minimum aceptable value. This criterion is used for comparing values and mocking values.optional10
toIt defines the "to" criterion, which is the maximum aceptable value. This criterion is used for comparing values and mocking values.optional10

Sample

var fnInstance = date({
    format: 'YYYY-MM-DD HH:mm:ss',
    type: 'between',
    from: '1999-01-02 01:01:01',
    to: '2001-12-31 01:01:01'
});

fnInstance.compareFunc('1999-01-01');//false;
fnInstance.compareFunc('1999-01-02');//false;
fnInstance.compareFunc('1999-01-03');//true;
fnInstance.compareFunc('2000-01-01');//true;
fnInstance.compareFunc('2001-12-31');//true;
fnInstance.compareFunc('2002-01-01');//false;

mockValue = fnInstance.mock();//2000-06-30 21:25:25
mockValue = fnInstance.mock();//2001-07-19 14:21:38
mockValue = fnInstance.mock();//2001-08-10 18:43:24
mockValue = fnInstance.mock();//1999-10-06 12:05:08
mockValue = fnInstance.mock();//2000-10-31 03:32:38

regex

Representing a string pattern value.

Implementing features

Comparable, Mockable

Function arguments

regex(pattern)

ArgumentDescriptionRequiredExample
patternThe string representation of Regex patternmandatory"0-9{4},a-z{6},A-Z{3}"

Sample

var fnInstance = regex("[0-9]{4},[a-z]{6},[A-Z]{3}");
fnInstance.compareFunc('0123,abcxyz,ABC'));//true
fnInstance.compareFunc('apple'));//false

fnInstance.mock();//9345,wpaump,JEU
fnInstance.mock();//4576,fcwlfz,NAP
fnInstance.mock();//3047,drfitz,OQC
fnInstance.mock();//5510,vpymny,SEC
fnInstance.mock();//1154,mgppdb,KYW

text

Representing a text value.

Implementing features

Comparable, Mockable

Options

Option keyDescriptionRequiredExample
typeIt defines the type of text criterion. It may be either "word", "words", "sentence" or "paragraphs".optionaleither "word", "words", "sentence" or "paragraphs"

Sample

var fnInstance = text({ type: "word" });
fnInstance.compareFunc("apple");//true;
fnInstance.mock();//"facilis"

fnInstance = text({ type: "words" });
fnInstance.compareFunc("apple tree");//true;
fnInstance.mock();//"voluptatum porro soluta"

fnInstance = text({ type: "sentence" });
fnInstance.compareFunc("Apple tree is great.");//true;
fnInstance.mock();//"Dolorem ut eum hic."

fnInstance = text({ type: "paragraphs" });
fnInstance.compareFunc("Apple tree is great. Lemon tree is great too.");//true;
fnInstance.compareFunc(123);//false;
fnInstance.mock();//"Omnis ipsam quis. Sed quo molestias. ......"

name

Representing a name value.

Implementing features

Comparable, Mockable

Sample

var fnInstance = name();
fnInstance.compareFunc("Mary Chan");//true;
fnInstance.mock();//"Jacinto Jones"

email

Representing an email value.

Implementing features

Comparable, Mockable

Sample

var fnInstance = name();
fnInstance.compareFunc("chan.mary123@test.com");//true;
fnInstance.mock();//"Hettie56@yahoo.com"

phone

Representing a phone value.

Implementing features

Comparable, Mockable

Sample

var fnInstance = phone();
fnInstance.compareFunc('23456789'));//true
fnInstance.compareFunc('(852)23456789'));//true
fnInstance.compareFunc('(852) 23456789'));//true
fnInstance.compareFunc('852-23456789'));//true
fnInstance.compareFunc('852-23456789x1234'));//true
fnInstance.compareFunc('852-23456789X1234'));//true
fnInstance.compareFunc('a23456789'));//false
fnInstance.compareFunc('123,456,789'));//false
fnInstance.compareFunc(23456789));//true

fnInstance.mock();//1-018-789-4795 x9374
fnInstance.mock();//(953) 814-4850
fnInstance.mock();//(788) 031-9558
fnInstance.mock();//(567) 476-8360 x436
fnInstance.mock();//1-430-650-6285 x861

address

Representing an address value.

Implementing features

Comparable, Mockable

Sample

var fnInstance = address();
fnInstance.compareFunc('Block 10, Baker Rd., UK'));//true
fnInstance.compareFunc('Block 10, \nBaker Rd., UK'));//true

fnInstance.mock();//"881 Hagenes Cape"
fnInstance.mock();//"74087 Nayeli Trace"
fnInstance.mock();//"087 Heller Divide"
fnInstance.mock();//"495 Parisian Lock"
fnInstance.mock();//"2458 Jeromy Village"

uuid4

Representing an uuid4 value.

Implementing features

Comparable, Mockable

Sample

var fnInstance = uuid4();
fnInstance.compareFunc('a341a6a8-b4de-4880-8942-d67c3c4f30fc'));//true
fnInstance.compareFunc('3caea76e-7060-471f-a0d8-11b7e272956b'));//true

fnInstance.mock();//"04072af7-b00b-4046-b669-42eeb17fa8da"
fnInstance.mock();//"0bd7cfcf-2cd0-48c4-bd4b-6e36e2b3f6cb"
fnInstance.mock();//"b4a8512c-addf-4596-82ea-d0a3e385b016"

jsonpath

Evaluating value from context's json path.

Implementing features

Evaluator

Function arguments

jsonpath(expression)

ArgumentDescriptionRequiredExample
expressionThe jsonpath expressionmandatory"$.req.body"

Sample

var fnInstance = jsonpath("$.req.body");

fnInstance.evaluate({
    req: {
        body: 100
    }
});
//100

fnInstance.evaluate({
    req: {
        body: "abc"
    }
});
//"abc"

sum

Evaluating sum of values

Implementing features

Evaluator

Function arguments

sum(values...)

ArgumentDescriptionRequiredExample
values...The function arguments are the input values for evaluating the summandatory1,2,3,4,...

Sample

var fnInstance = sum(6, 200, 50, 1000);
fnInstance.evaluate({});//1256

var fnInstance = sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
fnInstance.evaluate({});//55

var fnInstance = sum(jsonpath("$.req.body"), 1);
fnInstance.evaluate({
    req: {
        body: 100
    }
});
//101

subtract

Evaluating subtract result of two values

Implementing features

Evaluator

Function arguments

subtract(a, b)

ArgumentDescriptionRequiredExample
aThe number which would be subtracted by bmandatory10
bThe number which would be subtracted from amandatory3

Sample

var fnInstance = subtract(10, 3);
fnInstance.evaluate({});//7

var fnInstance = subtract(jsonpath("$.req.body"), 1);
fnInstance.evaluate({
    req: {
        body: 100
    }
});
//99

multiply

Evaluating multiply result of values

Implementing features

Evaluator

Function arguments

multiply(values...)

ArgumentDescriptionRequiredExample
values...The function arguments are the input values for evaluating the multiply resultmandatory1,2,3,4,...

Sample

var fnInstance = multiply(2, 4, 10);
fnInstance.evaluate({});//80

var fnInstance = multiply(1, 2, 3, 4, 5);
fnInstance.evaluate({});//120

var fnInstance = multiply(jsonpath("$.req.body"), 2);
fnInstance.evaluate({
    req: {
        body: 100
    }
});
//200

divide

Evaluating divide result of two values

Implementing features

Evaluator

Function arguments

divide(a, b)

ArgumentDescriptionRequiredExample
aThe number which would be divided by bmandatory12
bThe number which would be divided from amandatory3

Sample

var fnInstance = subtract(12, 3);
fnInstance.evaluate({});//4

var fnInstance = subtract(jsonpath("$.req.body"), 25);
fnInstance.evaluate({
    req: {
        body: 100
    }
});
//4

anyOf

Allow comparing value or mocking value which is any of the listed values.

Implementing features

Comparable, Mockable

Function arguments

anyOf(values...)

ArgumentDescriptionRequiredExample
values...The function arguments are the possible values.mandatory'a', 'b', 'c', ...

Sample

var fnInstance = anyOf('a', 'b', 'c');
fnInstance.compareFunc('a');//true
fnInstance.compareFunc('b');//true
fnInstance.compareFunc('c');//true
fnInstance.compareFunc('d');//false

fnInstance.mock();//"a"
fnInstance.mock();//"c"
fnInstance.mock();//"c"
fnInstance.mock();//"b"

notAnyOf

Allow comparing value which is not any of the listed values.

Implementing features

Comparable

Function arguments

notAnyOf(values...)

ArgumentDescriptionRequiredExample
values...The function arguments are the values that shouldn't bemandatory'a', 'b', 'c', ...

Sample

var fnInstance = notAnyOf('a', 'b', 'c');
fnInstance.compareFunc('a');//false
fnInstance.compareFunc('b');//false
fnInstance.compareFunc('c');//false
fnInstance.compareFunc('d');//true

evalContext

Evaluating value from the context

Implementing features

Evaluator

Function arguments

evalContext(evalFunction)

ArgumentDescriptionRequiredExample
evalFunctionThe function for evaluating contextmandatoryfunction(context){...}

Sample

var fnInstance = evalContext(function (context) {
    return context.req.body.a + context.req.body.b;
});

fnInstance.evaluate({
    req: {
        body: {
            a: 1,
            b: 2
        }
    }
});//3

var fnInstance = evalContext(function (context) {
    return "Hello " + context.req.body.name;
});

fnInstance.evaluate({
    req: {
        body: {
            name: "Mary"
        }
    }
});//"Hello Mary"

value

Evaluating "Stub" value or "Test" value. During evaluating value, it will look up the context's "isTest" value. If "isTest" is true, then it will evaluating test value, otherwise stub value.

Implementing features

Evaluator

Options

Option keyDescriptionRequiredExample
stubThe value being evaluated when evaluating stub valuemandatoryany
testThe value being evaluated when evaluating test valuemandatoryany

Sample

var fnInstance = value({ stub: 'a', test: 'b'});

fnInstance.evaluate({});//"a"
fnInstance.evaluate({});//"b"

var fnInstance = value({ stub: jsonpath("$.req.body"), test: "123"});

fnInstance.evaluate({
    req: {
        body: "abc"
    }
});//"abc"

fnInstance.evaluate({
    req: {
        body: "abc"
    },
    isTest: true
});//"123"