@c8y/ngx-components
Version:
Angular modules for Cumulocity IoT applications
74 lines • 2.84 kB
TypeScript
import { AsyncValidatorFn, ValidatorFn } from '@angular/forms';
export declare const ARRAY_VALIDATION_PREFIX = "INNER_ARRAY_ERROR_";
export type ArrayValidationErrors = {
[key: string]: ArrayValidationErrorValue;
};
export type ArrayValidationErrorValue = {
isArrayError: true;
originalKey: string;
entries: Array<{
index: number;
errorData: unknown;
}>;
};
/**
* This method can alter how validators work. It can be used in cases where there is need to validate FormControl
* that as value, takes array of elements. This allows to reuse validators, that works for such elements.
* For example if Validators.minLength(3) is used on control that takes string as value, validator will return error
* if string is less than 3 characters. If same is done on array of strings, validator will return error if array
* has less than 3 elements.
*
* validateArrayElements(Validators.minLength(3)) method makes it possible to check every member of array if it has minimum 3 characters.
* If there are elements that raise error, it will be possible to extract these errors using item index.
*
* As a example, this can be used for custom form elements, which manipulates array of elements,
* and there is need to display corresponding error messages for every array member that is displayed, while still using
* Angular's Validation approach.
*
* Errors that are generated by this helper are prefixed with 'INNER_ARRAY_ERROR_'. Reason for this is to prevent overwriting normal validation
* errors that could have same key
*
* @Example
*
* let control = new FormControl(['1234', '123456', '123', [Validators.minLength(5), validateArrayElements(Validators.minLength(5))] ])
*
* this FormControl has validation errors:
*
* {
* "INNER_ARRAY_ERROR_minlength": {
* isArrayError: true,
* originalKey: "minlength",
* entries: [
* {
* index: 0,
* errorData: {
* requiredLength: 5,
* actualLength: 4
* }
* },
* {
* index: 2,
* errorData: {
* requiredLength: 5,
* actualLength: 3
* }
* },
* ]
* },
* "minlength": {
* requiredLength: 5,
* actualLength: 3
* }
* }
*
* @param validator A standard Angular synchronous validator function.
*/
export declare function validateArrayElements(validator: ValidatorFn): ValidatorFn;
/**
* Async version of validateArrayElements.
*
* Note that Angular by design doesn't execute async validators if synchronous validators returns any error.
* @param validator A standard Angular asynchronous validator function.
*/
export declare function asyncValidateArrayElements(validator: AsyncValidatorFn): AsyncValidatorFn;
//# sourceMappingURL=validate-array-elements.d.ts.map