Skip to content

Commit

Permalink
added FunctionNode and added Changelog to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Sep 7, 2014
1 parent 9b6989b commit 7b67966
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 10 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ var plainConfiguration = config.get();
- choice(choices)
- notEmpty()

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

### Single parameter validation

It also possible to create a validator for a single parameter instead using a root object.
Expand All @@ -192,10 +196,26 @@ gender.get(); // male
gender.set('not a gender'); // throws AssertError
```

## Changelog

### Dev

- added FunctionNode

### 0.0.2a

- added MixedNode
- added ChoiceAssert
- added NotEmptyAssert
- added CountAssert

### 0.0.1a

- initial commit

## TODO

- add more assertions
- add FunctionNode
- add option to not throw errors
- add option to not fail on a single error and continue validating (even set validated values?)
- improve get logics or change it (maybe add property string) i.e. `.get('nested.property1')`
Expand Down
32 changes: 30 additions & 2 deletions dist/configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ AssertError.prototype = Object.create(Error.prototype);
function CountAssert(min, max) {
this.test = function (node, value) {
if (typeof min === 'number' && value.length < min) {
throw new AssertError(node, 'CountAssert', 'Expected array to contain `' + min + '` or more values');
throw new AssertError(node, 'CountAssert', 'Expected array to contain ' + min + ' or more values');
}

if (typeof max === 'number' && value.length > max) {
throw new AssertError(node, 'CountAssert', 'Expected array to contain `' + max + '` or less values');
throw new AssertError(node, 'CountAssert', 'Expected array to contain ' + max + ' or less values');
}
};
}
Expand Down Expand Up @@ -136,6 +136,15 @@ function ArrayAssert() {
};
}

function FunctionAssert() {
this.test = function (node, value) {
if (typeof value !== 'function') {
throw new AssertError(node, 'Function', 'type mismatch expected `function` got `' + typeof value + '`');
}
return true;
};
}

/**
* @constructor
*/
Expand Down Expand Up @@ -308,6 +317,20 @@ function BooleanNode(name, children, parent) {

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

/**
* @param name
* @param [children]
* @param [parent]
*
* @implements Node
* @constructor
*/
function FunctionNode(name, children, parent) {
Node.apply(this, [name, new FunctionAssert(), children, parent, false]);
}

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

/**
* @param name
* @param [children]
Expand Down Expand Up @@ -371,6 +394,10 @@ function NodeChildren(parent) {
return new MixedNode(name, null, parent);
};

this.functionNode = function (name) {
return new FunctionNode(name, null, parent);
};

this.end = function () {
return parent;
};
Expand Down Expand Up @@ -473,6 +500,7 @@ configurator.BooleanNode = BooleanNode;
configurator.ArrayNode = ArrayNode;
configurator.ObjectNode = ObjectNode;
configurator.MixedNode = MixedNode;
configurator.FunctionNode = FunctionNode;

if (typeof define === 'function' && define.amd) {
define([], configurator);
Expand Down
4 changes: 2 additions & 2 deletions dist/configurator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/asserts/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ function ArrayAssert() {
}
return true;
};
}

function FunctionAssert() {
this.test = function (node, value) {
if (typeof value !== 'function') {
throw new AssertError(node, 'Function', 'type mismatch expected `function` got `' + typeof value + '`');
}
return true;
};
}
3 changes: 2 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ configurator.NumberNode = NumberNode;
configurator.BooleanNode = BooleanNode;
configurator.ArrayNode = ArrayNode;
configurator.ObjectNode = ObjectNode;
configurator.MixedNode = MixedNode;
configurator.MixedNode = MixedNode;
configurator.FunctionNode = FunctionNode;
13 changes: 13 additions & 0 deletions src/node/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param name
* @param [children]
* @param [parent]
*
* @implements Node
* @constructor
*/
function FunctionNode(name, children, parent) {
Node.apply(this, [name, new FunctionAssert(), children, parent, false]);
}

FunctionNode.prototype = Object.create(Node.prototype);
4 changes: 4 additions & 0 deletions src/node/nodeChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function NodeChildren(parent) {
return new MixedNode(name, null, parent);
};

this.functionNode = function (name) {
return new FunctionNode(name, null, parent);
};

this.end = function () {
return parent;
};
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/ConfiguratorHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function methodConfigHelper() {
.mixedNode('mixedNode').choice([true, 'string', 5]).end()
.mixedNode('mixedNodeNotEmpty').notEmpty().end()
.arrayNode('arrayCount').count(1, 10).end()
.functionNode('callback').end()
.end();
});

Expand Down Expand Up @@ -78,7 +79,8 @@ function argConfigHelper() {
new configurator.MixedNode('mixedNodeWithAssert').greaterThan(0).lessThan(10),
new configurator.MixedNode('mixedNode').choice([true, 'string', 5]),
new configurator.MixedNode('mixedNodeNotEmpty').notEmpty(),
new configurator.ArrayNode('arrayCount').count(1, 10)
new configurator.ArrayNode('arrayCount').count(1, 10),
new configurator.FunctionNode('callback')
]);
});

Expand Down
23 changes: 20 additions & 3 deletions test/helpers/SharedPropertiesSpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function sharedPropertiesSpecHelper() {

expect(function () {
config.set({
property1: 5,
property1: 5,
mixedWithChildren: {
key1: 'string',
key2: 'string'
Expand All @@ -105,20 +105,37 @@ function sharedPropertiesSpecHelper() {

expect(function () {
config.set({
property1: 5,
property1: 5,
mixedWithChildren: [true, 'anotherstring']
});
}).toThrowError(/type mismatch expected `string` got `boolean`/);

expect(function () {
config.set({
property1: 5,
property1: 5,
mixedWithChildren: {
key1: true,
key2: 'string'
}
});
}).toThrowError(/type mismatch expected `string` got `boolean`/);
});

it('should validate a function nodes', function () {
expect(function () {
config.set({
property1: 5,
callback: function () {
}
});
}).not.toThrow();

expect(function () {
config.set({
property1: 5,
callback: 'string'
});
}).toThrowError(/type mismatch expected `function` got `string`/);
});
});
}

0 comments on commit 7b67966

Please sign in to comment.