Coder Social home page Coder Social logo

Comments (2)

biggora avatar biggora commented on September 26, 2024

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.

penguinpowernz avatar penguinpowernz commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.