Skip to content
Validator library for Java Script objects
JavaScript HTML
Find file
Latest commit 13617fd Krzysztof Sopa New build
Failed to load latest commit information.
build New build
src New build
.gitignore Change Validator interface
README.md Update readme
bower.json New build
package.json New build

README.md

JsonValidatorJs

Simple validation library written in ES6.

Instalation

$ bower install jsonvalidatorjs --save

Usage

var validator = Validator.create();

validator
    .addField('area').validate('isNotEmpty').validate('isSmallerThen', 20).validate('isInteger')
    .addField('price').validate('isNotEmpty').validate('isGreaterThen', 20)
    .addField('description').validate('isNotEmpty').validate('isSmallerThen', 20);


var valid = validator.isValid({
  area: '200string',
  price: 300000,
  description: 'Super description'
});

console.log(validator.getErrorsList()); // ['area must be integer', 'description must be smaller then 5 characters']
console.log(valid); // false

jsfiddle: https://jsfiddle.net/1meakg8v/1/

Using with AngularJS

After instalation the library will be available through $window service.

It can be injected as follow:

'use strict';

angular.module('app').factory('Validator', [
  '$window',
  function ($window) {
    return {
      create: function () {
        return $window.Validator;
      }
    }
  }
]);

angular.module('app').controller('ModelController', [
  'Validator', '$scope',
  function(
    Validator, $scope
  ) {
    var validator = Validator.create();

    //define rules
    validator.begin()
      .addField('area').validate('isNotEmpty').validate('isSmallerThen', 20).validate('isInteger')
      .addField('price').validate('isNotEmpty').validate('isSmallerThen', 20).validate('isInteger')
      .addField('description').validate('isNotEmpty').validate('isSmallerThen', 20);

    $scope.update = function() {
      var valid = validator.isValid($scope.model);
      console.log(validator.getErrorsList());
    };
  }
]);

Validators

License

MIT

Something went wrong with that request. Please try again.