MaxLengthValidator

MaxLengthValidator

Validates a variable which has a length property (e.g. a string) against the maximum length provided

MaxLengthValidator is used by DataValidatorListener for validating data objects.

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


 {
    "name": "model",
    "title": "Model",
    "description": "The model of the product. Use with the URL of a ProductModel or a textual representation of the model identifier. The URL of the ProductModel can be from an external source. It is recommended to additionally provide strong product identifiers via the gtin8/gtin13/gtin14 and mpn properties.",
    "type": "Text",
    "validation": {
        maxLength:8
    }
}
 

An operation tries to save a data object:


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

Result:


 {
    "code": "EMAXLEN",
    "model": "Product",
    "field": "model",
    "message": "The value is too long. It should have 8 characters or fewer."
}
 

Note:If validation.maxLength is missing and data model's attribute has a defined size then the operation will try to validate data object against this size.


 {
    "name": "model",
    "title": "Model",
    "description": "The model of the product. Use with the URL of a ProductModel or a textual representation of the model identifier. The URL of the ProductModel can be from an external source. It is recommended to additionally provide strong product identifiers via the gtin8/gtin13/gtin14 and mpn properties.",
    "type": "Text",
    "size":8
}
 

Constructor

new MaxLengthValidator(length)

Parameters:
Name Type Description
length number A number which represents the maximum length
Properties:
Name Type Description
maxLength number Gets or sets an integer which represents the maximum length.
message string Gets or sets a string which represents a custom validator message.
Source:

Extends

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:

validateSync(val) → {Object|undefined|*}

Validates the given value. If validation fails, the operation will return a validation result.
Parameters:
Name Type Description
val *
Source:
Returns:
Type
Object | undefined | *