Skip to content

Instantly share code, notes, and snippets.

@robophil
Last active January 19, 2017 11:49
Show Gist options
  • Save robophil/afe35cca8430bd17e64f70e18fa92179 to your computer and use it in GitHub Desktop.
Save robophil/afe35cca8430bd17e64f70e18fa92179 to your computer and use it in GitHub Desktop.
Custom validation for phone attribute for sails model Donor
/**
* Donor.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
const shortid = require('shortid');
module.exports = {
attributes: {
first_name: {
type: 'string',
required: true
},
last_name: {
type: 'string',
required: true
},
contact_no: {//validate
type: 'string',
required: true,
phoneNo: true
},
email: {//validate
type: 'email',
required: true
},
address: {
type: 'string',
required: true
},
bloodgroup: {
type: 'string',
required: true
},
ip: {//http://ipinfo.io
type: 'string'
},
location: {
type: 'string'
},
geolocation: {
type: 'json',
defaultsTo: {
x: '',
y: ''
}
},
url: {
type: 'string',
},
active: {
type: "boolean",
defaultsTo: true
}
},
// Lifecycle Callbacks
beforeCreate: function (values, cb) {
values.url = shortid.generate();
cb();
},
types: {
phoneNo: function (value) {
var validNo1 = /\+[0-9]{2}\s[0-9]{3}\s[0-9]{4}\s[0-9]{3}$/; //+xx xxx xxxx xxx
var validNo2 = /00[0-9]{2}\s[0-9]{3}\s[0-9]{4}\s[0-9]{3}$/; //00xx xxx xxxx xxx
return validNo1.test(value) || validNo2.test(value);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment