devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
159 lines (158 loc) • 6.28 kB
JavaScript
/**
* DevExtreme (esm/__internal/integration/knockout/validation.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["id", "complete"];
import {
EventsStrategy
} from "../../core/events_strategy";
import {
Guid
} from "../../core/guid";
import {
Deferred
} from "../../core/utils/deferred";
import {
extend
} from "../../core/utils/m_extend";
import {
each,
map
} from "../../core/utils/m_iterator";
import ValidationEngine from "../../ui/validation_engine";
import ko from "knockout";
const VALIDATION_STATUS_VALID = "valid";
const VALIDATION_STATUS_PENDING = "pending";
export class KoDxValidator {
constructor(target, _ref) {
let {
name: name,
validationRules: validationRules
} = _ref;
this.target = target;
this.name = name;
this.isValid = ko.observable(true);
this.validationError = ko.observable();
this.validationErrors = ko.observable();
this.validationStatus = ko.observable("valid");
this._eventsStrategy = new EventsStrategy(this);
this.validationRules = map(validationRules, (rule, index) => extend({}, rule, {
validator: this,
index: index
}));
this._validationInfo = {
result: null,
deferred: null
}
}
_updateValidationResult(result) {
const {
result: currentResult
} = this._validationInfo;
if (!currentResult || currentResult.id !== result.id) {
const complete = this._validationInfo.deferred && (null === currentResult || void 0 === currentResult ? void 0 : currentResult.complete);
const updatedResult = extend({}, result, {
complete: complete
});
this._validationInfo.result = updatedResult;
return updatedResult
}
const restResultProperties = _objectWithoutPropertiesLoose(result, _excluded);
Object.assign(currentResult, restResultProperties);
return currentResult
}
validate() {
var _this$_validationInfo, _result$complete;
const currentResult = null === (_this$_validationInfo = this._validationInfo) || void 0 === _this$_validationInfo ? void 0 : _this$_validationInfo.result;
const value = this.target();
if ("pending" === (null === currentResult || void 0 === currentResult ? void 0 : currentResult.status) && currentResult.value === value) {
return extend({}, currentResult)
}
const result = ValidationEngine.validate(value, this.validationRules, this.name);
result.id = (new Guid).toString();
this._applyValidationResult(result);
null === result || void 0 === result || null === (_result$complete = result.complete) || void 0 === _result$complete || _result$complete.then(res => {
var _this$_validationInfo2;
if (res.id === (null === (_this$_validationInfo2 = this._validationInfo.result) || void 0 === _this$_validationInfo2 ? void 0 : _this$_validationInfo2.id)) {
this._applyValidationResult(res)
}
});
return extend({}, this._validationInfo.result)
}
reset() {
this.target(null);
const result = {
id: null,
isValid: true,
brokenRule: null,
pendingRules: null,
status: "valid",
complete: null
};
this._applyValidationResult(result);
return result
}
_applyValidationResult(result) {
result.validator = this;
const currentResult = this._updateValidationResult(result);
const {
dxValidator: dxValidator
} = this.target;
null === dxValidator || void 0 === dxValidator || dxValidator.isValid(currentResult.isValid);
null === dxValidator || void 0 === dxValidator || dxValidator.validationError(currentResult.brokenRule);
null === dxValidator || void 0 === dxValidator || dxValidator.validationErrors(currentResult.brokenRules);
null === dxValidator || void 0 === dxValidator || dxValidator.validationStatus(currentResult.status);
if ("pending" === result.status) {
if (!this._validationInfo.deferred) {
this._validationInfo.deferred = Deferred();
currentResult.complete = this._validationInfo.deferred.promise()
}
this._eventsStrategy.fireEvent("validating", [currentResult]);
return
}
this._eventsStrategy.fireEvent("validated", [result]);
if (this._validationInfo.deferred) {
this._validationInfo.deferred.resolve(result);
this._validationInfo.deferred = null
}
}
on(eventName, eventHandler) {
this._eventsStrategy.on(eventName, eventHandler);
return this
}
off(eventName, eventHandler) {
this._eventsStrategy.off(eventName, eventHandler);
return this
}
}
const hasDxValidator = member => ko.isObservable(member) && "dxValidator" in member && !!member.dxValidator;
if (ko) {
ko.extenders.dxValidator = (target, option) => {
const validator = new KoDxValidator(target, option);
target.dxValidator = validator;
target.subscribe(validator.validate.bind(validator));
return target
};
Object.assign(ValidationEngine, {
registerModelForValidation(model) {
each(model, (_, member) => {
if (hasDxValidator(member)) {
ValidationEngine.registerValidatorInGroup(model, member.dxValidator)
}
})
},
unregisterModelForValidation(model) {
each(model, (_, member) => {
if (hasDxValidator(member)) {
ValidationEngine.removeRegisteredValidator(model, member.dxValidator)
}
})
},
validateModel: ValidationEngine.validateGroup
})
}