Class: TypeValidator

sogv.TypeValidator

Validates that a value is of a specific data type.

For example, if a variable should be an array, you can use this constraint with the array type option to validate this.

new sogv.TypeValidator (data, options, optionRules, lang, internal)

Create a new Validator.

Name Type Description
data * The data which needs to be validated.
options Object The setting options
optionRules Object The validation rules for setting options.
lang String The language used by the application. Default: "en".
internal Boolean If this parameter is true, it means, that validation called from core.
Properties:
Name Type Description
alias Array

The aliases for the current validator.

They could be used in the short validation format.

Defined aliases: ['type'].

options Object The description of the required options.
Example
var validator = new sogv.TypeValidator(data, {type: 'array'});
if (false === validator.isValid()) {
     validator.errors().first();
}

Extends

Members

any Boolean

If true, one of data type needs to be valid, otherwise passed data should be valid for all types.

Default: false

data * inherited

Data that needs to be validated.

lang String inherited

Language of error messages.

message String

The message if the underlying data is not of the given type.

Default: "This value should be of type %%type%%."

You can use the following parameters in this message:

Parameter Description
%%type%% The expected type
%%value%% The current (invalid) value

type String | Array

This required option defines the type or collection of types allowed for the given value.

The following types are available:
  • array - Finds whether a variable is an array.
  • bool, boolean - Finds out whether a variable is a boolean.
  • callable - Verify that the contents of a variable can be called as a function.
  • float - Finds whether the type of a variable is float.
  • double - Finds whether the type of a variable is double.
  • int, integer - Find whether the type of a variable is integer.
  • iterable - Verify that the contents of a variable is an iterable value.
  • null - Finds whether a variable is NULL.
  • numeric - Finds whether a variable is a number or a numeric string.
  • object - Finds whether a variable is an object.
  • real - Finds whether the type of a variable is real.
  • scalar - Finds whether a variable is a scalar. Scalar variables are those containing an integer, float, string or boolean.
  • string - Find whether the type of a variable is string.
  • alnum - Check for alphanumeric character(s).
  • alpha - Check for alphabetic character(s).
  • cntrl - Check for control character(s).
  • digit - Check for numeric character(s).
  • graph - Check for any printable character(s) except space.
  • lower - Check for lowercase character(s).
  • print - Check for printable character(s).
  • punct - Check for any printable character which is not whitespace or an alphanumeric character.
  • space - Check for whitespace character(s).
  • upper - Check for uppercase character(s).
  • xdigit - Check for character(s) representing a hexadecimal digit.

Methods

errors ()sogv.Error inherited

Return error errors

Returns:
Type Description
sogv.Error Error messages

isValid ()Boolean inherited overrides

Check if data valid.

Returns:
Type Description
Boolean Validation status