|
| 1 | +describe('validators.type', function() { |
| 2 | + var type = validate.validators.type; |
| 3 | + type = type.bind(type); |
| 4 | + |
| 5 | + afterEach(function() { |
| 6 | + delete validate.validators.type.message; |
| 7 | + delete validate.validators.type.options; |
| 8 | + delete validate.validators.type.types.custom; |
| 9 | + }); |
| 10 | + |
| 11 | + it("allows empty values", function() { |
| 12 | + expect(type(null, "string", "foo", {})).not.toBeDefined(); |
| 13 | + expect(type(undefined, "string", "foo", {})).not.toBeDefined(); |
| 14 | + }); |
| 15 | + |
| 16 | + it("allows the correct type", function() { |
| 17 | + expect(type("", {type: "string"}, "foo", {})).not.toBeDefined(); |
| 18 | + expect(type({}, {type: "object"}, "foo", {})).not.toBeDefined(); |
| 19 | + expect(type([], {type: "array"}, "foo", {})).not.toBeDefined(); |
| 20 | + expect(type(1, {type: "number"}, "foo", {})).not.toBeDefined(); |
| 21 | + expect(type(1.1, {type: "number"}, "foo", {})).not.toBeDefined(); |
| 22 | + expect(type(1, {type: "integer"}, "foo", {})).not.toBeDefined(); |
| 23 | + expect(type(true, {type: "boolean"}, "foo", {})).not.toBeDefined(); |
| 24 | + expect(type(new Date(), {type: "date"}, "foo", {})).not.toBeDefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("doesn't allow the incorrect type", function() { |
| 28 | + expect(type(new Date(), {type: "string"}, "foo", {})).toBeDefined(); |
| 29 | + expect(type("", {type: "object"}, "foo", {})).toBeDefined(); |
| 30 | + expect(type([], {type: "object"}, "foo", {})).toBeDefined(); |
| 31 | + expect(type({}, {type: "array"}, "foo", {})).toBeDefined(); |
| 32 | + expect(type([], {type: "number"}, "foo", {})).toBeDefined(); |
| 33 | + expect(type(1.1, {type: "integer"}, "foo", {})).toBeDefined(); |
| 34 | + expect(type(1, {type: "boolean"}, "foo", {})).toBeDefined(); |
| 35 | + expect(type(true, {type: "date"}, "foo", {})).toBeDefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("has a nice default message", function() { |
| 39 | + expect(type(new Date(), {type: "string"}, "foo", {})).toBe("must be of type string"); |
| 40 | + expect(type("", {type: "object"}, "foo", {})).toBe("must be of type object"); |
| 41 | + expect(type({}, {type: "array"}, "foo", {})).toBe("must be of type array"); |
| 42 | + expect(type([], {type: "number"}, "foo", {})).toBe("must be of type number"); |
| 43 | + expect(type(1.1, {type: "integer"}, "foo", {})).toBe("must be of type integer"); |
| 44 | + expect(type(1, {type: "boolean"}, "foo", {})).toBe("must be of type boolean"); |
| 45 | + expect(type(true, {type: "date"}, "foo", {})).toBe("must be of type date"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("allows you to customize the error message", function() { |
| 49 | + validate.validators.type.message = "some message %{attribute}"; |
| 50 | + expect(type("", {type: "object"}, "foo", {})).toBe("some message foo"); |
| 51 | + var options = {type: "object", message: "some other message %{attribute}"}; |
| 52 | + expect(type("", options, "foo", {})).toBe("some other message foo"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("allows functions as messages", function() { |
| 56 | + var message = function() { return "foo"; }; |
| 57 | + var options = {type: "object", message: message}; |
| 58 | + expect(type("", options, "foo", {})).toBe("foo"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("allows custom checks", function() { |
| 62 | + var globalOptions = {"globalOption": "globalValue"}; |
| 63 | + var attributes = {"attr": "value"}; |
| 64 | + var options = {type: "custom"}; |
| 65 | + var ret = false; |
| 66 | + validate.validators.type.types.custom = function(value, opts, attr, attrs, gopts) { |
| 67 | + expect(value).toBe("value"); |
| 68 | + expect(opts).toEqual(options); |
| 69 | + expect(attr).toBe("foo"); |
| 70 | + expect(attrs).toBe(attributes); |
| 71 | + expect(gopts).toBe(globalOptions); |
| 72 | + return ret; |
| 73 | + }; |
| 74 | + expect(type("value", options, "foo", attributes, globalOptions)).toEqual("must be of type custom"); |
| 75 | + ret = true; |
| 76 | + expect(type("value", options, "foo", attributes, globalOptions)).not.toBeDefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("allows inline checks", function() { |
| 80 | + var globalOptions = {"globalOption": "globalValue"}; |
| 81 | + var attributes = {"attr": "value"}; |
| 82 | + var value = "value"; |
| 83 | + var options = { |
| 84 | + type: function(v, opts, attr, attrs, gopts) { |
| 85 | + expect(v).toBe(value); |
| 86 | + expect(opts).toEqual(options); |
| 87 | + expect(attr).toBe("foo"); |
| 88 | + expect(attrs).toBe(attributes); |
| 89 | + expect(gopts).toBe(globalOptions); |
| 90 | + return value === "other"; |
| 91 | + } |
| 92 | + }; |
| 93 | + expect(type(value, options, "foo", attributes, globalOptions)).toEqual("must be of the correct type"); |
| 94 | + value = "other"; |
| 95 | + expect(type(value, options, "foo", attributes, globalOptions)).not.toBeDefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("allows custom messages per check", function() { |
| 99 | + var globalOptions = {"globalOption": "globalValue"}; |
| 100 | + var attributes = {"attr": "value"}; |
| 101 | + var options = {type: "custom"}; |
| 102 | + validate.validators.type.types.custom = function() { return false; }; |
| 103 | + validate.validators.type.messages.custom = "my custom message"; |
| 104 | + expect(type("value", options, "foo", globalOptions)).toEqual("my custom message"); |
| 105 | + validate.validators.type.messages.custom = function(value, opts, attr, attrs, gopts) { |
| 106 | + expect(value).toBe("value"); |
| 107 | + expect(opts).toEqual(options); |
| 108 | + expect(attr).toBe("foo"); |
| 109 | + expect(attrs).toBe(attributes); |
| 110 | + expect(gopts).toBe(globalOptions); |
| 111 | + return "my other custom message"; |
| 112 | + }; |
| 113 | + expect(type("value", options, "foo", attributes, globalOptions)).toEqual("my other custom message"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("throws if the type isn't valid", function() { |
| 117 | + expect(function() { type("", {}, "foo", {}); }).toThrow(); |
| 118 | + expect(function() { type("", "invalid", "foo", {}); }).toThrow(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments