UNPKG

redux-form-validators

Version:

Simple validations with redux-form / react-final-form

204 lines (162 loc) 6.54 kB
"use strict"; var _assert = _interopRequireDefault(require("assert")); var _index = _interopRequireWildcard(require("../index")); var _helper = _interopRequireDefault(require("./helper")); var _react = _interopRequireDefault(require("react")); var _reactIntl = require("react-intl"); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } /* eslint-disable no-unused-vars */ /* eslint-enable no-unused-vars */ function test(key, msg, func, value) { var params = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; params[key] = msg; return (0, _helper["default"])(func(params)(value)); } describe('Validator option: message', function () { it('should return a custom message', function () { var blank = ''; ['msg', 'message'].forEach(function (key) { _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.absence, 'foo')); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.acceptance)); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.confirmation, 'foo', { field: 'bar' })); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.email, blank)); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.date, blank, { format: 'mm/dd/yyyy' })); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.exclusion, blank, { "in": [blank] })); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.file)); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.format, blank, { "with": /^foo$/ })); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.inclusion, blank, { "in": [] })); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.length, blank, { is: 300 })); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.numericality, blank)); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.presence, blank)); _assert["default"].strictEqual('foobar', test(key, 'foobar', _index.url, blank)); _assert["default"].strictEqual('foobar', test(key, { id: 'foobar', defaultMessage: 'foo' }, _index.presence, blank)); }); }); it('should accept different message formats', function () { var blank = ''; ['msg', 'message'].forEach(function (key) { // React Intl element _assert["default"].strictEqual('foobar', test(key, _react["default"].createElement(_reactIntl.FormattedMessage, { id: "foobar" }), _index.absence, 'foo')); // Other formats _assert["default"].strictEqual('foobar', test(key, { absence: 'foobar' }, _index.absence, 'foo')); _assert["default"].strictEqual('foobar', test(key, { format: 'foobar' }, _index.date, blank, { format: 'mm/dd/yyyy' })); _assert["default"].strictEqual('foobar', test(key, { range: 'foobar' }, _index.date, '2000-01-01', { '<': new Date(1999, 0, 1) })); _assert["default"].strictEqual('foobar', test(key, { '<': 'foobar', range: 'foo' }, _index.date, '2000-01-01', { '<': new Date(1999, 0, 1) })); _assert["default"].strictEqual('foobar', test(key, { absence: { id: 'foobar' } }, _index.absence, 'foo')); _assert["default"].strictEqual('foobar', test(key, { wrongLength: { id: 'foobar' }, tooShort: { id: 'min' } }, _index.length, blank, { is: 300 })); _assert["default"].strictEqual('foobar', test(key, { wrongLength: { id: 'is' }, tooShort: { id: 'foobar' } }, _index.length, blank, { min: 1 })); _assert["default"].strictEqual('foobar', test(key, { wrongLength: 'foobar', tooShort: 'min' }, _index.length, blank, { is: 300 })); _assert["default"].strictEqual('foobar', test(key, { wrongLength: 'is', tooShort: 'foobar' }, _index.length, blank, { min: 1 })); }); }); it('should fallback to default message', function () { var blank = ''; ['msg', 'message'].forEach(function (key) { _assert["default"].strictEqual('form.errors.tooShort', test(key, { wrongLength: { id: 'is' } }, _index.length, blank, { min: 1 })); _assert["default"].strictEqual('form.errors.tooShort', test(key, { wrongLength: { id: 'is' } }, _index.length, blank, { is: 0, min: 1 })); }); }); it('should override default messages', function () { var formatMessage = _index["default"].formatMessage; _index["default"].formatMessage = global.ValidatorsFormatMessage; var defaultMessages = _index["default"].messages; _assert["default"].strictEqual(defaultMessages.presence.defaultMessage, (0, _index.presence)()('')); _index["default"].messages.presence = { id: 'form.errors.presence', defaultMessage: 'is mandatory' }; _assert["default"].strictEqual('is mandatory', (0, _index.presence)()('')); var tooShort = _index["default"].messages.tooShort; _index["default"].messages.tooShort = 'is too short: {count} chars expected'; _assert["default"].strictEqual('is too short: 4 chars expected', (0, _index.length)({ min: 4 })('')); _index["default"].messages.tooShort = tooShort; Object.assign(_index["default"].messages, { presence: { id: 'form.errors.presence', defaultMessage: 'is missing' } }); _assert["default"].strictEqual('is missing', (0, _index.presence)()('')); _index["default"].messages = defaultMessages; _index["default"].formatMessage = formatMessage; }); });