co.validation 2.2.1 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -292,6 +292,23 @@ describe('createValidation', () => {
292
292
  errors: undefined
293
293
  });
294
294
  });
295
+ it('supports maxLength with emoji', () => {
296
+ expect((0, _.createValidation)({
297
+ type: 'maxLength',
298
+ max: 4,
299
+ getMessage: max => max
300
+ })('❤️123')).toEqual({
301
+ data: '❤️123',
302
+ errors: undefined
303
+ });
304
+ expect((0, _.createValidation)({
305
+ type: 'maxLength',
306
+ max: 3
307
+ })('❤️123')).toEqual({
308
+ data: undefined,
309
+ errors: ['Must be 3 characters or less']
310
+ });
311
+ });
295
312
  it('supports minLength', () => {
296
313
  expect((0, _.createValidation)({
297
314
  type: 'minLength',
@@ -325,6 +342,30 @@ describe('createValidation', () => {
325
342
  errors: ['Must be at least 3 characters']
326
343
  });
327
344
  });
345
+ it('supports minLength with emoji', () => {
346
+ expect((0, _.createValidation)({
347
+ type: 'minLength',
348
+ min: 3,
349
+ getMessage: min => min
350
+ })('❤️')).toEqual({
351
+ data: undefined,
352
+ errors: ['3']
353
+ });
354
+ expect((0, _.createValidation)({
355
+ type: 'minLength',
356
+ min: 3
357
+ })('❤️1')).toEqual({
358
+ data: undefined,
359
+ errors: ['Must be at least 3 characters']
360
+ });
361
+ expect((0, _.createValidation)({
362
+ type: 'minLength',
363
+ min: 3
364
+ })('❤️123')).toEqual({
365
+ data: '❤️123',
366
+ errors: undefined
367
+ });
368
+ });
328
369
  it('supports number', () => {
329
370
  expect((0, _.createValidation)({
330
371
  type: 'number',
@@ -521,6 +562,12 @@ describe('createValidation', () => {
521
562
  data: undefined,
522
563
  errors: ['Invalid email format']
523
564
  });
565
+ expect((0, _.createValidation)({
566
+ type: 'email'
567
+ })('email.@mail.com')).toEqual({
568
+ data: undefined,
569
+ errors: ['Invalid email format']
570
+ });
524
571
  });
525
572
  it('supports alphanumeric', () => {
526
573
  expect((0, _.createValidation)({
package/lib/validators.js CHANGED
@@ -47,11 +47,16 @@ const equals = ({
47
47
 
48
48
  exports.equals = equals;
49
49
 
50
+ function getStringLength(str) {
51
+ const splitGraphemes = str.split(/[\ufe00-\ufe0f]/).join('').match(/./gu);
52
+ return splitGraphemes ? splitGraphemes.length : 0;
53
+ }
54
+
50
55
  const maxLength = ({
51
56
  max,
52
57
  getMessage
53
58
  }) => value => {
54
- return value && value.length > max ? {
59
+ return value && getStringLength(value) > max ? {
55
60
  errors: (0, _messageProcessor.default)(getMessage, `##maxLength`, string => string.replace(/{max}/g, max), max)
56
61
  } : {
57
62
  data: value
@@ -63,7 +68,7 @@ exports.maxLength = maxLength;
63
68
  const minLength = ({
64
69
  min,
65
70
  getMessage
66
- }) => value => value && value.length < min ? {
71
+ }) => value => value && getStringLength(value) < min ? {
67
72
  errors: (0, _messageProcessor.default)(getMessage, `##minLength`, string => string.replace(/{min}/g, min), min)
68
73
  } : {
69
74
  data: value
@@ -102,10 +107,11 @@ const maxValue = ({
102
107
  };
103
108
 
104
109
  exports.maxValue = maxValue;
110
+ const emailRegex = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
105
111
 
106
112
  const email = ({
107
113
  getMessage
108
- }) => value => value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) ? {
114
+ }) => value => value && !emailRegex.test(value) ? {
109
115
  errors: (0, _messageProcessor.default)(getMessage, '##email')
110
116
  } : {
111
117
  data: value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co.validation",
3
- "version": "2.2.1",
3
+ "version": "2.3.2",
4
4
  "description": "JSON based fluent validation for browser, node.js, redux-form and anything javascript compatible.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {