FileValidator

FileValidator

used to validate files attached to request


Constructor

new FileValidator(context, key, value, exists, params, deleteOnCheckFailed)

Parameters:
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

Extends

Methods

addError(tip)

Adds an error to the context

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

blacklist(s)

remove characters that appear in the blacklist

Parameters:
Name Type Description
s String
Overrides:

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).

Parameters:
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:

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

Parameters:
Name Type Description
key String
value Object
Overrides:

contains(s, tip)

check if the param contains the str

Parameters:
Name Type Description
s
tip String

Message to return on fail

Overrides:

contentTypeMatch(reg, tip)

Check the file's contentType with regular expression

Parameters:
Name Type Description
reg RegExp
tip String

message to return if test fails

copy(dst, afterCopy)

Move uploaded file to the target location.

Parameters:
Name Type Description
dst String | function

accepts params (fileObject,fieldName,context) returns String

afterCopy function

accepts params (fileObject,fieldName,context)

decodeBase64(inBuffer, tip)

decode current base64 to a normal string if inBuffer is true the value will be a Buffer

Parameters:
Name Type Description
inBuffer
tip

decodeURI(tip)

Parameters:
Name Type Description
tip
Overrides:

decodeURIComponent(tip)

Parameters:
Name Type Description
tip

default(d)

if the param not exits or is an empty string, it will take the default value

Parameters:
Name Type Description
d Object

default value if it doesn't exist

Overrides:

delete()

Deletes the uploaded file.

empty()

the params can be a empty string

Overrides:

encodeBase64()

encode current value to base64 string

encodeURI()

Overrides:

encodeURIComponent()

ensure(assertion, tip, shouldBail)

Ensure that assertion, an arbitrary value, is truthy.

Parameters:
Name Type Description
assertion
tip String

Message to return on fail

shouldBail
Overrides:

ensureNot(assertion, tip, shouldBail)

Ensure that assertion, an arbitrary value, is falsey.

Parameters:
Name Type Description
assertion
tip String

Message to return on fail

shouldBail
Overrides:

eq(l, tip)

check if the param equal to the value

Parameters:
Name Type Description
l
tip String

Message to return on fail

Overrides:

escape()

replace <, >, & and " with HTML entities

Overrides:

exist(tip)

Checks if key exists

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

fileNameMatch(reg, tip)

Check the file's name with regular expression

Parameters:
Name Type Description
reg RegExp
tip String

message to return if test fails

filter(fn, scope)

Filter the value if value is array. fn format function(value,index,key,requestParams):boolean

Parameters:
Name Type Description
fn function
scope Object
Overrides:

first()

Returns first value

Overrides:

ge(l, tip)

check if the param great then or equal the value

Parameters:
Name Type Description
l
tip String

Message to return on fail

Overrides:

get(index)

Returns value of given index

Parameters:
Name Type Description
index int

index to retreive value at

Overrides:

gt(l, tip)

check if the param greater then the value

Parameters:
Name Type Description
l
tip String

Message to return on fail

Overrides:

hasError() → {boolean}

Overrides:
Returns:
Type
boolean

hash(alg, enc)

hash current value use specified algorithm and encoding(if supplied , default is 'hex'). see Hash

Parameters:
Name Type Description
alg
enc
Overrides:

in(arr, tip)

check if the param is in the array.

Parameters:
Name Type Description
arr
tip String

Message to return on fail

Overrides:

isAfter(d, tip)

check if the param is a date that's after the specified date

Parameters:
Name Type Description
d Date
tip String

Message to return on fail

Overrides:

isAlpha(tip, locale)

check if the param contains only letters (a-zA-Z)

Parameters:
Name Type Description
tip String

Message to return on fail

locale
Overrides:

isAlphanumeric(tip, locale)

check if the param contains only letters and numbers

Parameters:
Name Type Description
tip String

Message to return on fail

locale

isAscii(tip)

check if the param contains ASCII chars only

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isBase64(tip)

check if a param is base64 encoded

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isBefore(d, tip)

check if the param is a date that's before the specified date

Parameters:
Name Type Description
d Date
tip String

Message to return on fail

Overrides:

isByteLength(min, max, charset, tip)

check if the param's length (in bytes) falls in a range

Parameters:
Name Type Description
min int
max int
charset String

default 'utf8'

tip String

Message to return on fail

isCreditCard(tip)

check if the param is a credit card

Parameters:
Name Type Description
tip String

Message to return on fail

isCurrency(tip)

check if the param is a currency

Parameters:
Name Type Description
tip String

Message to return on fail

isDataURI(tip)

check if the param is a data uri

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isDate(tip)

check if the param is a date

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isDivisibleBy(n, tip)

check if the param is a number that's divisible by another

Parameters:
Name Type Description
n
tip String

Message to return on fail

isEmail(tip, options)

check if the param is an email

Parameters:
Name Type Description
tip String

Message to return on fail

options
Overrides:

isFloat(tip, options)

check if the param is float

Parameters:
Name Type Description
tip String

Message to return on fail

options
Overrides:

isFQDN(tip, options)

Check if the param is a fully qualified domain name.

Parameters:
Name Type Description
tip String
options
Overrides:

isFullWidth(tip)

check if the param contains any full-width chars

Parameters:
Name Type Description
tip String

Message to return on fail

isHalfWidth(tip)

check if the param contains any half-width chars

Parameters:
Name Type Description
tip String

Message to return on fail

isHexadecimal(tip)

check if the param is a hexadecimal number

Parameters:
Name Type Description
tip String

Message to return on fail

isHexColor(tip)

check if the param is a hexadecimal color

Parameters:
Name Type Description
tip String

Message to return on fail

isImageContentType(tip)

Check if content type is of image.

Parameters:
Name Type Description
tip String

message to return if test fails

isInt(tip, options)

check if the param is integer

Parameters:
Name Type Description
tip String

Message to return on fail

options
Overrides:

isIp(tip, version)

check if the param is an IP (version 4 or 6)

Parameters:
Name Type Description
tip String

Message to return on fail

version
Overrides:

isISBN(tip, version)

check if the param is an ISBN (version 10 or 13)

Parameters:
Name Type Description
tip String

Message to return on fail

version
Overrides:

isISIN(tip)

check if the param is a ISIN

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isISO8601(tip)

check if the param is a ISO8601 string

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isJSON(tip)

check if the param is valid JSON (note: uses JSON.parse)

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isLength(min, max, tip)

check the param length.

Parameters:
Name Type Description
min
max
tip String

Message to return on fail

Overrides:

isLowercase(tip)

check if the param is lowercase

Parameters:
Name Type Description
tip String

Message to return on fail

isMACAddress(tip)

check if the param is a MAC address

Parameters:
Name Type Description
tip String

Message to return on fail

isMobilePhone(tip)

check if the param is a mobile phone

Parameters:
Name Type Description
tip String

Message to return on fail

isMultibyte(tip)

check if the param contains one or more multibyte chars

Parameters:
Name Type Description
tip String

Message to return on fail

isNull(tip)

check if the param is null

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isNumeric(tip)

check if the param contains only numbers

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isSurrogatePair(tip)

check if the param contains any surrogate pairs chars

Parameters:
Name Type Description
tip String

Message to return on fail

isTime(tip)

check if param is a time

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

isUppercase(tip)

check if the param is uppercase

Parameters:
Name Type Description
tip String

Message to return on fail

isUrl(tip, options)

check if the param is an URL

Parameters:
Name Type Description
tip String

Message to return on fail

options
Overrides:

isUUID(tip, ver)

check if the param is a UUID (version 3, 4 or 5)

Parameters:
Name Type Description
tip String

Message to return on fail

ver int
Overrides:

isVariableWidth(tip)

check if the param contains a mixture of full and half-width chars

Parameters:
Name Type Description
tip String

Message to return on fail

le(l, tip)

check if the param less then or equal the value

Parameters:
Name Type Description
l
tip String

Message to return on fail

Overrides:

lt(l, tip)

check if the param less then the value

Parameters:
Name Type Description
l
tip String

Message to return on fail

Overrides:

ltrim(c)

trim characters from the left-side of the param

Parameters:
Name Type Description
c
Overrides:

match(reg, tip)

pattern must be a RegExp instance ,eg. /abc/i

Parameters:
Name Type Description
reg
tip String

Message to return on fail

Overrides:

md5()

md5 current value into hex string

Overrides:

move(dst, afterMove)

Move upload file to the target location. If target ends with '/' or '',the target will be deemed as directory.

Parameters:
Name Type Description
dst String | function

accepts params (fileObject,fieldName,context) returns String

afterMove function

accepts params (fileObject,fieldName,context)

neq(l, tip)

check if the param not equal to the value

Parameters:
Name Type Description
l
tip String

Message to return on fail

Overrides:

notBlank(tip)

check if the param is not blank,use /^\s*$/gi reg to check.

Parameters:
Name Type Description
tip String

Message to return on fail

Overrides:

notContains(s, tip)

check if the param not contains the str

Parameters:
Name Type Description
s
tip String

Message to return on fail

notEmpty(tip)

Checks that file is not empty

Parameters:
Name Type Description
tip String

message to return if test fails

Overrides:

notMatch(reg, tip)

Ensure that a string does not match the supplied regular expression.

Parameters:
Name Type Description
reg
tip String

Message to return on fail

Overrides:

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:

replace(a, b)

the same as String replace

Parameters:
Name Type Description
a String
b String
Overrides:

rtrim(c)

trim characters from the right-side of the param.

Parameters:
Name Type Description
c
Overrides:

sha1()

sha1 current value into hex string

Overrides:

size(min, max, tip)

Checks size of file is between values

Parameters:
Name Type Description
min int

minimum file size

max int

maximum file size

tip String

message to return if test fails

stripLow(nl)

remove characters with a numerical value < 32 and 127, mostly control characters

Parameters:
Name Type Description
nl
Overrides:

suffixIn(arr, tip)

Check the suffix of file's if in specified arr. arr eg. ['png','jpg']

Parameters:
Name Type Description
arr Array
tip String

message to return if test fails

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:

toDate()

convert param to js Date object.

Overrides:

toFloat(tip)

convert param to float

Parameters:
Name Type Description
tip
Overrides:

toInt(tip, radix, options)

convert param to integer.radix for toInt,options for isInt

Parameters:
Name Type Description
tip
radix
options
Overrides:

toJson(tip)

convert param to json object

Parameters:
Name Type Description
tip
Overrides:

toLowerCase()

convert param to lowercase

toUpperCase()

Convert param to uppercase

trim(c)

trim characters (whitespace by default) from both sides of the param.

Parameters:
Name Type Description
c
Overrides:

type(t, tip)

Checks if value is of type

Parameters:
Name Type Description
t Object

type to match

tip String

message to send if fails

Overrides:

whitelist(s)

remove characters that do not appear in the whitelist.

Parameters:
Name Type Description
s String
Overrides: