Module
the validation tool nobody asked for | hoyams = hold on you are missing something aka. zucker
Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250import { ArrayRule, ObjectRule, Rule, Ruleset, validate } from "./mod.ts";import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts";
// test RuleDeno.test("test Rule valid", () => { assertEquals( new Rule((v) => typeof v === "string" || "no string").validate("lol"), "lol", );});
Deno.test("test Rule invalid", () => { assertEquals( (() => { try { new Rule((v) => typeof v === "string" || "no string").validate(123); } catch (e) { return e.map; } })(), "no string", );});
// test RulesetDeno.test("test Ruleset valid", () => { assertEquals( new Ruleset([ (v) => typeof v === "string" || "no string", (v) => typeof v === "string" && v.length > 2 || "too short", ]).validate("lol"), "lol", );});
Deno.test("test Ruleset invalid", () => { assertEquals( (() => { try { new Ruleset([ (v) => typeof v === "string" || "no string", (v) => typeof v === "string" && v.length > 2 || "too short", ]).validate(123); } catch (e) { return e.map; } })(), ["no string", "too short"], );});
// test ArrayRuleDeno.test("test ArrayRule valid", () => { assertEquals( new ArrayRule( new Rule((v) => typeof v === "string" || "no string"), ).validate(["hai", "lol"]), ["hai", "lol"], );});
Deno.test("test ArrayRule is not array", () => { assertEquals( (() => { try { new ArrayRule( new Rule((v) => typeof v === "string" || "no string"), ).validate("hai"); } catch (e) { return e.message; } })(), "is not an array", );});
Deno.test("test ArrayRule invalid ", () => { assertEquals( (() => { try { new ArrayRule( new Rule((v) => typeof v === "string" || "no string"), ).validate(["hai", 123]); } catch (e) { return e.map; } })(), [null, "no string"], );});
// test ObjectRuleDeno.test("test ObjectRule valid", () => { assertEquals( new ObjectRule({ name: new Rule((v) => typeof v === "string" || "no string"), }).validate({ name: "lol" }), { name: "lol" }, );});
Deno.test("test ObjectRule is not object", () => { assertEquals( (() => { try { return new ObjectRule({ name: new Rule((v) => typeof v === "string" || "no string"), }).validate("hai"); } catch (e) { return e.message; } })(), "is not an object", );});
Deno.test("test ObjectRule invalid", () => { assertEquals( (() => { try { return new ObjectRule({ name: new Rule((v) => typeof v === "string" || "no string"), }).validate({ name: 123 }); } catch (e) { return e.map; } })(), { name: "no string" }, );});
// test Allconst testRule = { id: new Rule((v) => typeof v === "number" || "not a number"), user: { name: new Rule((v) => typeof v === "string" || "no string"), tags: new ArrayRule( new Ruleset([ (v) => typeof v === "string" || "no string", (v) => typeof v === "string" && v.length > 2 || "too short", ]), ), email: new Rule((v) => typeof v === "string" && !!v.match(/^\w+@\w+\.\w+$/) || "no valid email" ), },};
const testValid = { id: 123, user: { name: "lol", tags: [ "lol", "hai", ], email: "lol@foo.bar", },};
const testInvalid = { id: "lol", user: { name: 123, tags: [ "lol", 123, ], email: "lol@foobar", },};
const testObjectMissing = { id: "lol",};
const testArrayMissing = { user: { name: "lol", email: "lol@foo.bar", },};
Deno.test("test All valid", () => { assertEquals( validate(testValid, testRule), testValid, );});
Deno.test("test All invalid", () => { assertEquals( (() => { try { validate(testInvalid, testRule); } catch (e) { return e.map; } })(), { id: "not a number", user: { name: "no string", tags: [ null, [ "no string", "too short", ], ], email: "no valid email", }, }, );});
Deno.test("test All object missing", () => { assertEquals( (() => { try { validate(testObjectMissing, testRule); } catch (e) { return e.map; } })(), { id: "not a number", user: "is not an object", }, );});
Deno.test("test All array missing", () => { assertEquals( (() => { try { validate(testArrayMissing, testRule); } catch (e) { return e.map; } })(), { id: "not a number", user: { tags: "is not an array", }, }, );});