Simple Schema

Un package qui va vraiment vous faciliter la vie !!!

En résumer, simple-schema vous permet de définir la structure de chaque Collection ou d'un simple formulaire.

Mais le plus important est que la validation est utilisable autant coté client que coté serveur.

  • https://github.com/aldeed/meteor-simple-schema

  • Voir https://github.com/copleykj/Mesosphere

Exemple:


BookSchema = new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  author: {
    type: String,
    label: "Author"
  },
  copies: {
    type: Number,
    label: "Number of copies",
    min: 0
  },
  lastCheckedOut: {
    type: Date,
    label: "Last date this book was checked out",
    optional: true
  },
  summary: {
    type: String,
    label: "Brief summary",
    optional: true,
    max: 1000
  }
});

Les types de champs

En version simple:

String
Number
Boolean
Object

La même chose comme tableau:

[String]
[Number]
[Boolean]
[Object]
[Date]

SimpleSchema.RegEx

SimpleSchema.RegEx.Email for emails (uses a permissive regEx recommended by W3C, which most browsers use)
SimpleSchema.RegEx.Domain for external domains and the domain only (requires a tld like .com)
SimpleSchema.RegEx.WeakDomain for less strict domains and IPv4 and IPv6
SimpleSchema.RegEx.IP for IPv4 or IPv6
SimpleSchema.RegEx.IPv4 for just IPv4
SimpleSchema.RegEx.IPv6 for just IPv6
SimpleSchema.RegEx.Url for http, https and ftp urls
SimpleSchema.RegEx.Id for IDs generated by Random.id() of the random package, also usable to validate a relation id.
SimpleSchema.RegEx.ZipCode for 5- and 9-digit ZIP codes

owner: {
  type: String,
  regEx: SimpleSchema.RegEx.Id,
  autoValue: function() {
    if (this.isInsert) {
      return Meteor.userId();
    }
  },

extendOptions

https://www.discovermeteor.com/blog/allow-deny-a-security-primer/

SimpleSchema.extendOptions({
  editable: Match.Optional(Boolean)
});

MessageSchema = new SimpleSchema({
  body: {
    type: String,
    editable: true
  },
  createdAt: {
    type: Date
  },
  userId: {
    type: String
  }
});

var whitelist = _.filter(_.keys(MessageSchema), function (property) {
  return MessageSchema[property].editable;
});

Messages.allow({
  update: function (userId, doc, fields, modifier) {
    if (userId && doc.userId === userId && _.difference(fields, whitelist).length === 0) {
      return true;
    }
  }
});