Comments (2)
Hi,
If you need unique field for radis adapter, please use custom async validation.
docs here
example 1:
User.validatesUniquenessOf('email', {message: 'not unique'});
example 2:
/* define model */
var User = schema.define('User', {
email: { type: schema.String, limit: 150 },,
name: { type: schema.String, limit: 250 },,
bio: { type: schema.Text },,
approved: { type: schema.Boolean, default: false },
joinedAt: { type: schema.Date, default: Date.now },
age: { type: schema.Number, default: 0 }
});
/* custom validator */
function emailValidator(err, done) {
User.findOne( { email : this.email }).exec(function (e,u) {
if (u) { err(); }
done();
});
});
/* define validations */
User.validatesLengthOf('email', {min: 3, max: 150 , message: {min: 'is too short', max: 'is too long'}});
User.validatesLengthOf('name', {min: 2, max: 250 , message: {min: 'is too short', max: 'is too long'}});
/* custom validation */
User.validateAsync('email', emailValidator, {message: 'is duplicate'});
var user = new User ({ email: "[email protected]", name: 'Jdoe' });
user.isValid(function (isValid) {
if(isValid) {
user.save(function (err) {
if(err) { return console.log('user create err:', err); }
console.log('user created');
});
} else {
console.log('user validation error:', user.errors);
}
});
from caminte.
Hey dude, when trying your example 2 above I get this error whenever I try to get a record:
/home/robert/src/phoenix/api/node_modules/caminte/lib/adapters/redis.js:591
throw new Error(model + ': no indexes found for ' +
^
Error: reading: no indexes found for address impossible to sort and filter using redis adapter
at /home/robert/src/phoenix/api/node_modules/caminte/lib/adapters/redis.js:591:19
at BridgeToRedis.possibleIndexes (/home/robert/src/phoenix/api/node_modules/caminte/lib/adapters/redis.js:553:9)
at BridgeToRedis.all (/home/robert/src/phoenix/api/node_modules/caminte/lib/adapters/redis.js:580:10)
at Function.all (/home/robert/src/phoenix/api/node_modules/caminte/lib/abstract-class.js:496:31)
at Function.findOne (/home/robert/src/phoenix/api/node_modules/caminte/lib/abstract-class.js:541:18)
at Query.self.(anonymous function) [as exec] (/home/robert/src/phoenix/api/node_modules/caminte/lib/query.js:42:31)
at ModelConstructor.addressValidator (/home/robert/src/phoenix/api/models/Reading.js:22:48)
at ModelConstructor.validateCustom (/home/robert/src/phoenix/api/node_modules/caminte/lib/validatable.js:301:26)
at validationFailed (/home/robert/src/phoenix/api/node_modules/caminte/lib/validatable.js:475:15)
at /home/robert/src/phoenix/api/node_modules/caminte/lib/validatable.js:385:17
It also does not work when saving, as every save operation returns an error (400 bad request).
This is the model:
module.exports = function(schema){
var Reading = schema.define('reading', {
address : { type : schema.String, unique: true },
timestamp : { type : schema.Date },
data : { type : schema.Json }
});
// this didn't work either
//Reading.validatesUniquenessOf('address', {message: 'not unique'});
var addressValidator = function(err, done) {
Reading.findOne({address: this.address}, function (e,u) {
if (u) { err(); }
done();
});
};
Reading.validateAsync('address', addressValidator, {message: 'is not unique'});
return Reading;
};
Any ideas?
from caminte.
Related Issues (20)
- Mysql adapter models get reset after reconnection
- Json objects returned as strings for "memory" adapter
- support mysql2 driver
- Database Changes
- Database connection error handlers
- Mongoose driver never calls remove callback if no record to delete was found
- sql.js
- I cannot get each column that is specified in mongodb. HOT 1
- two or more database HOT 2
- promise is not return for all/find when asking for all records HOT 2
- MySQL Between Bug
- TypeError [ERR_INVALID_CALLBACK]: Callback must be a function HOT 4
- Electron Integration Example
- Any plan to support GraphQL? HOT 2
- OR clause in where condition not working
- Auxiliary map
- error using caminte -i -a mysql HOT 2
- MYSQL case sensitive comparison
- Domain camintejs.com expired HOT 2
- Composite primary key HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from caminte.