js-data/js-data-angular

Schemator configuration?

Closed this issue · 2 comments

I'm trying to define custom validation rules using the Schemator API. This repo requires js-data-schema as a dependency.

Is the best practice to just call

var schemator = window.Schemator;
schemator.defineRule();

Thanks

You should be able do to this:

angular.module('myApp', ['js-data']).run(function (DS) {
  DS.schemator.defineRule('divisibleBy', function (x, divisor) {
    if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
      return {
        rule: 'divisibleBy',
        actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
        expected: '' + x + ' % ' + divisor + ' === 0'
      };
    }
    return null;
  });
});

I figured out why DS.schemator is undefined when using browserify. Will be fixed soon. See js-data/js-data#104