Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bbraithwaite
Created February 3, 2015 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbraithwaite/4189bab0e24ad0e270b4 to your computer and use it in GitHub Desktop.
Save bbraithwaite/4189bab0e24ad0e270b4 to your computer and use it in GitHub Desktop.
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Validation
*/
function validateLength (v) {
// a custom validation function for checking string length
return v.length <= 15;
}
/**
* Category Schema
*/
var CategorySchema = new Schema({
created: { // the property name
type: Date, // types are defined e.g. String, Date, Number - http://mongoosejs.com/docs/guide.html
default: Date.now
},
description: {
type: String,
default: '',
trim: true // types have specific functions e.g. trim, lowercase, uppercase - http://mongoosejs.com/docs/api.html#schema-string-js
},
name: {
type: String,
default: '',
trim: true,
unique : true,
required: 'name cannot be blank',
validate: [validateLength, 'name must be 15 chars in length or less'] // wires into our custom validator function - http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate
}
});
// Expose the model to other objects (similar to a 'public' setter).
mongoose.model('Category', CategorySchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment