Skip to content

Commit

Permalink
added DateNode, regex assert method only for String, Number and Mixed…
Browse files Browse the repository at this point in the history
… nodes update unit tests
  • Loading branch information
ChristiaanScheermeijer committed Sep 23, 2014
1 parent 327aa33 commit 54c813e
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 163 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,12 @@ var plainConfiguration = config.get();

#### BooleanNode
- required()
- regex(expression)

#### ObjectNode
- required()
- regex(expression)

#### ArrayNode
- required()
- regex(expression)
- count(min, max)

#### MixedNode
Expand All @@ -203,7 +200,6 @@ var plainConfiguration = config.get();

#### FunctionNode
- required()
- regex(expression)

#### RegexNode
- required()
Expand All @@ -227,8 +223,10 @@ gender.set('not a gender'); // throws AssertError

- fixed issue #1
- fixed issue #2
- regex assert only for String, Number and Mixed nodes
- added utils
- added RegexNode
- added DateNode

### 0.1.0

Expand Down
66 changes: 54 additions & 12 deletions dist/configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ utils.isRegexp = function (value) {
return toString(value) === '[object RegExp]';
};

utils.isDate = function (value) {
return toString(value) === '[object Date]';
};

/**
* @param node
* @param message
Expand Down Expand Up @@ -111,12 +115,14 @@ function RegexAssert(expression, expect) {
}
var regexResult = expect || true;
this.test = function (node, value) {
if (true === utils.isRegexp(regex)) {
if (regex.test(value) !== regexResult) {
throw new AssertError(node, 'RegexAssert', 'Expected value to match regex `' + regex.toString() + '`');
if (true === utils.isString(value) || true === utils.isNumber(value)) {
if (true === utils.isRegexp(regex)) {
if (regex.test(value) !== regexResult) {
throw new AssertError(node, 'RegexAssert', 'Expected value to match regex `' + regex.toString() + '`');
}
} else {
throw new AssertError(node, 'WrongRegexAssert', 'Regex not valid');
}
} else {
throw new AssertError(node, 'WrongRegexAssert', 'Regex not valid');
}
};
}
Expand Down Expand Up @@ -201,6 +207,15 @@ function RegexTypeAssert() {
};
}

function DateTypeAssert() {
this.test = function (node, value) {
if (false === utils.isDate(value)) {
throw new AssertError(node, 'Date', 'type mismatch expected `[object Date]` got `' + utils.callToString(value) + '`');
}
return true;
};
}

/**
* @constructor
*/
Expand Down Expand Up @@ -248,11 +263,6 @@ Node.prototype.setDefault = function (val) {
return this;
};

Node.prototype.regex = function (expr, expects) {
this.asserts.push(new RegexAssert(expr, expects));
return this;
};

Node.prototype.getLongname = function () {
var name = [this.name], prev = this;

Expand Down Expand Up @@ -373,6 +383,20 @@ function BooleanNode(name, children, parent) {

BooleanNode.prototype = Object.create(Node.prototype);

/**
* @param name
* @param [children]
* @param [parent]
*
* @implements Node
* @constructor
*/
function DateNode(name, children, parent) {
Node.apply(this, [name, new DateTypeAssert(), children, parent, true]);
}

DateNode.prototype = Object.create(Node.prototype);

/**
* @param name
* @param [children]
Expand Down Expand Up @@ -417,6 +441,11 @@ function MixedNode(name, children, parent) {
this.asserts.push(new NotEmptyAssert());
return this;
};

this.regex = function (expr, expects) {
this.asserts.push(new RegexAssert(expr, expects));
return this;
};
}

MixedNode.prototype = Object.create(Node.prototype);
Expand Down Expand Up @@ -458,6 +487,10 @@ function NodeChildren(parent) {
return new RegexNode(name, null, parent);
};

this.dateNode = function (name) {
return new DateNode(name, null, parent);
};

this.end = function () {
return parent;
};
Expand Down Expand Up @@ -488,6 +521,11 @@ function NumberNode(name, children, parent) {
this.asserts.push(new ChoiceAssert(choices));
return this;
};

this.regex = function (expr, expects) {
this.asserts.push(new RegexAssert(expr, expects));
return this;
};
}

NumberNode.prototype = Object.create(Node.prototype);
Expand Down Expand Up @@ -516,8 +554,6 @@ ObjectNode.prototype = Object.create(Node.prototype);
*/
function RegexNode(name, children, parent) {
Node.apply(this, [name, new RegexTypeAssert(), children, parent, true]);

this.regex = null;
}

RegexNode.prototype = Object.create(Node.prototype);
Expand Down Expand Up @@ -552,6 +588,11 @@ function StringNode(name, children, parent) {
this.asserts.push(new NotEmptyAssert());
return this;
};

this.regex = function (expr, expects) {
this.asserts.push(new RegexAssert(expr, expects));
return this;
};
}

StringNode.prototype = Object.create(Node.prototype);
Expand All @@ -578,6 +619,7 @@ configurator.ObjectNode = ObjectNode;
configurator.MixedNode = MixedNode;
configurator.FunctionNode = FunctionNode;
configurator.RegexNode = RegexNode;
configurator.DateNode = DateNode;

if (typeof define === 'function' && define.amd) {
define([], function () {
Expand Down

0 comments on commit 54c813e

Please sign in to comment.