Last active
August 29, 2015 14:22
-
-
Save lorenzoongithub/ca13ca60b9400b9c1793 to your computer and use it in GitHub Desktop.
validator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// validator.js - String validation and sanitization | |
// https://github.com/chriso/validator.js | |
// | |
// loosely based on https://github.com/chriso/validator.js/blob/master/test/validators.js | |
// | |
load('https://cdnjs.cloudflare.com/ajax/libs/validator/3.12.0/validator.js'); | |
// | |
//should validate email addresses | |
// | |
if (validator.isEmail("foo@bar.com") == false) throw ""; | |
if (validator.isEmail("x@x.au") == false) throw ""; | |
if (validator.isEmail("foo@bar.com.au") == false) throw ""; | |
if (validator.isEmail("foo+bar@bar.com") == false) throw ""; | |
if (validator.isEmail("hans.m?ller@test.com") == false) throw ""; | |
if (validator.isEmail("test+ext@gmail.com") == false) throw ""; | |
if (validator.isEmail("some.name.midd.leNa.me.+extension@GoogleMail.com") == false) throw ""; | |
if (validator.isEmail("invalidemail@")) throw ""; | |
if (validator.isEmail("invalid.com")) throw ""; | |
if (validator.isEmail("@invalid.com")) throw ""; | |
// | |
//should validate email addresses without UTF8 characters in local part | |
// | |
if (validator.isEmail("foo@bar.com") == false) throw ""; | |
if (validator.isEmail("x@x.au") == false) throw ""; | |
if (validator.isEmail("foo@bar.com.au") == false) throw ""; | |
if (validator.isEmail("foo+bar@bar.com") == false) throw ""; | |
if (validator.isEmail("test+ext@gmail.com") == false) throw ""; | |
if (validator.isEmail("some.name.midd.leNa.me.+extension@GoogleMail.com") == false) throw ""; | |
if (validator.isEmail("invalidemail@")) throw ""; | |
if (validator.isEmail("invalid.com")) throw ""; | |
if (validator.isEmail("@invalid.com")) throw ""; | |
// | |
//should validate email addresses with display names | |
// | |
if (validator.isEmail("foo@bar.com") == false) throw ""; | |
if (validator.isEmail("x@x.au") == false) throw ""; | |
if (validator.isEmail("foo@bar.com.au") == false) throw ""; | |
if (validator.isEmail("foo+bar@bar.com") == false) throw ""; | |
if (validator.isEmail("hans.m?ller@test.com") == false) throw ""; | |
if (validator.isEmail("test+ext@gmail.com") == false) throw ""; | |
if (validator.isEmail("some.name.midd.leNa.me.+extension@GoogleMail.com") == false) throw ""; | |
if (validator.isEmail("invalidemail@")) throw ""; | |
if (validator.isEmail("invalid.com")) throw ""; 2 | |
if (validator.isEmail("@invalid.com")) throw ""; | |
if (validator.isEmail("Some Name <invalidemail@>")) throw ""; | |
if (validator.isEmail("Some Name <invalid.com>")) throw ""; | |
if (validator.isEmail("Some Name <@invalid.com>")) throw ""; | |
if (validator.isEmail("Some Name <foo@bar.com.>")) throw ""; | |
if (validator.isEmail("Some Name <foo@bar.co.uk.>")) throw ""; | |
if (validator.isEmail("Some Name foo@bar.co.uk.>")) throw ""; | |
if (validator.isEmail("Some Name <foo@bar.co.uk.")) throw ""; | |
if (validator.isEmail("Some Name < foo@bar.co.uk >")) throw ""; | |
if (validator.isEmail("Name foo@bar.co.uk")) throw ""; | |
// | |
//should validate URLs | |
// | |
if (validator.isURL("foobar.com") == false) throw ""; | |
if (validator.isURL("www.foobar.com") == false) throw ""; | |
if (validator.isURL("foobar.com/") == false) throw ""; | |
if (validator.isURL("valid.au") == false) throw ""; | |
if (validator.isURL("http://www.foobar.com/") == false) throw ""; | |
if (validator.isURL("http://www.foobar.com:23/") == false) throw ""; | |
if (validator.isURL("http://www.foobar.com:65535/") == false) throw ""; | |
if (validator.isURL("http://www.foobar.com:5/") == false) throw ""; | |
if (validator.isURL("https://www.foobar.com/") == false) throw ""; | |
if (validator.isURL("ftp://www.foobar.com/") == false) throw ""; | |
if (validator.isURL("http://www.foobar.com/~foobar") == false) throw ""; | |
if (validator.isURL("http://user:pass@www.foobar.com/") == false) throw ""; | |
if (validator.isURL("http://user:@www.foobar.com/") == false) throw ""; | |
if (validator.isURL("http://127.0.0.1/") == false) throw ""; | |
if (validator.isURL("http://10.0.0.0/") == false) throw ""; | |
if (validator.isURL("http://189.123.14.13/") == false) throw ""; | |
if (validator.isURL("http://duckduckgo.com/?q=%2F") == false) throw ""; | |
if (validator.isURL("http://foobar.com/t$-_.+!*'(),") == false) throw ""; | |
if (validator.isURL("http://localhost:3000/") == false) throw ""; | |
if (validator.isURL("http://foobar.com/?foo=bar#baz=qux") == false) throw ""; | |
if (validator.isURL("http://foobar.com?foo=bar") == false) throw ""; | |
if (validator.isURL("http://foobar.com#baz=qux") == false) throw ""; | |
if (validator.isURL("http://www.xn--froschgrn-x9a.net/") == false) throw ""; | |
if (validator.isURL("http://xn--froschgrn-x9a.com/") == false) throw ""; | |
if (validator.isURL("http://foo--bar.com") == false) throw ""; | |
if (validator.isURL("http://høyfjellet.no") == false) throw ""; | |
if (validator.isURL("http://xn--j1aac5a4g.xn--j1amh") == false) throw ""; | |
if (validator.isURL("http://?????.???") == false) throw ""; | |
if (validator.isURL("xyz://foobar.com")) throw ""; | |
if (validator.isURL("invalid/")) throw ""; | |
if (validator.isURL("invalid.x")) throw ""; | |
if (validator.isURL("invalid.")) throw ""; | |
if (validator.isURL(".com")) throw ""; | |
if (validator.isURL("http://com/")) throw ""; | |
if (validator.isURL("http://300.0.0.1/")) throw ""; | |
if (validator.isURL("mailto:foo@bar.com")) throw ""; | |
if (validator.isURL("rtmp://foobar.com")) throw ""; | |
if (validator.isURL("http://www.xn--.com/")) throw ""; | |
if (validator.isURL("http://xn--.com/")) throw ""; | |
if (validator.isURL("http://www.foobar.com:0/")) throw ""; | |
if (validator.isURL("http://www.foobar.com:70000/")) throw ""; | |
if (validator.isURL("http://www.foobar.com:99999/")) throw ""; | |
if (validator.isURL("http://www.-foobar.com/")) throw ""; | |
if (validator.isURL("http://www.foobar-.com/")) throw ""; | |
if (validator.isURL("http://www.foo---bar.com/")) throw ""; | |
if (validator.isURL("http://foobar/# lol")) throw ""; | |
if (validator.isURL("http://foobar/? lol")) throw ""; | |
if (validator.isURL("http://foobar/ lol/")) throw ""; | |
if (validator.isURL("http://lol @foobar.com/")) throw ""; | |
if (validator.isURL("http://lol:lol @foobar.com/")) throw ""; | |
if (validator.isURL("http://lol: @foobar.com/")) throw ""; | |
if (validator.isURL("http://www.foo_bar.com/")) throw ""; | |
if (validator.isURL("http://www.foobar.com/\t")) throw ""; | |
if (validator.isURL("http://\n@www.foobar.com/")) throw ""; | |
if (validator.isURL("")) throw ""; | |
if (validator.isURL("http://*.foo.com")) throw ""; | |
if (validator.isURL("*.foo.com")) throw ""; | |
if (validator.isURL("!.foo.com")) throw ""; | |
if (validator.isURL("http://example.com.")) throw ""; | |
if (validator.isURL("http://localhost:61500this is an invalid url!!!!")) throw ""; | |
if (validator.isURL("////foobar.com")) throw ""; | |
if (validator.isURL("http:////foobar.com")) throw ""; | |
// | |
//should validate URLs with custom protocols | |
// | |
if (validator.isURL("rtmp://foobar.com") == false) throw ""; | |
// | |
//should validate URLs with underscores | |
// | |
if (validator.isURL("http://foo_bar.com") == false) throw ""; | |
if (validator.isURL("http://pr.example_com.294.example.com/") == false) throw ""; | |
if (validator.isURL("http://foo__bar.com")) throw ""; | |
// | |
//should validate URLs that do not have a TLD | |
// | |
if (validator.isURL("http://foobar.com/") == false) throw ""; | |
if (validator.isURL("http://foobar/") == false) throw ""; | |
if (validator.isURL("foobar/") == false) throw ""; | |
if (validator.isURL("foobar") == false) throw ""; | |
// | |
//should validate URLs with a trailing dot option | |
// | |
if (validator.isURL("http://example.com.") == false) throw ""; | |
if (validator.isURL("foobar.") == false) throw ""; | |
// | |
//should validate protocol relative URLs | |
// | |
if (validator.isURL("//foobar.com") == false) throw ""; | |
if (validator.isURL("http://foobar.com") == false) throw ""; | |
if (validator.isURL("foobar.com") == false) throw ""; | |
if (validator.isURL("://foobar.com")) throw ""; | |
if (validator.isURL("/foobar.com")) throw ""; | |
if (validator.isURL("////foobar.com")) throw ""; | |
if (validator.isURL("http:////foobar.com")) throw ""; | |
// | |
//should not validate protocol relative URLs when require protocol is true | |
// | |
if (validator.isURL("http://foobar.com") == false) throw ""; | |
if (validator.isURL("//foobar.com")) throw ""; | |
if (validator.isURL("://foobar.com")) throw ""; | |
if (validator.isURL("/foobar.com")) throw ""; | |
// | |
//should let users specify whether URLs require a protocol | |
// | |
if (validator.isURL("http://foobar.com/") == false) throw ""; | |
if (validator.isURL("http://localhost/") == false) throw ""; | |
if (validator.isURL("foobar")) throw ""; | |
// | |
//should let users specify a host whitelist | |
// | |
if (validator.isURL("http://bar.com/") == false) throw ""; | |
if (validator.isURL("http://foo.com/") == false) throw ""; | |
// | |
//should let users specify a host blacklist | |
// | |
if (validator.isURL("http://foobar.com") == false) throw ""; | |
if (validator.isURL("http://foo.bar.com/") == false) throw ""; | |
if (validator.isURL("http://qux.com") == false) throw ""; | |
// | |
//should validate IP addresses | |
// | |
if (validator.isIP("127.0.0.1") == false) throw ""; | |
if (validator.isIP("0.0.0.0") == false) throw ""; | |
if (validator.isIP("255.255.255.255") == false) throw ""; | |
if (validator.isIP("1.2.3.4") == false) throw ""; | |
if (validator.isIP("::1") == false) throw ""; | |
if (validator.isIP("2001:db8:0000:1:1:1:1:1") == false) throw ""; | |
if (validator.isIP("2001:41d0:2:a141::1") == false) throw ""; | |
if (validator.isIP("::0000") == false) throw ""; | |
if (validator.isIP("1111:1:1:1:1:1:1:1") == false) throw ""; | |
if (validator.isIP("fe80::a6db:30ff:fe98:e946") == false) throw ""; | |
if (validator.isIP("::") == false) throw ""; | |
if (validator.isIP("abc")) throw ""; | |
if (validator.isIP("256.0.0.0")) throw ""; | |
if (validator.isIP("0.0.0.256")) throw ""; | |
if (validator.isIP("banana::")) throw ""; | |
if (validator.isIP("1:")) throw ""; | |
if (validator.isIP(":1")) throw ""; | |
if (validator.isIP(":1:1:1::2")) throw ""; | |
if (validator.isIP("1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1")) throw ""; | |
if (validator.isIP("11111:1:1:1:1:1:1:1")) throw ""; | |
if (validator.isIP("127.0.0.1") == false) throw ""; | |
if (validator.isIP("0.0.0.0") == false) throw ""; | |
if (validator.isIP("255.255.255.255") == false) throw ""; | |
if (validator.isIP("1.2.3.4") == false) throw ""; | |
if (validator.isIP("::1") == false) throw ""; | |
if (validator.isIP("2001:db8:0000:1:1:1:1:1") == false) throw ""; | |
// | |
//should validate FQDN | |
// | |
// | |
//should validate FQDN with trailing dot option | |
// | |
// | |
//should validate alpha strings | |
// | |
if (validator.isAlpha("abc") == false) throw ""; | |
if (validator.isAlpha("ABC") == false) throw ""; | |
if (validator.isAlpha("FoObar") == false) throw ""; | |
if (validator.isAlpha("abc1")) throw ""; | |
if (validator.isAlpha(" foo ")) throw ""; | |
if (validator.isAlpha("")) throw ""; | |
// | |
//should validate alphanumeric strings | |
// | |
if (validator.isAlphanumeric("abc123") == false) throw ""; | |
if (validator.isAlphanumeric("ABC11") == false) throw ""; | |
if (validator.isAlphanumeric("abc ")) throw ""; | |
if (validator.isAlphanumeric("foo!!")) throw ""; | |
// | |
//should validate numeric strings | |
// | |
if (validator.isNumeric("123") == false) throw ""; | |
if (validator.isNumeric("00123") == false) throw ""; | |
if (validator.isNumeric("-00123") == false) throw ""; | |
if (validator.isNumeric("0") == false) throw ""; | |
if (validator.isNumeric("-0") == false) throw ""; | |
if (validator.isNumeric("123.123")) throw ""; | |
if (validator.isNumeric(" ")) throw ""; | |
if (validator.isNumeric(".")) throw ""; | |
// | |
//should validate lowercase strings | |
// | |
if (validator.isLowercase("abc") == false) throw ""; | |
if (validator.isLowercase("abc123") == false) throw ""; | |
if (validator.isLowercase("this is lowercase.") == false) throw ""; | |
if (validator.isLowercase("tr?s ?ber") == false) throw ""; | |
if (validator.isLowercase("fooBar")) throw ""; | |
if (validator.isLowercase("123A")) throw ""; | |
// | |
//should validate uppercase strings | |
// | |
if (validator.isUppercase("ABC") == false) throw ""; | |
if (validator.isUppercase("ABC123") == false) throw ""; | |
if (validator.isUppercase("ALL CAPS IS FUN.") == false) throw ""; | |
if (validator.isUppercase(" .") == false) throw ""; | |
if (validator.isUppercase("fooBar")) throw ""; | |
if (validator.isUppercase("123abc")) throw ""; | |
// | |
//should validate integers | |
// | |
if (validator.isInt("13") == false) throw ""; | |
if (validator.isInt("123") == false) throw ""; | |
if (validator.isInt("0") == false) throw ""; | |
if (validator.isInt("123") == false) throw ""; | |
if (validator.isInt("-0") == false) throw ""; | |
if (validator.isInt("01")) throw ""; | |
if (validator.isInt("-01")) throw ""; | |
if (validator.isInt("000")) throw ""; | |
if (validator.isInt("100e10")) throw ""; | |
if (validator.isInt("123.123")) throw ""; | |
if (validator.isInt(" ")) throw ""; | |
if (validator.isInt("")) throw ""; | |
if (validator.isInt(15) == false) throw ""; | |
if (validator.isInt(80) == false) throw ""; | |
if (validator.isInt(99) == false) throw ""; | |
if (validator.isInt(3.2)) throw ""; | |
if (validator.isInt("a")) throw ""; | |
if (validator.isInt(15) == false) throw ""; | |
if (validator.isInt(11) == false) throw ""; | |
if (validator.isInt(13) == false) throw ""; | |
if (validator.isInt(3.2)) throw ""; | |
if (validator.isInt("a")) throw ""; | |
// | |
//should validate floats | |
// | |
if (validator.isFloat("123") == false) throw ""; | |
if (validator.isFloat("123.") == false) throw ""; | |
if (validator.isFloat("123.123") == false) throw ""; | |
if (validator.isFloat("-123.123") == false) throw ""; | |
if (validator.isFloat("-0.123") == false) throw ""; | |
if (validator.isFloat("0.123") == false) throw ""; | |
if (validator.isFloat(".0") == false) throw ""; | |
if (validator.isFloat("01.123") == false) throw ""; | |
if (validator.isFloat("-0.22250738585072011e-307") == false) throw ""; | |
if (validator.isFloat("-.123")) throw ""; | |
if (validator.isFloat(" ")) throw ""; | |
if (validator.isFloat("")) throw ""; | |
if (validator.isFloat("foo")) throw ""; | |
if (validator.isFloat(3.888) == false) throw ""; | |
if (validator.isFloat(3.92) == false) throw ""; | |
if (validator.isFloat(4.5) == false) throw ""; | |
if (validator.isFloat(50) == false) throw ""; | |
if (validator.isFloat(3.7) == false) throw ""; | |
if (validator.isFloat(3.71) == false) throw ""; | |
if (validator.isFloat("a")) throw ""; | |
if (validator.isFloat(0.1) == false) throw ""; | |
if (validator.isFloat(1) == false) throw ""; | |
if (validator.isFloat(0.15) == false) throw ""; | |
if (validator.isFloat(0.33) == false) throw ""; | |
if (validator.isFloat(0.57) == false) throw ""; | |
if (validator.isFloat(0.7) == false) throw ""; | |
if (validator.isFloat("a")) throw ""; | |
// | |
//should validate hexadecimal strings | |
// | |
if (validator.isHexadecimal("deadBEEF") == false) throw ""; | |
if (validator.isHexadecimal("ff0044") == false) throw ""; | |
if (validator.isHexadecimal("abcdefg")) throw ""; | |
if (validator.isHexadecimal("")) throw ""; | |
if (validator.isHexadecimal("..")) throw ""; | |
// | |
//should validate hexadecimal color strings | |
// | |
if (validator.isHexColor("#ff0034") == false) throw ""; | |
if (validator.isHexColor("#CCCCCC") == false) throw ""; | |
if (validator.isHexColor("fff") == false) throw ""; | |
if (validator.isHexColor("#f00") == false) throw ""; | |
if (validator.isHexColor("#ff")) throw ""; | |
if (validator.isHexColor("fff0")) throw ""; | |
if (validator.isHexColor("#ff12FG")) throw ""; | |
// | |
//should validate null strings | |
// | |
if (validator.isNull("") == false) throw ""; | |
if (validator.isNull(null) == false) throw ""; | |
if (validator.isNull([]) == false) throw ""; | |
if (validator.isNull(undefined) == false) throw ""; | |
if (validator.isNull(null) == false) throw ""; | |
if (validator.isNull(" ")) throw ""; | |
if (validator.isNull("foo")) throw ""; | |
// | |
//should validate strings against an expected value | |
// | |
if (validator.equals("Abc")) throw ""; | |
if (validator.equals("123")) throw ""; | |
// | |
//should validate strings contain another string | |
// | |
if (validator.contains("foo") == false) throw ""; | |
if (validator.contains("foobar") == false) throw ""; | |
if (validator.contains("bazfoo") == false) throw ""; | |
// | |
//should validate strings against a pattern | |
// | |
if (validator.matches("abc") == false) throw ""; | |
if (validator.matches("abcdef") == false) throw ""; | |
if (validator.matches("123abc") == false) throw ""; | |
if (validator.matches("abc") == false) throw ""; | |
if (validator.matches("abcdef") == false) throw ""; | |
if (validator.matches("123abc") == false) throw ""; | |
if (validator.matches("abc") == false) throw ""; | |
if (validator.matches("abcdef") == false) throw ""; | |
if (validator.matches("123abc") == false) throw ""; | |
if (validator.matches("AbC") == false) throw ""; | |
// | |
//should validate strings by length | |
// | |
if (validator.isLength("")) throw ""; | |
if (validator.isLength("a")) throw ""; | |
if (validator.isLength("")) throw ""; | |
if (validator.isLength("a")) throw ""; | |
if (validator.isLength("abcd")) throw ""; | |
if (validator.isLength("")) throw ""; | |
if (validator.isLength("??")) throw ""; | |
if (validator.isLength("????")) throw ""; | |
// | |
//should validate strings by byte length | |
// | |
// | |
//should validate UUIDs | |
// | |
if (validator.isUUID("A987FBC9-4BED-3078-CF07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("A987FBC9-4BED-4078-8F07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("A987FBC9-4BED-5078-AF07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("")) throw ""; | |
if (validator.isUUID("xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3")) throw ""; | |
if (validator.isUUID("A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx")) throw ""; | |
if (validator.isUUID("A987FBC94BED3078CF079141BA07C9F3")) throw ""; | |
if (validator.isUUID("934859")) throw ""; | |
if (validator.isUUID("987FBC9-4BED-3078-CF07A-9141BA07C9F3")) throw ""; | |
if (validator.isUUID("AAAAAAAA-1111-1111-AAAG-111111111111")) throw ""; | |
if (validator.isUUID("A987FBC9-4BED-3078-CF07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("")) throw ""; | |
if (validator.isUUID("xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3")) throw ""; | |
if (validator.isUUID("934859")) throw ""; | |
if (validator.isUUID("AAAAAAAA-1111-1111-AAAG-111111111111")) throw ""; | |
if (validator.isUUID("713ae7e3-cb32-45f9-adcb-7c4fa86b90c1") == false) throw ""; | |
if (validator.isUUID("625e63f3-58f5-40b7-83a1-a72ad31acffb") == false) throw ""; | |
if (validator.isUUID("57b73598-8764-4ad0-a76a-679bb6640eb1") == false) throw ""; | |
if (validator.isUUID("9c858901-8a57-4791-81fe-4c455b099bc9") == false) throw ""; | |
if (validator.isUUID("")) throw ""; | |
if (validator.isUUID("xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3")) throw ""; | |
if (validator.isUUID("934859")) throw ""; | |
if (validator.isUUID("AAAAAAAA-1111-1111-AAAG-111111111111")) throw ""; | |
if (validator.isUUID("987FBC97-4BED-5078-AF07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("987FBC97-4BED-5078-BF07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("987FBC97-4BED-5078-8F07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("987FBC97-4BED-5078-9F07-9141BA07C9F3") == false) throw ""; | |
if (validator.isUUID("")) throw ""; | |
if (validator.isUUID("xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3")) throw ""; | |
if (validator.isUUID("934859")) throw ""; | |
if (validator.isUUID("AAAAAAAA-1111-1111-AAAG-111111111111")) throw ""; | |
// | |
//should validate a string that is in another string or array | |
// | |
if (validator.isIn("foobarbaz")) throw ""; | |
if (validator.isIn("barfoo")) throw ""; | |
if (validator.isIn("foobar")) throw ""; | |
if (validator.isIn("barfoo")) throw ""; | |
if (validator.isIn("")) throw ""; | |
if (validator.isIn("4")) throw ""; | |
if (validator.isIn("")) throw ""; | |
if (validator.isIn("foo")) throw ""; | |
if (validator.isIn("")) throw ""; | |
// | |
//should validate a string that is in another object | |
// | |
if (validator.isIn("foobarbaz")) throw ""; | |
if (validator.isIn("barfoo")) throw ""; | |
if (validator.isIn("")) throw ""; | |
if (validator.isIn("4")) throw ""; | |
if (validator.isIn("")) throw ""; | |
// | |
//should validate dates | |
// | |
if (validator.isDate("2011-08-04") == false) throw ""; | |
if (validator.isDate("04. 08. 2011.") == false) throw ""; | |
if (validator.isDate("08/04/2011") == false) throw ""; | |
if (validator.isDate("2011.08.04") == false) throw ""; | |
if (validator.isDate("4. 8. 2011. GMT") == false) throw ""; | |
if (validator.isDate("2011-08-04 12:00") == false) throw ""; | |
if (validator.isDate("foo")) throw ""; | |
if (validator.isDate("2011-foo-04")) throw ""; | |
if (validator.isDate("GMT")) throw ""; | |
// | |
//should validate dates against a start date | |
// | |
if (validator.isAfter("2010-07-02")) throw ""; | |
if (validator.isAfter("2011-08-03")) throw ""; | |
if (validator.isAfter("1970-01-01T00:00:00.000Z")) throw ""; | |
if (validator.isAfter("foo")) throw ""; | |
if (validator.isAfter("2100-08-04") == false) throw ""; | |
if (validator.isAfter("2045-05-27T15:34:57.758Z") == false) throw ""; | |
if (validator.isAfter("2010-07-02")) throw ""; | |
if (validator.isAfter("1970-01-01T00:00:00.000Z")) throw ""; | |
// | |
//should validate dates against an end date | |
// | |
if (validator.isBefore("2010-07-02") == false) throw ""; | |
if (validator.isBefore("2010-08-04") == false) throw ""; | |
if (validator.isBefore("1970-01-01T00:00:00.000Z") == false) throw ""; | |
if (validator.isBefore("2010-07-02") == false) throw ""; | |
if (validator.isBefore("2010-08-04") == false) throw ""; | |
if (validator.isBefore("1970-01-01T00:00:00.000Z") == false) throw ""; | |
if (validator.isBefore("2000-08-04") == false) throw ""; | |
if (validator.isBefore("1970-01-01T00:00:00.000Z") == false) throw ""; | |
if (validator.isBefore("2015-05-25T15:34:57.759Z") == false) throw ""; | |
if (validator.isBefore("2100-07-02")) throw ""; | |
if (validator.isBefore("2017-11-10T00:00:00.000Z")) throw ""; | |
// | |
//should validate that integer strings are divisible by a number | |
// | |
if (validator.isDivisibleBy("1")) throw ""; | |
if (validator.isDivisibleBy("2.5")) throw ""; | |
if (validator.isDivisibleBy("101")) throw ""; | |
if (validator.isDivisibleBy("foo")) throw ""; | |
if (validator.isDivisibleBy("")) throw ""; | |
// | |
//should validate credit cards | |
// | |
if (validator.isCreditCard("375556917985515") == false) throw ""; | |
if (validator.isCreditCard("36050234196908") == false) throw ""; | |
if (validator.isCreditCard("4716461583322103") == false) throw ""; | |
if (validator.isCreditCard("4716-2210-5188-5662") == false) throw ""; | |
if (validator.isCreditCard("4929 7226 5379 7141") == false) throw ""; | |
if (validator.isCreditCard("5398228707871527") == false) throw ""; | |
if (validator.isCreditCard("foo")) throw ""; | |
if (validator.isCreditCard("foo")) throw ""; | |
if (validator.isCreditCard("5398228707871528")) throw ""; | |
// | |
//should validate ISBNs | |
// | |
if (validator.isISBN("3836221195") == false) throw ""; | |
if (validator.isISBN("3-8362-2119-5") == false) throw ""; | |
if (validator.isISBN("3 8362 2119 5") == false) throw ""; | |
if (validator.isISBN("1617290858") == false) throw ""; | |
if (validator.isISBN("1-61729-085-8") == false) throw ""; | |
if (validator.isISBN("1 61729 085-8") == false) throw ""; | |
if (validator.isISBN("0007269706") == false) throw ""; | |
if (validator.isISBN("0-00-726970-6") == false) throw ""; | |
if (validator.isISBN("0 00 726970 6") == false) throw ""; | |
if (validator.isISBN("3423214120") == false) throw ""; | |
if (validator.isISBN("3-423-21412-0") == false) throw ""; | |
if (validator.isISBN("3 423 21412 0") == false) throw ""; | |
if (validator.isISBN("340101319X") == false) throw ""; | |
if (validator.isISBN("3-401-01319-X") == false) throw ""; | |
if (validator.isISBN("3 401 01319 X") == false) throw ""; | |
if (validator.isISBN("3423214121")) throw ""; | |
if (validator.isISBN("3-423-21412-1")) throw ""; | |
if (validator.isISBN("3 423 21412 1")) throw ""; | |
if (validator.isISBN("123456789a")) throw ""; | |
if (validator.isISBN("foo")) throw ""; | |
if (validator.isISBN("")) throw ""; | |
if (validator.isISBN("9783836221191") == false) throw ""; | |
if (validator.isISBN("978-3-8362-2119-1") == false) throw ""; | |
if (validator.isISBN("978 3 8362 2119 1") == false) throw ""; | |
if (validator.isISBN("9783401013190") == false) throw ""; | |
if (validator.isISBN("978-3401013190") == false) throw ""; | |
if (validator.isISBN("978 3401013190") == false) throw ""; | |
if (validator.isISBN("9784873113685") == false) throw ""; | |
if (validator.isISBN("978-4-87311-368-5") == false) throw ""; | |
if (validator.isISBN("978 4 87311 368 5") == false) throw ""; | |
if (validator.isISBN("9783836221190")) throw ""; | |
if (validator.isISBN("978-3-8362-2119-0")) throw ""; | |
if (validator.isISBN("978 3 8362 2119 0")) throw ""; | |
if (validator.isISBN("01234567890ab")) throw ""; | |
if (validator.isISBN("foo")) throw ""; | |
if (validator.isISBN("")) throw ""; | |
if (validator.isISBN("340101319X") == false) throw ""; | |
if (validator.isISBN("9784873113685") == false) throw ""; | |
if (validator.isISBN("3423214121")) throw ""; | |
if (validator.isISBN("9783836221190")) throw ""; | |
// | |
//should validate JSON | |
// | |
if (validator.isJSON("{ \"key\": \"value\" }") == false) throw ""; | |
if (validator.isJSON("{ key: \"value\" }")) throw ""; | |
if (validator.isJSON({"key":"value"})) throw ""; | |
if (validator.isJSON({"key":"value"})) throw ""; | |
if (validator.isJSON("{ 'key': 'value' }")) throw ""; | |
// | |
//should validate multibyte strings | |
// | |
if (validator.isMultibyte("abc")) throw ""; | |
if (validator.isMultibyte("abc123")) throw ""; | |
if (validator.isMultibyte("<>@\" *.")) throw ""; | |
// | |
//should validate ascii strings | |
// | |
if (validator.isAscii("foobar") == false) throw ""; | |
if (validator.isAscii("0987654321") == false) throw ""; | |
if (validator.isAscii("test@example.com") == false) throw ""; | |
if (validator.isAscii("1234abcDEF") == false) throw ""; | |
// | |
//should validate full-width strings | |
// | |
if (validator.isFullWidth("abc")) throw ""; | |
if (validator.isFullWidth("abc123")) throw ""; | |
if (validator.isFullWidth("!\"#$%&()<>/+=-_? ~^|.,@`{}[]")) throw ""; | |
// | |
//should validate half-width strings | |
// | |
if (validator.isHalfWidth("!\"#$%&()<>/+=-_? ~^|.,@`{}[]") == false) throw ""; | |
if (validator.isHalfWidth("l-btn_02--active") == false) throw ""; | |
if (validator.isHalfWidth("abc123?") == false) throw ""; | |
if (validator.isHalfWidth("???????") == false) throw ""; | |
// | |
//should validate variable-width strings | |
// | |
if (validator.isVariableWidth("abc")) throw ""; | |
if (validator.isVariableWidth("abc123")) throw ""; | |
if (validator.isVariableWidth("!\"#$%&()<>/+=-_? ~^|.,@`{}[]")) throw ""; | |
if (validator.isVariableWidth("?????????????")) throw ""; | |
if (validator.isVariableWidth("??????")) throw ""; | |
if (validator.isVariableWidth("??????")) throw ""; | |
// | |
//should validate surrogate pair strings | |
// | |
if (validator.isSurrogatePair("???")) throw ""; | |
if (validator.isSurrogatePair("?")) throw ""; | |
if (validator.isSurrogatePair("ABC1-2-3")) throw ""; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment