Skip to content

Instantly share code, notes, and snippets.

@ciwolsey
Created November 30, 2014 21:24
Show Gist options
  • Select an option

  • Save ciwolsey/189c7168e85185fdadc4 to your computer and use it in GitHub Desktop.

Select an option

Save ciwolsey/189c7168e85185fdadc4 to your computer and use it in GitHub Desktop.
Schemas = {};
Schemas.HostLogs = new SimpleSchema({
ip: {
type: String,
label: "IP",
custom: function(){
if(this.isFromTrustedCode){
// trusted, no need for regex validation
} else {
var value = this.value;
var ip = SimpleSchema.RegEx.IP;
var domain = SimpleSchema.RegEx.Domain;
if(!(ip.test(value) || domain.test(value))){
return "Hostname must be either an IP or domain"
}
}
}
},
port: {
type: Number,
label: "Port",
min: 1,
max: 65535,
},
status: {
type: String,
optional: true,
}
});
HostLogs = new Mongo.Collection('hostlogs');
HostLogs.attachSchema(Schemas.HostLogs);
var hostLogsContext = Schemas.HostLogs.namedContext('default');
// Enabling unique indexing of ip/port
if(Meteor.isServer){
HostLogs._ensureIndex({ip:1, port:1}, {unique: 1});
}
@zimme
Copy link
Copy Markdown

zimme commented Dec 1, 2014

Schemas.HostLogs = new SimpleSchema({
  ip: {
    custom: function() {
      if (!this.isFromTrustedCode) {
        regex = new RexExp(SimpleSchema.RegEx.IP.source + '|' + SimpleSchema.RegEx.Domain.source);
        if (!regex.test(this.value)) {
          return 'regEx';
        }
      }
    },
    label: 'Address',
    type: String
  }
});

Schemas.HostLogs.messages({
  'regEx ip': '[label] needs to be an IP address or a domain name'
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment