co.validation 2.2.2 → 2.3.3

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',
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co.validation",
3
- "version": "2.2.2",
3
+ "version": "2.3.3",
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": {