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.
Type | Description |
NegativeInteger | An integer containing only negative values (..,-2,-1) |
NegativeNumber | A number containing only negative values (..,-2,-1) |
NonNegativeInteger | An integer containing only non-negative values (0,1,2,..) |
NonNegativeNumber | An number containing only non-negative values (0,1,2,..) |
NonPositiveInteger | An integer containing only non-positive values (..,-2,-1,0) |
NonPositiveNumber | A number containing only non-positive values (..,-2,-1,0) |
PositiveInteger | An integer containing only positive values (1,2,..) |
PositiveNumber | A number containing only positive values (0.1,+1,2,..) |
Float | Float data type is a single-precision floating point. |
Email | A string which represents an email address (e.g. user@example.com) |
Guid | A string which represents a global unique identifier (e.g. 21EC2020-3AEA-4069-A2DD-08002B30309D). |
AbsoluteURI | A string which represents an absolute URI address (e.g. https://www.example.com/help?article=1001) |
RelativeURI | A string which represents a relative URI address (e.g. /help?article=1001) |
Time | A string which represents an instant of time that recurs every day (e.g. 13:20:45) |
Date | Represents a date value. |
DateTime | Represents a date and time value. |
Duration | A string which represents a duration of time (e.g. P1Y1M10D, P10D, -P0Y1M10D2H15M30S etc) |
IP | A 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."
}