Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Why Mongoose validation isn't working in Keystone.js app?

Is there validation on required fields, email field and name field in Keystone.js?

Here is my User model for example:

const keystone = require('keystone');
const Types = keystone.Field.Types;
const User = new keystone.List('User');

User.add({
  name: { type: Types.Name, required: true, initial: true, index: true },
  email: { type: Types.Email, required: true, initial: true, index: true, unique: true },
  password: { type: Types.Password, initial: true, required: true },
}, 'Permissions', {
  isAdmin: { type: Boolean, label: 'Can access Keystone', index: true },
});

// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function () {
  return this.isAdmin;
});

User.defaultColumns = 'name, email, isAdmin';
User.register();

And some sort of unit tests. The last two fail because error isn't throwing.

const User = keystone.list('User').model;

describe('User model', function () {
  const newUser = {
    name: {
      first: 'Test',
      last: 'Testov',
    },
    email: 'test@test.com',
    password: 'test',
  };

  describe('validation', function () {
    it('should reject a new user without email', function (done) {
      const invalidUser = Object.assign({}, newUser);
      delete invalidUser.email;
      new User(invalidUser).validate((err) => {
        assert.equal('Path `email` is required.', err.errors.email.message);
        done();
      });
    });

    it('should reject a new user without password', function (done) {
      const invalidUser = Object.assign({}, newUser);
      delete invalidUser.password;
      new User(invalidUser).validate((err) => {
        assert.equal('Path `password` is required.', err.errors.password.message);
        done();
      });
    });

    it('should reject a new user with invalid email', function (done) {
      const invalidUser = Object.assign({}, newUser);
      invalidUser.email = 'roflrofl';
      new User(invalidUser).validate((err) => {
        console.log(err);
        assert.isOk(err);
        done();
      });
    });

    it('should reject a new user without name', function (done) {
      const invalidUser = Object.assign({}, newUser);
      invalidUser.name = { omg: 'wtf' };
      new User(invalidUser).validate((err) => {
        console.log(err);
        assert.isOk(err);
        done();
       });
    });
  });
});

So the first two unit tests is passing positive but the last two not. Why the validation isn't work?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's because the validate method isn't a keystone method, but one that mongoose provides. And mongoose has far more limited validation than keystone. The required: true options are passed to the mongoose schema, which is why it throws when you're trying to leave a required field blank. There's no real keystone equivalent for validate, but the (mongoose+keystone) validation will run whenever you try to save a document.

P.s. realised too late that it had nothing to do with the Keystone version

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!