Nickydonna
Nickydonna

Reputation: 802

How to validate a model in sailsjs

I'm using Sails and I have created a model, and most of its attributes are required, but if I post a JSON with most of its attributes missing it will still create the record in the db. I'm using sails-postgresql. I've even tried doing it manually with:

ModelName.create(req.body).done(function(){})

Upvotes: 2

Views: 2065

Answers (1)

4ega
4ega

Reputation: 2122

You need to add a special fields to your models attributes - "required: true" or "minLength: 5"

module.exports = {
  attributes: {
    name: {
      type: 'string',
      maxLength: 20,
      minLength: 5
    },

    email: {
      type: 'email',
      required: true
    }
  }
}

You can read about validation in sails models here - http://sailsjs.org/#!documentation/models

Upvotes: 3

Related Questions