| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- /**
- * DevExtreme (ui/validation_engine.js)
- * Version: 19.1.16
- * Build date: Tue Oct 18 2022
- *
- * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
- * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
- */
- "use strict";
- var Class = require("../core/class");
- var extend = require("../core/utils/extend").extend;
- var inArray = require("../core/utils/array").inArray;
- var each = require("../core/utils/iterator").each;
- var EventsMixin = require("../core/events_mixin");
- var errors = require("../core/errors");
- var commonUtils = require("../core/utils/common");
- var typeUtils = require("../core/utils/type");
- var numberLocalization = require("../localization/number");
- var messageLocalization = require("../localization/message");
- var BaseRuleValidator = Class.inherit({
- NAME: "base",
- defaultMessage: function(value) {
- return messageLocalization.getFormatter("validation-" + this.NAME)(value)
- },
- defaultFormattedMessage: function(value) {
- return messageLocalization.getFormatter("validation-" + this.NAME + "-formatted")(value)
- },
- _isValueEmpty: function(value) {
- return !rulesValidators.required.validate(value, {})
- },
- validate: function(value, rule) {
- var valueArray = Array.isArray(value) ? value : [value];
- var result = true;
- if (valueArray.length) {
- valueArray.every(function(itemValue) {
- result = this._validate(itemValue, rule);
- return result
- }, this)
- } else {
- result = this._validate(null, rule)
- }
- return result
- }
- });
- var RequiredRuleValidator = BaseRuleValidator.inherit({
- NAME: "required",
- _validate: function(value, rule) {
- if (!typeUtils.isDefined(value)) {
- return false
- }
- if (false === value) {
- return false
- }
- value = String(value);
- if (rule.trim || !typeUtils.isDefined(rule.trim)) {
- value = value.trim()
- }
- return "" !== value
- }
- });
- var NumericRuleValidator = BaseRuleValidator.inherit({
- NAME: "numeric",
- _validate: function(value, rule) {
- if (false !== rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- if (rule.useCultureSettings && typeUtils.isString(value)) {
- return !isNaN(numberLocalization.parse(value))
- } else {
- return typeUtils.isNumeric(value)
- }
- }
- });
- var RangeRuleValidator = BaseRuleValidator.inherit({
- NAME: "range",
- _validate: function(value, rule) {
- if (false !== rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- var validNumber = rulesValidators.numeric.validate(value, rule);
- var validValue = typeUtils.isDefined(value) && "" !== value;
- var number = validNumber ? parseFloat(value) : validValue && value.valueOf();
- var min = rule.min;
- var max = rule.max;
- if (!(validNumber || typeUtils.isDate(value)) && !validValue) {
- return false
- }
- if (typeUtils.isDefined(min)) {
- if (typeUtils.isDefined(max)) {
- return number >= min && number <= max
- }
- return number >= min
- } else {
- if (typeUtils.isDefined(max)) {
- return number <= max
- } else {
- throw errors.Error("E0101")
- }
- }
- }
- });
- var StringLengthRuleValidator = BaseRuleValidator.inherit({
- NAME: "stringLength",
- _validate: function(value, rule) {
- value = typeUtils.isDefined(value) ? String(value) : "";
- if (rule.trim || !typeUtils.isDefined(rule.trim)) {
- value = value.trim()
- }
- if (rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- return rulesValidators.range.validate(value.length, extend({}, rule))
- }
- });
- var CustomRuleValidator = BaseRuleValidator.inherit({
- NAME: "custom",
- validate: function(value, rule) {
- if (rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- var validator = rule.validator;
- var dataGetter = validator && typeUtils.isFunction(validator.option) && validator.option("dataGetter");
- var data = typeUtils.isFunction(dataGetter) && dataGetter();
- var params = {
- value: value,
- validator: validator,
- rule: rule
- };
- if (data) {
- params.data = data
- }
- return rule.validationCallback(params)
- }
- });
- var CompareRuleValidator = BaseRuleValidator.inherit({
- NAME: "compare",
- _validate: function(value, rule) {
- if (!rule.comparisonTarget) {
- throw errors.Error("E0102")
- }
- if (rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- extend(rule, {
- reevaluate: true
- });
- var otherValue = rule.comparisonTarget();
- var type = rule.comparisonType || "==";
- switch (type) {
- case "==":
- return value == otherValue;
- case "!=":
- return value != otherValue;
- case "===":
- return value === otherValue;
- case "!==":
- return value !== otherValue;
- case ">":
- return value > otherValue;
- case ">=":
- return value >= otherValue;
- case "<":
- return value < otherValue;
- case "<=":
- return value <= otherValue
- }
- }
- });
- var PatternRuleValidator = BaseRuleValidator.inherit({
- NAME: "pattern",
- _validate: function(value, rule) {
- if (false !== rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- var pattern = rule.pattern;
- if (typeUtils.isString(pattern)) {
- pattern = new RegExp(pattern)
- }
- return pattern.test(value)
- }
- });
- var EmailRuleValidator = BaseRuleValidator.inherit({
- NAME: "email",
- _validate: function(value, rule) {
- if (false !== rule.ignoreEmptyValue && this._isValueEmpty(value)) {
- return true
- }
- return rulesValidators.pattern.validate(value, extend({}, rule, {
- pattern: /^[\d\w._-]+@[\d\w._-]+\.[\w]+$/i
- }))
- }
- });
- var rulesValidators = {
- required: new RequiredRuleValidator,
- numeric: new NumericRuleValidator,
- range: new RangeRuleValidator,
- stringLength: new StringLengthRuleValidator,
- custom: new CustomRuleValidator,
- compare: new CompareRuleValidator,
- pattern: new PatternRuleValidator,
- email: new EmailRuleValidator
- };
- var GroupConfig = Class.inherit({
- ctor: function(group) {
- this.group = group;
- this.validators = []
- },
- validate: function() {
- var result = {
- isValid: true,
- brokenRules: [],
- validators: []
- };
- each(this.validators, function(_, validator) {
- var validatorResult = validator.validate();
- result.isValid = result.isValid && validatorResult.isValid;
- if (validatorResult.brokenRule) {
- result.brokenRules.push(validatorResult.brokenRule)
- }
- result.validators.push(validator)
- });
- this.fireEvent("validated", [{
- validators: result.validators,
- brokenRules: result.brokenRules,
- isValid: result.isValid
- }]);
- return result
- },
- reset: function() {
- each(this.validators, function(_, validator) {
- validator.reset()
- })
- }
- }).include(EventsMixin);
- var ValidationEngine = {
- groups: [],
- getGroupConfig: function(group) {
- var result = commonUtils.grep(this.groups, function(config) {
- return config.group === group
- });
- if (result.length) {
- return result[0]
- }
- },
- initGroups: function() {
- this.groups = [];
- this.addGroup()
- },
- addGroup: function(group) {
- var config = this.getGroupConfig(group);
- if (!config) {
- config = new GroupConfig(group);
- this.groups.push(config)
- }
- return config
- },
- removeGroup: function(group) {
- var config = this.getGroupConfig(group);
- var index = inArray(config, this.groups);
- if (index > -1) {
- this.groups.splice(index, 1)
- }
- return config
- },
- _setDefaultMessage: function(rule, validator, name) {
- if (!typeUtils.isDefined(rule.message)) {
- if (validator.defaultFormattedMessage && typeUtils.isDefined(name)) {
- rule.message = validator.defaultFormattedMessage(name)
- } else {
- rule.message = validator.defaultMessage()
- }
- }
- },
- validate: function(value, rules, name) {
- var result = {
- name: name,
- value: value,
- brokenRule: null,
- isValid: true,
- validationRules: rules
- };
- var that = this;
- each(rules || [], function(_, rule) {
- var ruleValidator = rulesValidators[rule.type];
- var ruleValidationResult;
- if (ruleValidator) {
- if (typeUtils.isDefined(rule.isValid) && rule.value === value && !rule.reevaluate) {
- if (!rule.isValid) {
- result.isValid = false;
- result.brokenRule = rule;
- return false
- }
- return true
- }
- rule.value = value;
- ruleValidationResult = ruleValidator.validate(value, rule);
- rule.isValid = ruleValidationResult;
- if (!ruleValidationResult) {
- result.isValid = false;
- that._setDefaultMessage(rule, ruleValidator, name);
- result.brokenRule = rule
- }
- if (!rule.isValid) {
- return false
- }
- } else {
- throw errors.Error("E0100")
- }
- });
- return result
- },
- registerValidatorInGroup: function(group, validator) {
- var groupConfig = ValidationEngine.addGroup(group);
- if (inArray(validator, groupConfig.validators) < 0) {
- groupConfig.validators.push(validator)
- }
- },
- _shouldRemoveGroup: function(group, validatorsInGroup) {
- var isDefaultGroup = void 0 === group;
- var isValidationGroupInstance = group && "dxValidationGroup" === group.NAME;
- return !isDefaultGroup && !isValidationGroupInstance && !validatorsInGroup.length
- },
- removeRegisteredValidator: function(group, validator) {
- var config = ValidationEngine.getGroupConfig(group);
- var validatorsInGroup = config && config.validators;
- var index = inArray(validator, validatorsInGroup);
- if (index > -1) {
- validatorsInGroup.splice(index, 1);
- if (this._shouldRemoveGroup(group, validatorsInGroup)) {
- this.removeGroup(group)
- }
- }
- },
- validateGroup: function(group) {
- var groupConfig = ValidationEngine.getGroupConfig(group);
- if (!groupConfig) {
- throw errors.Error("E0110")
- }
- return groupConfig.validate()
- },
- resetGroup: function(group) {
- var groupConfig = ValidationEngine.getGroupConfig(group);
- if (!groupConfig) {
- throw errors.Error("E0110")
- }
- return groupConfig.reset()
- }
- };
- ValidationEngine.initGroups();
- module.exports = ValidationEngine;
- module.exports.default = module.exports;
|