Module: x2node-validators

Record validation/normalization module.
Implements:

Requires

Classes

ValidationErrors

Mixins

PropertyDescriptorWithValidators
RecordTypeDescriptorWithValidators

Methods


<static> addDefaultValidators(desc, validators)

Can be used by record types library extensions to add validators to the default set of validators on a record type or property descriptor. Note that the validators module installs validators on properties and record types in an onContainerComplete handler, so extensions must call this function before that.
Parameters:
Name Type Description
desc module:x2node-records~RecordTypeDescriptor | module:x2node-records~PropertyDescriptor The descriptor. May not be a view.
validators Object.<string, Array.<(string|Array)>> Validator specifications as would be provided on the definition.

<static> createValidationErrors()

Create new, empty validation errors object.
Returns:
Validation errors object.
Type
module:x2node-validators~ValidationErrors

<static> dep(depPtrs, validatorFunc)

Create validator function that checks that specified properties in the context do not have any errors.
Parameters:
Name Type Description
depPtrs Array.<string> JSON pointers for properties, none of which should have errors for the validator to be invoked.
validatorFunc module:x2node-validators.curriedValidator The validator function to invoke if all dependency properties are valid. The returned value is ignored.
Returns:
Single-element array with the resulting validator function.
Type
Array.<module:x2node-validators.validator>

<static> isSupported(obj)

Tell if the provided object is supported by the module. Currently, only a record types library instance can be tested using this function and it tells if the library was constructed with the x2node-validators extension.
Parameters:
Name Type Description
obj * Object to test.
Returns:
true if supported by the validators module.
Type
boolean

<static> isValidationErrors(obj)

Tell if the provided object is a validation errors object.
Parameters:
Name Type Description
obj * The object to test.
Returns:
true if instance of ValidationErrors.
Type
boolean

<static> listpat(list)

Create regular expression for a list of valid values. Useful with the "pattern" standard validator.
Parameters:
Name Type Description
list Array.<string> List of valid values.
Returns:
The resular expression.
Type
RegExp

<static> normalizeRecord(recordType, recordTypeName, record [, lang] [, validationSets])

Validate and normalize the specified record.
Parameters:
Name Type Argument Description
recordType module:x2node-records~RecordTypesLibrary Record types library.
recordTypeName string Record type name.
record Object The record to validate. May not be null or undefined.
lang string <optional>
Language for the error messages in the same format as used by the HTTP's "Accept-Language" request header. If not provided, "*" is assumed.
validationSets string <optional>
Comma-separated validation set names. If not provided, the default validation set is used.
Throws:
If unknown record type, record was not provided or invalid language or validation set specification.
Type
module:x2node-common.X2UsageError
Returns:
Errors if the record is invalid, or null if it has been successfully validated and normalized.
Type
module:x2node-validators~ValidationErrors

<static> registerValidationErrorMessage(ctx, messageId, messageDef)

Can be used by record types library extensions to register additional validation error messages available to the library. Depending on where the function is called the registred message scope is determined. For example, if called in the extention's extendRecordTypesLibrary() method, the message is registred for the whole library.
Parameters:
Name Type Description
ctx module:x2node-records~LibraryConstructionContext Library construction context.
messageId string Message identifier.
messageDef Object.<string, string> Message definition. The keys are language codes, the values are message templates.

<static> registerValidator(ctx, validatorId, Validator)

Can be used by record types library extensions to register additional validators available to the library. Depending on where the function is called the registred validator scope is determined. For example, if called in the extention's extendRecordTypesLibrary() method, the validator is registred for the whole library.
Parameters:
Name Type Description
ctx module:x2node-records~LibraryConstructionContext Library construction context.
validatorId string Validator id.
Validator module:x2node-validators.validator function.

<static> replaceDefaultValidators(desc, validators)

Can be used by record types library extensions to replace the default set of validators on a record type or property descriptor. Note that the validators module installs validators on properties and record types in an onContainerComplete handler, so extensions must call this function before that.
Parameters:
Name Type Description
desc module:x2node-records~RecordTypeDescriptor | module:x2node-records~PropertyDescriptor The descriptor. May not be a view.
validators Object.<string, Array.<(string|Array)>> Validator specifications as would be provided on the definition.

Type Definitions


curriedValidator(ctx, value)

Validator/normalizer function curried with the parameters.
Parameters:
Name Type Description
ctx module:x2node-validators~ValidationContext Current validation context.
value * The value to validate/normalize.
Returns:
Normalized value, which, if different from the current value, is set back into the record object.
Type
*

validator( [params], ctx, value)

Validator/normalizer function.
Parameters:
Name Type Argument Description
params Array <optional>
Validator parameters from the subject definition.
ctx module:x2node-validators~ValidationContext Current validation context.
value * The value to validate/normalize.
Returns:
Normalized value, which, if different from the current value, is set back into the record object.
Type
*