Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
v0.2.2
Browse files Browse the repository at this point in the history
- added optimization of structs validation when `value` is an instance
of `type`
  • Loading branch information
gcanti committed Dec 6, 2014
1 parent 56af3c4 commit 2affa68
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 21 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.log
node_modules
build
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.2.2

- added optimization of structs validation when `value` is an instance of `type`

v0.2.1

- update to tcomb v0.3.4
Expand Down
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tcomb-validation",
"main": "index.js",
"version": "0.2.1",
"version": "0.2.2",
"homepage": "https://github.com/gcanti/tcomb-validation",
"authors": [
"gcanti <giulio.canti@gmail.com>"
Expand All @@ -28,7 +28,6 @@
"package.json",
"Gruntfile.js",
".jshintrc",
"CHANGELOG.md",
"LICENSE",
"README.md"
]
Expand Down
31 changes: 18 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@
// validate
//

function _validate(x, type, path) {
var kind = t.util.getKind(type);
return validators[kind](x, type, path);
function validate(x, type) {
return new ValidationResult(recurse(x, type, []));
}

function validate(x, type) {
return new ValidationResult(_validate(x, type, []));
function recurse(x, type, path) {
var kind = t.util.getKind(type);
return validators[kind](x, type, path);
}

var validators = {};
Expand All @@ -98,7 +98,7 @@
var ret = {value: [], errors: []};
// every item should be of type `type.meta.type`
for (var i = 0, len = x.length ; i < len ; i++ ) {
var item = _validate(x[i], type.meta.type, path.concat(i));
var item = recurse(x[i], type.meta.type, path.concat(i));
ret.value[i] = item.value;
ret.errors = ret.errors.concat(item.errors);
}
Expand All @@ -108,7 +108,7 @@
validators.subtype = function validateSubtype(x, type, path) {

// x should be a valid inner type
var ret = _validate(x, type.meta.type, path);
var ret = recurse(x, type.meta.type, path);
if (ret.errors.length) {
return ret;
}
Expand All @@ -125,7 +125,7 @@
validators.maybe = function validateMaybe(x, type, path) {
return t.Nil.is(x) ?
{value: null, errors: []} :
_validate(x, type.meta.type, path);
recurse(x, type.meta.type, path);
};

validators.struct = function validateStruct(x, type, path) {
Expand All @@ -135,12 +135,17 @@
return {value: x, errors: [ValidationError.of(x, type, path)]};
}

// [optimization]
if (type.is(x)) {
return {value: x, errors: []};
}

var ret = {value: {}, errors: []};
var props = type.meta.props;
// every item should be of type `props[name]`
for (var name in props) {
if (props.hasOwnProperty(name)) {
var prop = _validate(x[name], props[name], path.concat(name));
var prop = recurse(x[name], props[name], path.concat(name));
ret.value[name] = prop.value;
ret.errors = ret.errors.concat(prop.errors);
}
Expand All @@ -164,7 +169,7 @@
var ret = {value: [], errors: []};
// every item should be of type `types[i]`
for (var i = 0 ; i < len ; i++ ) {
var item = _validate(x[i], types[i], path.concat(i));
var item = recurse(x[i], types[i], path.concat(i));
ret.value[i] = item.value;
ret.errors = ret.errors.concat(item.errors);
}
Expand All @@ -184,8 +189,8 @@
for (var k in x) {
if (x.hasOwnProperty(k)) {
path = path.concat(k);
var key = _validate(k, type.meta.domain, path);
var item = _validate(x[k], type.meta.codomain, path);
var key = recurse(k, type.meta.domain, path);
var item = recurse(x[k], type.meta.codomain, path);
ret.value[k] = item.value;
ret.errors = ret.errors.concat(key.errors, item.errors);
}
Expand All @@ -196,7 +201,7 @@
validators.union = function validateUnion(x, type, path) {
var ctor = type.dispatch(x);
return t.Func.is(ctor)?
_validate(x, ctor, path.concat(type.meta.types.indexOf(ctor))) :
recurse(x, ctor, path.concat(type.meta.types.indexOf(ctor))) :
{value: x, errors: [ValidationError.of(x, type, path)]};
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tcomb-validation",
"version": "0.2.1",
"version": "0.2.2",
"description": "General purpose validation library for JavaScript",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions tcomb-validation.min.js

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

2 changes: 1 addition & 1 deletion tcomb-validation.min.js.map

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

3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function success(value) {
//

var ok = function (x) { assert.strictEqual(true, x); };
var eq = assert.deepEqual;
var eq = assert.deepEqual;

describe('validate()', function () {

Expand Down Expand Up @@ -58,6 +58,7 @@ describe('validate()', function () {
eq(validate({x: 0, y: 0}, Point), success({x: 0, y: 0}));
ok(validate({x: 0, y: 0}, Point).value instanceof Point);
eq(validate({x: 0, y: 'a'}, Point), failure('a', Num, ['y'], '/y is `"a"` should be a `Num`', {x: 0, y: 'a'}));
eq(validate(new Point({x: 0, y: 0}), Point), success({x: 0, y: 0}));
});

it('list', function () {
Expand Down

0 comments on commit 2affa68

Please sign in to comment.