Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 4x 4x 6x 34x 4x | import { ValidationContext } from "../../../../utils/validation/Interfaces";
import Validator from "../../../../utils/validation/validators/Validator";
import { CustomElementPropertyMetadata } from "../../../interfaces";
export const validationEvent = 'validationEvent';
const ValidatableMixin = Base =>
class Validatable extends Base {
static get properties(): Record<string, CustomElementPropertyMetadata> {
return {
validators: {
type: Array,
mutable: true,
value: [],
transform: function (value) {
return this.initializeValidators(value);
}
}
};
}
/**
* Validates a validatable object
* @returns true is the value is valid, false otherwise
*/
validate(): boolean {
if (this.validators.length === 0) {
return true; // Nothing to validate
}
// Create a new validation context
const context: ValidationContext = this.createValidationContext();
// Validate
this.validators.forEach((validator: Validator) => validator.validate(context));
const {
warnings,
errors
} = context;
// Dispatch the event even if there are no errors to trigger a repaint
this.dispatchCustomEvent(validationEvent, {
warnings,
errors
});
return errors.length === 0;
}
initializeValidators(validators: (Validator | string)[]): Validator[] {
for (let i = 0; i < validators.length; ++i) {
const validator = validators[i];
if (typeof validator === 'string') {
validators[i] = this.initializeValidator(validator);
}
}
return validators as Validator[];
}
};
export default ValidatableMixin; |