Skip to content
Christian Alfoni edited this page Mar 5, 2015 · 1 revision

Given you pass the correct parameters, a baobab tree is able to check whether its data is valid or not against the supplied specification.

This specification must be written in the typology library's style.

Example

var baobab = new Baobab(

  // Initial state
  {
    hello: 'world',
    colors: ['yellow', 'blue'],
    counters: {
      users: 3,
      groups: 1
    }
  },

  // Parameters
  {
    validate: {
      hello: '?string',
      colors: ['string'],
      counters: {
        users: 'number',
        groups: 'number'
      }
    }
  }
);

// If one updates the tree and does not respect the validation specification
baobab.set('hello', 42);

// Then the tree will fire an 'invalid' event containing a list of errors
baobab.on('invalid', function(e) {
  console.log(e.data.errors);
});