UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

76 lines (63 loc) 3.98 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _types; import { isMoment } from 'moment'; import isEmpty from '../../_util/isEmpty'; import ValidationResult from '../ValidationResult'; import { $l } from '../../locale-context'; import { FieldType } from '../../data-set/enum'; import { toRangeValue } from '../../field/utils'; /* eslint-disable */ var emailReg = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/; var urlReg = /^(?:(?:https?|HTTPS?|ftp|FTP):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)(?:\.(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)*)(?::\d{2,5})?(?:[\/?#]\S*)?$/; var colorRgbaReg = /^[rR][gG][Bb][Aa]?\((\s*(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\s*,){2}\s*(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\s*,?\s*(0?\.\d{1,2}|1|0)?\s*\){1}$/; var colorHexReg = /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/; /* eslint-enable */ var isNotDateIf = function isNotDateIf(value, range) { if (range) { return toRangeValue(value, range).some(function (item) { return !isEmpty(item) && isMoment(item) && !item.isValid(); }); } return isMoment(value) && !value.isValid(); }; var isNotDate = function isNotDate(value, range) { if (range) { return toRangeValue(value, range).some(function (item) { return !isEmpty(item) && (!isMoment(item) || !item.isValid()); }); } return !isMoment(value) || !value.isValid(); }; var types = (_types = {}, _defineProperty(_types, FieldType.email, [function (value) { return !emailReg.test(value); }, 'EmailField']), _defineProperty(_types, FieldType.url, [function (value) { return !urlReg.test(value); }, 'UrlField']), _defineProperty(_types, FieldType.color, [function (value) { return !(colorRgbaReg.test(value) || colorHexReg.test(value)); }, 'ColorPicker']), _defineProperty(_types, FieldType.date, [isNotDate, 'DatePicker']), _defineProperty(_types, FieldType.dateTime, [isNotDate, 'DatePicker']), _defineProperty(_types, FieldType.week, [isNotDate, 'DatePicker']), _defineProperty(_types, FieldType.month, [isNotDate, 'DatePicker']), _defineProperty(_types, FieldType.year, [isNotDate, 'DatePicker']), _defineProperty(_types, FieldType.time, [isNotDate, 'DatePicker']), _defineProperty(_types, FieldType.auto, [isNotDateIf, 'DatePicker']), _types); export default function typeMismatch(value, props) { var type = props.type, defaultValidationMessages = props.defaultValidationMessages, range = props.range; if (!isEmpty(value) && type) { var validateType = types[type]; if (validateType) { var _validateType = _slicedToArray(validateType, 2), validate = _validateType[0], component = _validateType[1]; if (validate(value, range)) { var ruleName = 'typeMismatch'; var _defaultValidationMes = defaultValidationMessages[ruleName], validationMessage = _defaultValidationMes === void 0 ? $l(component, 'type_mismatch') : _defaultValidationMes; return new ValidationResult({ validationMessage: validationMessage, value: value, ruleName: ruleName }); } } } return true; } //# sourceMappingURL=typeMismatch.js.map