DataTypeValidator

DataTypeValidator

Validates a value against a pre-defined data type

DataTypeValidator is used by DataValidatorListener for validating data objects.

An attribute of a data model may define a data type in validation properties:


 {
    "name": "price",
    "title": "Price",
    "description": "The price of the product.",
    "type": "Number",
    "validation": {
        "type":"NonNegativeNumber"
    }
}
 

There is a collection of pre-defined data types. This collection may also be extended by using dataTypes.json configuration.

TypeDescription
NegativeIntegerAn integer containing only negative values (..,-2,-1)
NegativeNumberA number containing only negative values (..,-2,-1)
NonNegativeIntegerAn integer containing only non-negative values (0,1,2,..)
NonNegativeNumberAn number containing only non-negative values (0,1,2,..)
NonPositiveIntegerAn integer containing only non-positive values (..,-2,-1,0)
NonPositiveNumberA number containing only non-positive values (..,-2,-1,0)
PositiveIntegerAn integer containing only positive values (1,2,..)
PositiveNumberA number containing only positive values (0.1,+1,2,..)
FloatFloat data type is a single-precision floating point.
EmailA string which represents an email address (e.g. user@example.com)
GuidA string which represents a global unique identifier (e.g. 21EC2020-3AEA-4069-A2DD-08002B30309D).
AbsoluteURIA string which represents an absolute URI address (e.g. https://www.example.com/help?article=1001)
RelativeURIA string which represents a relative URI address (e.g. /help?article=1001)
TimeA string which represents an instant of time that recurs every day (e.g. 13:20:45)
DateRepresents a date value.
DateTimeRepresents a date and time value.
DurationA string which represents a duration of time (e.g. P1Y1M10D, P10D, -P0Y1M10D2H15M30S etc)
IPA string which represents an IPv4 address (e.g. 127.0.0.1)

A custom data type may be defined as follows:


 "ProductModel": {
    "comment": "A string which represents the model of a product",
    "label": "Product Model",
    "properties": {
      "pattern":"^[A-Z]{2}\\.\\d{3}$",
      "patternMessage":"Product model seems to be invalid. Valid values are VC.100, DX.010 etc."
    },
    "supertypes": [
      "Text"
    ],
    "type": "string",
    "sqltype":"Text",
    "version":"1.0"
  }
 

An operation tries to save a data object:


 var obj = {
                "price":-10.75
                "model": "FS2USB42",
                "name": "USB 3.0 Adapter"
            };
 context.model("Product").save(obj).then(function() {
               return done();
           }).catch(function(err) {
               return done(err);
           });
 

and the result is:


 {
    "code": "EPATTERN",
    "model": "Product",
    "field": "price",
    "message": "The value should be a number greater or equal to zero."
}
 

Constructor

new DataTypeValidator(type)

Parameters:
Name Type Description
type string | * The data type which is going to be used for data validation
Properties:
Name Type Description
dataType * Gets or sets the data type which is going to be used for data validation
Source:

Extends

Members

dataType :Object|*

Type:
  • Object | *
Source:

Methods

getContext() → {DataContext|*}

Gets the current data context, if any.
Inherited From:
Source:
Returns:
Type
DataContext | *

setContext(context)

Sets the current data context.
Parameters:
Name Type Description
context DataContext | *
Inherited From:
Source: