Merge pull request #3133 from tgfjt/feature/validator

added validator/validator.d.ts
This commit is contained in:
Masahiro Wakame 2014-11-13 12:37:23 +09:00
commit cf08dc6503
3 changed files with 297 additions and 0 deletions

View File

@ -423,6 +423,7 @@ All definitions files include a header with the author and editors, so at some p
* [urlrouter](https://github.com/fengmk2/urlrouter) (by [Carlos Ballesteros Velasco](https://github.com/soywiz))
* [UUID.js](https://github.com/LiosK/UUID.js) (by [Jason Jarrett](https://github.com/staxmanade))
* [Valerie](https://github.com/davewatts/valerie) (by [Howard Richards](https://github.com/conficient))
* [validator](https://github.com/chriso/validator.js) (by [tgfjt](https://github.com/tgfjt))
* [Velocity](http://velocityjs.org/) (by [Greg Smith](https://github.com/smrq))
* [Viewporter](https://github.com/zynga/viewporter) (by [Boris Yankov](https://github.com/borisyankov))
* [Vimeo](http://developer.vimeo.com/player/js-api) (by [Daz Wilkin](https://github.com/DazWilkin/))

View File

@ -0,0 +1,106 @@
/// <reference path="./validator.d.ts" />
import validator = require("validator");
validator.extend("isTest", function(str) {
return !str;
});
validator.equals("abc", "Abc");
validator.contains("foo", "foobar");
validator.matches("foobar", "foo/i");
validator.isEmail("sample");
validator.isURL("sample");
validator.isFQDN("sample");
validator.isIP("sample");
validator.isAlpha("sample");
validator.isNumeric("sample");
validator.isAlphanumeric("sample");
validator.isBase64("sample");
validator.isHexadecimal("sample");
validator.isHexColor("sample");
validator.isLowercase("sample");
validator.isUppercase("sample");
validator.isInt("sample");
validator.isFloat("sample");
validator.isDivisibleBy("sample", 2);
validator.isNull("sample");
validator.isLength("sample", 3, 5);
validator.isByteLength("sample", 3);
validator.isUUID("sample");
validator.isDate("sample");
validator.isAfter("sample");
validator.isBefore("sample");
validator.isIn("sample", []);
validator.isCreditCard("sample");
validator.isISBN("sample");
validator.isJSON("sample");
validator.isMultibyte("sample");
validator.isAscii("sample");
validator.isFullWidth("sample");
validator.isHalfWidth("sample");
validator.isVariableWidth("sample");
validator.isSurrogatePair("sample");
validator.isMongoId("sample");
validator.toString(123);
validator.toDate(1225);
validator.toFloat('011');
validator.toInt('aa');
validator.toBoolean('yes!');
validator.trim(' triming ');
validator.ltrim(' triming ');
validator.rtrim(' triming ');
validator.escape('<script>');
validator.stripLow('\x7Ffoo\x02');
validator.whitelist('ab', 'abcdef');
validator.blacklist('abc', 'abcdef');
validator.normalizeEmail('!');

190
validator/validator.d.ts vendored Normal file
View File

@ -0,0 +1,190 @@
// Type definitions for validator.js v3.22.1
// Project: https://github.com/chriso/validator.js
// Definitions by: tgfjt <https://github.com/tgfjt>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// options for #isURL
interface IURLoptions {
protocols?: string[]
require_tld?: boolean
require_protocol?: boolean
allow_underscores?: boolean
}
// options for isFQDN
interface IFQDNoptions {
require_tld?: boolean
allow_underscores?: boolean
}
// options for normalizeEmail
interface IEmailoptions {
lowercase?: boolean
}
// callback type for #extend
interface IExtendCallback {
(argv: string): any
}
// return function for #extend
interface IExtendFunc {
(argv: string): boolean
}
interface IValidatorStatic {
// add your own validators
extend(name: string, fn: IExtendCallback): IExtendFunc;
// check if the string matches the comparison.
equals(str: string, comparison: any): boolean;
// check if the string contains the seed.
contains(str: string, elem: any): boolean;
// check if string matches the pattern.
matches(str: string, pattern: any, modifiers?: string): boolean;
// check if the string is an email.
isEmail(str: string): boolean;
// check if the string is an URL.
isURL(str: string, options?: IURLoptions): boolean;
// check if the string is a fully qualified domain name (e.g. domain.com).
isFQDN(str: string, options?: IFQDNoptions): boolean;
// check if the string is an IP (version 4 or 6).
isIP(str: string, version?: number): boolean;
// check if the string contains only letters (a-zA-Z).
isAlpha(str: string): boolean;
// check if the string contains only numbers.
isNumeric(str: string): boolean;
// check if the string contains only letters and numbers.
isAlphanumeric(str: string): boolean;
// check if a string is base64 encoded.
isBase64(str: string): boolean;
// check if the string is a hexadecimal number.
isHexadecimal(str: string): boolean;
// check if the string is a hexadecimal color.
isHexColor(str: string): boolean;
// check if the string is lowercase.
isLowercase(str: string): boolean;
// check if the string is uppercase.
isUppercase(str: string): boolean;
// check if the string is an integer.
isInt(str: string): boolean;
// check if the string is a float.
isFloat(str: string): boolean;
// check if the string is a number that's divisible by another.
isDivisibleBy(str: string, number: number): boolean;
// check if the string is null.
isNull(str: string): boolean;
// check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
isLength(str: string, min: number, max?: number): boolean;
// check if the string's length (in bytes) falls in a range.
isByteLength(str: string, min: number, max?: number): boolean;
// check if the string is a UUID (version 3, 4 or 5).
isUUID(str: string, version?: number): boolean;
// check if the string is a date.
isDate(str: string): boolean;
// check if the string is a date that's after the specified date (defaults to now).
isAfter(str: string, date?: Date): boolean;
// check if the string is a date that's before the specified date.
isBefore(str: string, date?: Date): boolean;
// check if the string is in a array of allowed values.
isIn(str: string, values: any[]): boolean;
// check if the string is a credit card.
isCreditCard(str: string): boolean;
// check if the string is an ISBN (version 10 or 13).
isISBN(str: string, version?: number): boolean;
// check if the string is valid JSON (note: uses JSON.parse).
isJSON(str: string): boolean;
// check if the string contains one or more multibyte chars.
isMultibyte(str: string): boolean;
// check if the string contains ASCII chars only.
isAscii(str: string): boolean;
// check if the string contains any full-width chars.
isFullWidth(str: string): boolean;
// check if the string contains any half-width chars.
isHalfWidth(str: string): boolean;
// check if the string contains a mixture of full and half-width chars.
isVariableWidth(str: string): boolean;
// check if the string contains any surrogate pairs chars.
isSurrogatePair(str: string): boolean;
// check if the string is a valid hex-encoded representation of a MongoDB ObjectId.
isMongoId(str: string): boolean;
// convert the input to a string.
toString(input: any): string;
// convert the input to a date, or null if the input is not a date.
toDate(input: any): any; // Date or null
// convert the input to a float, or NaN if the input is not a float.
toFloat(input:any): number; // number or NaN
// convert the input to an integer, or NaN if the input is not an integer.
toInt(input:any, radix?: number): number; // number or NaN
// convert the input to a boolean.
toBoolean(input:any, strict?: boolean): boolean;
// trim characters (whitespace by default) from both sides of the input.
trim(input: any, chars?: string): string;
// trim characters from the left-side of the input.
ltrim(input: any, chars?: string): string;
// trim characters from the right-side of the input.
rtrim(input: any, chars?: string): string;
// replace <, >, &, ' and " with HTML entities.
escape(input: string): string;
// remove characters with a numerical value < 32 and 127
stripLow(input: string, keep_new_lines?: boolean): string;
// remove characters that do not appear in the whitelist.
whitelist(input: string, chars: string): string;
// remove characters that appear in the blacklist.
blacklist(input: string, chars: string): string;
// canonicalize an email address.
normalizeEmail(email: string, options?: IEmailoptions): string;
}
declare module "validator" {
var validator: IValidatorStatic;
export = validator;
}