Define schemas using shorthand and longhand syntax
mainSimpleSchema supports three ways to define schema structures:
- Shorthand: Map a property name directly to a type (e.g.,
name: String). This ensures the property is present and matches the type. - Longhand: Use an object to define a type along with additional rules like
max,optional, ordefaultValue(e.g.,name: { type: String, max: 40 }). - Mixed: Combine both syntaxes within a single schema definition.
Special Shorthand Rules:
- Regex: Setting a key to a regular expression automatically treats the type as
Stringwith aregExrule. - Arrays: Setting a key to an array of a type (e.g.,
[String]) is shorthand for an array type where every element must match that type.
import SimpleSchema from "simpl-schema";
// Shorthand
const shorthandSchema = new SimpleSchema({
name: String,
age: SimpleSchema.Integer,
registered: Boolean,
exp: /foo/,
friends: [String],
});
// Longhand
const longhandSchema = new SimpleSchema({
name: {
type: String,
max: 40,
},
age: {
type: SimpleSchema.Integer,
optional: true,
},
registered: {
type: Boolean,
defaultValue: false,
},
});