narrows
Version:
Super lean and simple object validation with TypeScript support.
209 lines • 8.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("./index");
describe("boolean", function () {
it("should return true for booleans", function () {
expect(index_1.boolean(true)).toBe(true);
});
it("should return false for other types", function () {
expect(index_1.boolean(1)).toBe(false);
expect(index_1.boolean("test")).toBe(false);
expect(index_1.boolean(Symbol())).toBe(false);
expect(index_1.boolean(null)).toBe(false);
expect(index_1.boolean(undefined)).toBe(false);
expect(index_1.boolean({})).toBe(false);
});
});
describe("number", function () {
it("should return true for numbers", function () {
expect(index_1.number(0)).toBe(true);
});
it("should return false for other types", function () {
expect(index_1.number(true)).toBe(false);
expect(index_1.number("test")).toBe(false);
expect(index_1.number(Symbol())).toBe(false);
expect(index_1.number(null)).toBe(false);
expect(index_1.number(undefined)).toBe(false);
expect(index_1.number({})).toBe(false);
});
});
describe("string", function () {
it("should return true for strings", function () {
expect(index_1.string("test")).toBe(true);
});
it("should return false for other types", function () {
expect(index_1.string(true)).toBe(false);
expect(index_1.string(1)).toBe(false);
expect(index_1.string(Symbol())).toBe(false);
expect(index_1.string(null)).toBe(false);
expect(index_1.string(undefined)).toBe(false);
expect(index_1.string({})).toBe(false);
});
});
describe("empty", function () {
it("should return true for undefined", function () {
expect(index_1.empty(undefined)).toBe(true);
});
it("should return false for other types", function () {
expect(index_1.empty(null)).toBe(false);
expect(index_1.empty("test")).toBe(false);
expect(index_1.empty(true)).toBe(false);
expect(index_1.empty(1)).toBe(false);
expect(index_1.empty(Symbol())).toBe(false);
expect(index_1.empty({})).toBe(false);
});
});
describe("nil", function () {
it("should return true for null", function () {
expect(index_1.nil(null)).toBe(true);
});
it("should return false for other types", function () {
expect(index_1.nil(undefined)).toBe(false);
expect(index_1.nil("test")).toBe(false);
expect(index_1.nil(true)).toBe(false);
expect(index_1.nil(1)).toBe(false);
expect(index_1.nil(Symbol())).toBe(false);
expect(index_1.nil({})).toBe(false);
});
});
describe("literal", function () {
it("should return true for the same literal", function () {
var validate = index_1.literal(1);
expect(validate(1)).toBe(true);
});
it("should return false for other values", function () {
var validate = index_1.literal(1);
expect(validate(2)).toBe(false);
expect(validate("test")).toBe(false);
expect(validate(true)).toBe(false);
expect(validate(Symbol())).toBe(false);
expect(validate({})).toBe(false);
});
});
describe("object", function () {
it("should return true if all properties match the given validator", function () {
var validate = index_1.object(index_1.number);
expect(validate({
a: 1,
b: 2
})).toBe(true);
});
it("should return false if any property doesn't match the given validator", function () {
var validate = index_1.object(index_1.number);
expect(validate({
a: 1,
b: "2"
})).toBe(false);
});
});
describe("array", function () {
it("should return true if all elements match the given validator", function () {
var validate = index_1.array(index_1.string);
expect(validate(["one", "two"])).toBe(true);
});
it("should return false if any element doesn't match the given validator", function () {
var validate = index_1.array(index_1.string);
expect(validate(["one", 2])).toBe(false);
});
it("should return false if not passed an array", function () {
var validate = index_1.array(index_1.string);
expect(validate({ 0: "one", 1: "two" })).toBe(false);
});
});
describe("instance", function () {
it("should return true if the value is an instance of the given class", function () {
var validate = index_1.instance(Date);
expect(validate(new Date())).toBe(true);
});
it("should return false if the value is not an instance of the given class", function () {
var validate = index_1.instance(index_1.string);
expect(validate({})).toBe(false);
});
});
describe("record", function () {
it("should return true if all properties match the schema", function () {
var validate = index_1.record({
a: index_1.string,
b: index_1.number
});
expect(validate({ a: "one", b: 2 })).toBe(true);
});
it("should return true even if extra properties exist in the object", function () {
var validate = index_1.record({
a: index_1.string,
b: index_1.number
});
expect(validate({ a: "one", b: 2, c: true })).toBe(true);
});
it("should return false if any property doesn't match the schema", function () {
var validate = index_1.record({
a: index_1.string,
b: index_1.number
});
expect(validate({ a: "one", b: "two" })).toBe(false);
});
});
describe("tuple", function () {
it("should return true if all elements match the schema", function () {
var validate = index_1.tuple(index_1.string, index_1.number);
expect(validate(["one", 2])).toBe(true);
});
it("should return true even if extra elements exist in the object", function () {
var validate = index_1.tuple(index_1.string, index_1.number);
expect(validate(["one", 2, true])).toBe(true);
});
it("should return false if any element doesn't match the schema", function () {
var validate = index_1.tuple(index_1.string, index_1.number);
expect(validate(["one", "two"])).toBe(false);
});
});
describe("optional", function () {
it("should return true if the value matches the given validator", function () {
var validate = index_1.optional(index_1.number);
expect(validate(1)).toBe(true);
});
it("should return true if the value is undefined", function () {
var validate = index_1.optional(index_1.number);
expect(validate(undefined)).toBe(true);
});
it("should return false if the value doesn't match the given validator", function () {
var validate = index_1.optional(index_1.number);
expect(validate("test")).toBe(false);
});
});
describe("nullable", function () {
it("should return true if the value matches the given validator", function () {
var validate = index_1.nullable(index_1.number);
expect(validate(1)).toBe(true);
});
it("should return true if the value is null", function () {
var validate = index_1.nullable(index_1.number);
expect(validate(null)).toBe(true);
});
it("should return false if the value doesn't match the given validator", function () {
var validate = index_1.nullable(index_1.number);
expect(validate("test")).toBe(false);
});
});
describe("any", function () {
it("should return true if the value matches any given validator", function () {
var validate = index_1.any(index_1.number, index_1.string);
expect(validate(1)).toBe(true);
expect(validate("two")).toBe(true);
});
it("should return false if the value doesn't match any given validator", function () {
var validate = index_1.any(index_1.number, index_1.string);
expect(validate(true)).toBe(false);
});
});
describe("all", function () {
it("should return true if the value matches all given validators", function () {
var validate = index_1.all(index_1.record({ a: index_1.string }), index_1.record({ b: index_1.number }));
expect(validate({ a: "one", b: 2 })).toBe(true);
});
it("should return false if the value doesn't match all given validators", function () {
var validate = index_1.all(index_1.record({ a: index_1.string }), index_1.record({ b: index_1.number }));
expect(validate({ a: "one", b: "two" })).toBe(false);
});
});
//# sourceMappingURL=index.test.js.map