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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 | 1×
1×
1×
1×
1×
46×
46×
46×
1×
1×
1×
1×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| import assert from 'assert'
import Validators, {
absence,
date,
acceptance,
confirmation,
email,
exclusion,
file,
format,
inclusion,
length,
numericality,
presence,
url
} from '../index'
import getErrorId from './helper'
/* eslint-disable no-unused-vars */
import React from 'react'
import { FormattedMessage } from 'react-intl'
/* eslint-enable no-unused-vars */
function test (key, msg, func, value, params = {}) {
params[key] = msg
return getErrorId(func(params)(value))
}
describe('Validator option: message', function () {
it('should return a custom message', function () {
let blank = ''
;['msg', 'message'].forEach(function (key) {
assert.equal('foobar', test(key, 'foobar', absence, 'foo'))
assert.equal('foobar', test(key, 'foobar', acceptance))
assert.equal('foobar', test(key, 'foobar', confirmation, 'foo', { field: 'bar' }))
assert.equal('foobar', test(key, 'foobar', email, blank))
assert.equal('foobar', test(key, 'foobar', date, blank, { format: 'mm/dd/yyyy' }))
assert.equal('foobar', test(key, 'foobar', exclusion, blank, { in: [blank] }))
assert.equal('foobar', test(key, 'foobar', file))
assert.equal('foobar', test(key, 'foobar', format, blank, { with: /^foo$/ }))
assert.equal('foobar', test(key, 'foobar', inclusion, blank, { in: [] }))
assert.equal('foobar', test(key, 'foobar', length, blank, { is: 300 }))
assert.equal('foobar', test(key, 'foobar', numericality, blank))
assert.equal('foobar', test(key, 'foobar', presence, blank))
assert.equal('foobar', test(key, 'foobar', url, blank))
assert.equal('foobar', test(key, { id: 'foobar', defaultMessage: 'foo' }, presence, blank))
})
})
it('should accept different message formats', function () {
let blank = ''
;['msg', 'message'].forEach(function (key) {
// React Intl element
assert.equal('foobar', test(key, <FormattedMessage id="foobar" />, absence, 'foo'))
// Other formats
assert.equal('foobar', test(key, { absence: 'foobar' }, absence, 'foo'))
assert.equal('foobar', test(key, { absence: { id: 'foobar' } }, absence, 'foo'))
assert.equal(
'foobar',
test(key, { wrongLength: { id: 'foobar' }, tooShort: { id: 'min' } }, length, blank, { is: 300 })
)
assert.equal(
'foobar',
test(key, { wrongLength: { id: 'is' }, tooShort: { id: 'foobar' } }, length, blank, { min: 1 })
)
assert.equal('foobar', test(key, { wrongLength: 'foobar', tooShort: 'min' }, length, blank, { is: 300 }))
assert.equal('foobar', test(key, { wrongLength: 'is', tooShort: 'foobar' }, length, blank, { min: 1 }))
})
})
it('should fallback to default message', function () {
let blank = ''
;['msg', 'message'].forEach(function (key) {
assert.equal('form.errors.tooShort', test(key, { wrongLength: { id: 'is' } }, length, blank, { min: 1 }))
assert.equal('form.errors.tooShort', test(key, { wrongLength: { id: 'is' } }, length, blank, { is: 0, min: 1 }))
})
})
it('should override default messages', function () {
let formatMessage = Validators.formatMessage
Validators.formatMessage = global.ValidatorsFormatMessage
let defaultMessages = Validators.messages
assert.equal(defaultMessages.presence.defaultMessage, presence()(''))
Validators.messages.presence = {
id: 'form.errors.presence',
defaultMessage: 'is mandatory'
}
assert.equal('is mandatory', presence()(''))
Validators.messages.tooShort = 'is too short: {count} chars expected'
assert.equal('is too short: 4 chars expected', length({ min: 4 })(''))
Object.assign(Validators.messages, {
presence: {
id: 'form.errors.presence',
defaultMessage: 'is missing'
}
})
assert.equal('is missing', presence()(''))
Validators.messages = defaultMessages
Validators.formatMessage = formatMessage
})
})
|