-1

I am writing an Angular app coming from a PHP background. I wanted to know if there are any good assert/validation libraries I can use to help me validation the arguments passed in to my models e.g. Validate.isNumber(data.id) and automatically throw an appropriate exception on failure

app.factory('Something', ['moment', function(moment){

    function Something(data) {

        // validate data has required properties and they are of the expected type!!!

        this.id = data.id;
        this.title = data.title;
        this.description = data.description;
    }

    Something.prototype.getId = function () {
        return this.id;
    };

    Something.prototype.getTitle = function () {
        return this.title;
    };

    Something.prototype.getDescription = function () {
        return this.description;
    };

    return Something;

}]);
Ben_hawk
  • 2,476
  • 7
  • 34
  • 59

1 Answers1

0

I personally use this JSON-Validator tv4:

https://github.com/geraintluff/tv4

It relies on a schema you provide to tell it how your passed arguments(JSON-structure) have to be formed and what kind of types are allowed.

And here you find all validation-options it supports:

http://json-schema.org/latest/json-schema-validation.html

Here you find a practical example

Community
  • 1
  • 1
Blauharley
  • 4,186
  • 6
  • 28
  • 47