alamid-schema
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "alamid-schema", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "an extendable mongoose-like like schemas for node.js and the browser", | ||
"main": "lib/Schema.js", | ||
"scripts": { | ||
"test": "node node_modules/mocha/bin/mocha -R spec" | ||
"test": "node node_modules/mocha/bin/mocha -R spec" | ||
}, | ||
@@ -30,5 +30,6 @@ "repository": { | ||
"devDependencies": { | ||
"chai": "1.x", | ||
"mocha": "1.x" | ||
"chai": "1.x", | ||
"mocha": "1.x", | ||
"chai-spies": "~0.5.1" | ||
} | ||
} |
@@ -32,13 +32,13 @@ "use strict"; | ||
validators.forEach(function (validator) { | ||
//async | ||
if (validator.length === 2) { | ||
validator.call(context, field, validationDone); | ||
} | ||
//sync | ||
if (validator.length === 1) { | ||
else { | ||
//no setImmediate on client! | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
validationDone(validator.call(context, field)); | ||
}, 0); | ||
} | ||
//async | ||
else { | ||
validator.call(context, field, validationDone); | ||
} | ||
}); | ||
@@ -60,3 +60,3 @@ } | ||
if(arguments.length === 1) { | ||
if (arguments.length === 1) { | ||
schema = arguments[0]; | ||
@@ -88,3 +88,3 @@ } | ||
if (value(fieldDefinition.max).typeOf(Number)) { | ||
this.validators[key].push(defaultValidators.min(fieldDefinition.max)); | ||
this.validators[key].push(defaultValidators.max(fieldDefinition.max)); | ||
} | ||
@@ -91,0 +91,0 @@ |
@@ -48,3 +48,3 @@ "use strict"; | ||
exports.min = minValidator; | ||
exports.max = minValidator; | ||
exports.max = maxValidator; | ||
exports.enum = enumValidator; |
@@ -0,0 +0,0 @@ ## alamid-schema |
@@ -23,7 +23,4 @@ "use strict"; | ||
beforeEach(function () { | ||
it("should return an independent instance", function () { | ||
subset = schema.fields("name", "age"); | ||
}); | ||
it("should return an independent instance", function () { | ||
expect(subset).not.to.equal(schema); | ||
@@ -33,2 +30,3 @@ }); | ||
it("should change the .keys-property to the given keys", function () { | ||
subset = schema.fields("name", "age"); | ||
expect(subset.keys).to.eql(["name", "age"]); | ||
@@ -39,4 +37,14 @@ }); | ||
describe(".fields(keys)", function () { | ||
var subset; | ||
it("should just work like .fields(key1, key2, kex3)", function () { | ||
subset = schema.fields(["name", "age"]); | ||
expect(subset.keys).to.eql(["name", "age"]); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -5,6 +5,9 @@ "use strict"; | ||
expect = chai.expect, | ||
Schema = require("../lib/Schema.js"), | ||
spies = require('chai-spies'); | ||
var Schema = require("../lib/Schema.js"), | ||
validators = require("../plugins/validation/validators.js"), | ||
validationPlugin = require("../plugins/validation/index.js"); | ||
chai.use(spies); | ||
chai.Assertion.includeStack = true; | ||
@@ -20,8 +23,6 @@ | ||
Schema.use(validationPlugin); | ||
describe("validationPlugin", function () { | ||
before(function () { | ||
Schema.use(validationPlugin); | ||
}); | ||
describe("#constructor", function () { | ||
@@ -119,2 +120,22 @@ | ||
}); | ||
describe("mixed validators", function () { | ||
it("should register default validators always before custom validators", function () { | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
min: 1, | ||
validate: [oldEnough, notTooOld] | ||
} | ||
}); | ||
expect(schema.validators.age).to.be.an("array"); | ||
expect(schema.validators.age).to.have.length(3); | ||
expect(schema.validators.age[0].name).to.eql("validateMin"); | ||
expect(schema.validators.age[1]).to.eql(oldEnough); | ||
expect(schema.validators.age[2]).to.eql(notTooOld); | ||
}); | ||
}); | ||
}); | ||
@@ -124,5 +145,142 @@ | ||
}); | ||
describe("sync validators", function (done) { | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
validate: function (age) { | ||
return age > 8; | ||
} | ||
} | ||
}); | ||
schema.validate({ age: 18 }, function (validation) { | ||
expect(validation.result).to.eql(true); | ||
expect(validation.failedFields).to.eql([]); | ||
done(); | ||
}); | ||
}); | ||
describe("async validators", function (done) { | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
validate: function (age, callback) { | ||
setTimeout(function () { | ||
callback(age > 8); | ||
}, 0); | ||
} | ||
} | ||
}); | ||
schema.validate({ age: 18 }, function (validation) { | ||
expect(validation.result).to.eql(true); | ||
expect(validation.failedFields).to.eql([]); | ||
done(); | ||
}); | ||
}); | ||
describe("mixed validators", function () { | ||
function validateAgeAsync(age, callback) { | ||
setTimeout(function () { | ||
callback(age > 8 || "fail-async"); | ||
}, 0); | ||
} | ||
function validateAgeSync(age) { | ||
return age > 8 || "fail-sync"; | ||
} | ||
it("should pass if async & sync validators pass", function (done) { | ||
var asyncSpy = chai.spy(validateAgeAsync), | ||
syncSpy = chai.spy(validateAgeSync); | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
validate: [asyncSpy, syncSpy] | ||
} | ||
}); | ||
schema.validate({ age: 18 }, function (validation) { | ||
expect(asyncSpy).to.have.been.called.once(); | ||
expect(syncSpy).to.have.been.called.once(); | ||
expect(validation.result).to.eql(true); | ||
expect(validation.failedFields).to.eql({}); | ||
done(); | ||
}); | ||
}); | ||
it("should fail if a async and sync validator fail", function (done) { | ||
var asyncSpy = chai.spy(validateAgeAsync), | ||
syncSpy = chai.spy(validateAgeSync); | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
validate: [asyncSpy, syncSpy] | ||
} | ||
}); | ||
schema.validate({ age: 6 }, function (validation) { | ||
expect(asyncSpy).to.have.been.called.once(); | ||
expect(syncSpy).to.have.been.called.once(); | ||
expect(validation.result).to.eql(false); | ||
expect(validation.failedFields.age).to.contain("fail-async", "fail-sync"); | ||
done(); | ||
}); | ||
}); | ||
it("should fail if the async validator fails & sync passes", function (done) { | ||
var asyncSpy = chai.spy(function (age, callback) { | ||
setTimeout(function () { | ||
callback("fail-async"); | ||
}, 0); | ||
}), | ||
syncSpy = chai.spy(validateAgeSync); | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
validate: [asyncSpy, syncSpy] | ||
} | ||
}); | ||
schema.validate({ age: 6 }, function (validation) { | ||
expect(asyncSpy).to.have.been.called.once(); | ||
expect(syncSpy).to.have.been.called.once(); | ||
expect(validation.result).to.eql(false); | ||
expect(validation.failedFields.age).to.contain("fail-async"); | ||
done(); | ||
}); | ||
}); | ||
it("should fail if the sync validator fails & async passes", function (done) { | ||
var asyncSpy = chai.spy(function(age, callback) { | ||
callback(true); | ||
}), | ||
syncSpy = chai.spy(function(age) { | ||
return "fail-sync"; | ||
}); | ||
var schema = new Schema({ | ||
age: { | ||
type: Number, | ||
validate: [asyncSpy, syncSpy] | ||
} | ||
}); | ||
schema.validate({ age: 8 }, function (validation) { | ||
expect(asyncSpy).to.have.been.called.once(); | ||
expect(syncSpy).to.have.been.called.once(); | ||
expect(validation.result).to.eql(false); | ||
expect(validation.failedFields.age).to.contain("fail-sync"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21317
540
3