Constructor
new FileValidator(context, key, value, exists, params, deleteOnCheckFailed)
Name | Type | Description |
---|---|---|
context |
object | koa ctx instance |
key |
string | key to validate in request |
value |
object | value of key if exists |
exists |
bool | if key exists |
params |
||
deleteOnCheckFailed |
bool | deletes file if any validation fails |
- Source:
Extends
Methods
addError(tip)
Adds an error to the context
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
blacklist(s)
remove characters that appear in the blacklist
Name | Type | Description |
---|---|---|
s |
String |
- Overrides:
- Source:
check(fn, tip, scope)
Runs passed in function and checks for returned boolean. Function will be passed scope if provided else will receive (value, key, context).
Name | Type | Description |
---|---|---|
fn |
function | function to run that returns boolean |
tip |
tip for error if fn fails |
|
scope |
custom scope that can be passed into fn |
- Overrides:
- Source:
clone(key, value)
clone current value to the new key, if newValue supplied , use it. this.checkBody('v1').clone('md5').md5(); then your can use this.request.body.md5
Name | Type | Description |
---|---|---|
key |
String | |
value |
Object |
- Overrides:
- Source:
contains(s, tip)
check if the param contains the str
Name | Type | Description |
---|---|---|
s |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
contentTypeMatch(reg, tip)
Check the file's contentType with regular expression
Name | Type | Description |
---|---|---|
reg |
RegExp | |
tip |
String | message to return if test fails |
- Source:
copy(dst, afterCopy)
Move uploaded file to the target location.
Name | Type | Description |
---|---|---|
dst |
String | function | accepts params (fileObject,fieldName,context) returns String |
afterCopy |
function | accepts params (fileObject,fieldName,context) |
- Source:
decodeBase64(inBuffer, tip)
decode current base64 to a normal string if inBuffer is true the value will be a Buffer
Name | Type | Description |
---|---|---|
inBuffer |
||
tip |
- Overrides:
- Source:
decodeURI(tip)
Name | Type | Description |
---|---|---|
tip |
- Overrides:
- Source:
decodeURIComponent(tip)
Name | Type | Description |
---|---|---|
tip |
- Overrides:
- Source:
default(d)
if the param not exits or is an empty string, it will take the default value
Name | Type | Description |
---|---|---|
d |
Object | default value if it doesn't exist |
- Overrides:
- Source:
delete()
Deletes the uploaded file.
- Source:
empty()
the params can be a empty string
- Overrides:
- Source:
encodeBase64()
encode current value to base64 string
- Overrides:
- Source:
encodeURI()
- Overrides:
- Source:
encodeURIComponent()
- Overrides:
- Source:
ensure(assertion, tip, shouldBail)
Ensure that assertion
, an arbitrary value, is truthy.
Name | Type | Description |
---|---|---|
assertion |
||
tip |
String | Message to return on fail |
shouldBail |
- Overrides:
- Source:
ensureNot(assertion, tip, shouldBail)
Ensure that assertion
, an arbitrary value, is falsey.
Name | Type | Description |
---|---|---|
assertion |
||
tip |
String | Message to return on fail |
shouldBail |
- Overrides:
- Source:
eq(l, tip)
check if the param equal to the value
Name | Type | Description |
---|---|---|
l |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
escape()
replace <, >, & and " with HTML entities
- Overrides:
- Source:
exist(tip)
Checks if key exists
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
fileNameMatch(reg, tip)
Check the file's name with regular expression
Name | Type | Description |
---|---|---|
reg |
RegExp | |
tip |
String | message to return if test fails |
- Source:
filter(fn, scope)
Filter the value if value is array. fn format function(value,index,key,requestParams):boolean
Name | Type | Description |
---|---|---|
fn |
function | |
scope |
Object |
- Overrides:
- Source:
first()
Returns first value
- Overrides:
- Source:
ge(l, tip)
check if the param great then or equal the value
Name | Type | Description |
---|---|---|
l |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
get(index)
Returns value of given index
Name | Type | Description |
---|---|---|
index |
int | index to retreive value at |
- Overrides:
- Source:
gt(l, tip)
check if the param greater then the value
Name | Type | Description |
---|---|---|
l |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
hasError() → {boolean}
- Overrides:
- Source:
- Type
- boolean
hash(alg, enc)
hash current value use specified algorithm and encoding(if supplied , default is 'hex'). see Hash
Name | Type | Description |
---|---|---|
alg |
||
enc |
- Overrides:
- Source:
in(arr, tip)
check if the param is in the array.
Name | Type | Description |
---|---|---|
arr |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
isAfter(d, tip)
check if the param is a date that's after the specified date
Name | Type | Description |
---|---|---|
d |
Date | |
tip |
String | Message to return on fail |
- Overrides:
- Source:
isAlpha(tip, locale)
check if the param contains only letters (a-zA-Z)
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
locale |
- Overrides:
- Source:
isAlphanumeric(tip, locale)
check if the param contains only letters and numbers
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
locale |
- Overrides:
- Source:
isAscii(tip)
check if the param contains ASCII chars only
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isBase64(tip)
check if a param is base64 encoded
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isBefore(d, tip)
check if the param is a date that's before the specified date
Name | Type | Description |
---|---|---|
d |
Date | |
tip |
String | Message to return on fail |
- Overrides:
- Source:
isByteLength(min, max, charset, tip)
check if the param's length (in bytes) falls in a range
Name | Type | Description |
---|---|---|
min |
int | |
max |
int | |
charset |
String | default 'utf8' |
tip |
String | Message to return on fail |
- Overrides:
- Source:
isCreditCard(tip)
check if the param is a credit card
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isCurrency(tip)
check if the param is a currency
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isDataURI(tip)
check if the param is a data uri
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isDate(tip)
check if the param is a date
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isDivisibleBy(n, tip)
check if the param is a number that's divisible by another
Name | Type | Description |
---|---|---|
n |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
isEmail(tip, options)
check if the param is an email
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
options |
- Overrides:
- Source:
isFloat(tip, options)
check if the param is float
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
options |
- Overrides:
- Source:
isFQDN(tip, options)
Check if the param is a fully qualified domain name.
Name | Type | Description |
---|---|---|
tip |
String | |
options |
- Overrides:
- Source:
isFullWidth(tip)
check if the param contains any full-width chars
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isHalfWidth(tip)
check if the param contains any half-width chars
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isHexadecimal(tip)
check if the param is a hexadecimal number
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isHexColor(tip)
check if the param is a hexadecimal color
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isImageContentType(tip)
Check if content type is of image.
Name | Type | Description |
---|---|---|
tip |
String | message to return if test fails |
- Source:
isInt(tip, options)
check if the param is integer
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
options |
- Overrides:
- Source:
isIp(tip, version)
check if the param is an IP (version 4 or 6)
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
version |
- Overrides:
- Source:
isISBN(tip, version)
check if the param is an ISBN (version 10 or 13)
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
version |
- Overrides:
- Source:
isISIN(tip)
check if the param is a ISIN
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isISO8601(tip)
check if the param is a ISO8601 string
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isJSON(tip)
check if the param is valid JSON (note: uses JSON.parse)
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isLength(min, max, tip)
check the param length.
Name | Type | Description |
---|---|---|
min |
||
max |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
isLowercase(tip)
check if the param is lowercase
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isMACAddress(tip)
check if the param is a MAC address
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isMobilePhone(tip)
check if the param is a mobile phone
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isMultibyte(tip)
check if the param contains one or more multibyte chars
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isNull(tip)
check if the param is null
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isNumeric(tip)
check if the param contains only numbers
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isSurrogatePair(tip)
check if the param contains any surrogate pairs chars
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isTime(tip)
check if param is a time
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isUppercase(tip)
check if the param is uppercase
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
isUrl(tip, options)
check if the param is an URL
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
options |
- Overrides:
- Source:
isUUID(tip, ver)
check if the param is a UUID (version 3, 4 or 5)
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
ver |
int |
- Overrides:
- Source:
isVariableWidth(tip)
check if the param contains a mixture of full and half-width chars
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
le(l, tip)
check if the param less then or equal the value
Name | Type | Description |
---|---|---|
l |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
lt(l, tip)
check if the param less then the value
Name | Type | Description |
---|---|---|
l |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
ltrim(c)
trim characters from the left-side of the param
Name | Type | Description |
---|---|---|
c |
- Overrides:
- Source:
match(reg, tip)
pattern must be a RegExp instance ,eg. /abc/i
Name | Type | Description |
---|---|---|
reg |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
md5()
md5 current value into hex string
- Overrides:
- Source:
move(dst, afterMove)
Move upload file to the target location. If target ends with '/' or '',the target will be deemed as directory.
Name | Type | Description |
---|---|---|
dst |
String | function | accepts params (fileObject,fieldName,context) returns String |
afterMove |
function | accepts params (fileObject,fieldName,context) |
- Source:
neq(l, tip)
check if the param not equal to the value
Name | Type | Description |
---|---|---|
l |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
notBlank(tip)
check if the param is not blank,use /^\s*$/gi reg to check.
Name | Type | Description |
---|---|---|
tip |
String | Message to return on fail |
- Overrides:
- Source:
notContains(s, tip)
check if the param not contains the str
Name | Type | Description |
---|---|---|
s |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
notEmpty(tip)
Checks that file is not empty
Name | Type | Description |
---|---|---|
tip |
String | message to return if test fails |
- Overrides:
- Source:
notMatch(reg, tip)
Ensure that a string does not match the supplied regular expression.
Name | Type | Description |
---|---|---|
reg |
||
tip |
String | Message to return on fail |
- Overrides:
- Source:
optional()
The param may not in the params. If the param not exists,it has no error,no matter whether have other checker or not.
- Overrides:
- Source:
replace(a, b)
the same as String replace
Name | Type | Description |
---|---|---|
a |
String | |
b |
String |
- Overrides:
- Source:
rtrim(c)
trim characters from the right-side of the param.
Name | Type | Description |
---|---|---|
c |
- Overrides:
- Source:
sha1()
sha1 current value into hex string
- Overrides:
- Source:
size(min, max, tip)
Checks size of file is between values
Name | Type | Description |
---|---|---|
min |
int | minimum file size |
max |
int | maximum file size |
tip |
String | message to return if test fails |
- Source:
stripLow(nl)
remove characters with a numerical value < 32 and 127, mostly control characters
Name | Type | Description |
---|---|---|
nl |
- Overrides:
- Source:
suffixIn(arr, tip)
Check the suffix of file's if in specified arr. arr eg. ['png','jpg']
Name | Type | Description |
---|---|---|
arr |
Array | |
tip |
String | message to return if test fails |
- Source:
toBoolean()
Convert the param to a boolean. Everything except for '0', 'false' and '' returns true. In strict mode only '1' and 'true' return true.
- Overrides:
- Source:
toDate()
convert param to js Date object.
- Overrides:
- Source:
toFloat(tip)
convert param to float
Name | Type | Description |
---|---|---|
tip |
- Overrides:
- Source:
toInt(tip, radix, options)
convert param to integer.radix for toInt,options for isInt
Name | Type | Description |
---|---|---|
tip |
||
radix |
||
options |
- Overrides:
- Source:
toJson(tip)
convert param to json object
Name | Type | Description |
---|---|---|
tip |
- Overrides:
- Source:
toLowerCase()
convert param to lowercase
- Overrides:
- Source:
toUpperCase()
Convert param to uppercase
- Overrides:
- Source:
trim(c)
trim characters (whitespace by default) from both sides of the param.
Name | Type | Description |
---|---|---|
c |
- Overrides:
- Source:
type(t, tip)
Checks if value is of type
Name | Type | Description |
---|---|---|
t |
Object | type to match |
tip |
String | message to send if fails |
- Overrides:
- Source:
whitelist(s)
remove characters that do not appear in the whitelist.
Name | Type | Description |
---|---|---|
s |
String |
- Overrides:
- Source: