🦉 Props Validation 🦉

As an application becomes complex, it may be quite unsafe to define props in an informal way. This leads to two issues:

A props type system solves both issues, by describing the types and shapes of the props. Here is how it works in Owl:

For example:

class ComponentA extends owl.Component { static props = ['id', 'url']; ... } class ComponentB extends owl.Component { static props = { count: {type: Number}, messages: { type: Array, element: {type: Object, shape: {id: Boolean, text: String } }, date: Date, combinedVal: [Number, Boolean] }; ... }

For each key, a prop definition is either a boolean, a constructor, a list of constructors, or an object:

Examples:

// only the existence of those 3 keys is documented static props = ['message', 'id', 'date'];
// size is optional static props = ['message', 'size?'];
static props = { messageIds: {type: Array, element: Number}, // list of number otherArr: {type: Array}, // just array. no validation is made on sub elements otherArr2: Array, // same as otherArr someObj: {type: Object}, // just an object, no internal validation someObj2: { type: Object, shape: { id: Number, name: {type: String, optional: true}, url: String ]}, // object, with keys id (number), name (string, optional) and url (string) someFlag: Boolean, // a boolean, mandatory (even if `false`) someVal: [Boolean, Date], // either a boolean or a date otherValue: true, // indicates that it is a prop kindofsmallnumber: { type: Number, validate: n => (0 <= n && n <= 10) }, size: { validate: e => ["small", "medium", "large"].includes(e) }, };