co.validation 2.3.2 → 2.4.1

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.
@@ -51,6 +51,26 @@ describe('createValidation', () => {
51
51
  errors: ['test']
52
52
  });
53
53
  });
54
+ it('check that array has some item', () => {
55
+ expect((0, _.createValidation)({
56
+ type: 'requiredArrayWithItem'
57
+ })(undefined)).toEqual({
58
+ data: undefined,
59
+ errors: ['Must be an array']
60
+ });
61
+ expect((0, _.createValidation)({
62
+ type: 'requiredArrayWithItem'
63
+ })([1])).toEqual({
64
+ data: [1],
65
+ errors: undefined
66
+ });
67
+ expect((0, _.createValidation)({
68
+ type: 'requiredArrayWithItem'
69
+ })([])).toEqual({
70
+ data: undefined,
71
+ errors: ['Required']
72
+ });
73
+ });
54
74
  it('single invalid item creates error', () => {
55
75
  const validatedObject = [{
56
76
  login: ''
@@ -265,7 +265,7 @@ describe('createValidation', () => {
265
265
  max: 3
266
266
  })('1234')).toEqual({
267
267
  data: undefined,
268
- errors: ['Must be 3 characters or less']
268
+ errors: ['Maximum number of characters - 3']
269
269
  });
270
270
  expect((0, _.createValidation)({
271
271
  type: 'maxLength',
@@ -306,7 +306,7 @@ describe('createValidation', () => {
306
306
  max: 3
307
307
  })('❤️123')).toEqual({
308
308
  data: undefined,
309
- errors: ['Must be 3 characters or less']
309
+ errors: ['Maximum number of characters - 3']
310
310
  });
311
311
  });
312
312
  it('supports minLength', () => {
@@ -339,7 +339,7 @@ describe('createValidation', () => {
339
339
  min: 3
340
340
  })('12')).toEqual({
341
341
  data: undefined,
342
- errors: ['Must be at least 3 characters']
342
+ errors: ['Minimum number of characters - 3']
343
343
  });
344
344
  });
345
345
  it('supports minLength with emoji', () => {
@@ -356,7 +356,7 @@ describe('createValidation', () => {
356
356
  min: 3
357
357
  })('❤️1')).toEqual({
358
358
  data: undefined,
359
- errors: ['Must be at least 3 characters']
359
+ errors: ['Minimum number of characters - 3']
360
360
  });
361
361
  expect((0, _.createValidation)({
362
362
  type: 'minLength',
@@ -667,7 +667,7 @@ describe('createValidation', () => {
667
667
  getMessage: () => 'Email'
668
668
  }]
669
669
  })('123')).toEqual({
670
- data: '123',
670
+ data: 123,
671
671
  errors: undefined
672
672
  });
673
673
  expect((0, _.createValidation)({
@@ -45,6 +45,15 @@ const registrationFormValidation = (0, _.createValidation)({
45
45
  type: 'number'
46
46
  }]
47
47
  },
48
+ numberOfDiplomas: {
49
+ type: 'oneOf',
50
+ validators: [{
51
+ type: 'equals',
52
+ to: undefined
53
+ }, {
54
+ type: 'number'
55
+ }]
56
+ },
48
57
  hobbies: {
49
58
  type: 'array',
50
59
  itemRule: {
@@ -102,6 +111,30 @@ const ukErrors = {
102
111
  '##custom': 'Помилка'
103
112
  };
104
113
  describe('registrationFormValidation in language=' + encodeURI, () => {
114
+ it('success of full validation', () => {
115
+ const validatedObject = {
116
+ firstName: 'John',
117
+ lastName: 'Doe',
118
+ email: 'john.doe@example.com',
119
+ age: 25,
120
+ numberOfDiplomas: '2',
121
+ hobbies: [{
122
+ title: 'Gardening',
123
+ description: 'I love spending time in my garden, growing flowers and vegetables.',
124
+ toys: ['Shovel', 'Watering can']
125
+ }, {
126
+ title: 'Cooking',
127
+ description: 'Cooking is my passion. I enjoy experimenting with new recipes.',
128
+ toys: ['Cookware', 'Cutting board']
129
+ }]
130
+ };
131
+ ;
132
+ const validationResult = registrationFormValidation(validatedObject);
133
+ expect(validationResult.errors).toEqual(undefined);
134
+ expect(validationResult.data).toEqual({ ...validatedObject,
135
+ numberOfDiplomas: 2
136
+ });
137
+ });
105
138
  ['en', 'uk', 'en'].map(lang => {
106
139
  it('single invalid item creates error without array in lang' + lang, () => {
107
140
  if (lang === 'en') {
@@ -143,7 +143,7 @@ describe('createValueValidation', () => {
143
143
  expect((0, _.createValueValidation)({
144
144
  type: 'maxLength',
145
145
  max: 3
146
- })('1234')).toEqual(['Must be 3 characters or less']);
146
+ })('1234')).toEqual(['Maximum number of characters - 3']);
147
147
  expect((0, _.createValueValidation)({
148
148
  type: 'maxLength',
149
149
  max: 3,
@@ -179,7 +179,7 @@ describe('createValueValidation', () => {
179
179
  expect((0, _.createValueValidation)({
180
180
  type: 'minLength',
181
181
  min: 3
182
- })('12')).toEqual(['Must be at least 3 characters']);
182
+ })('12')).toEqual(['Minimum number of characters - 3']);
183
183
  });
184
184
  it('supports number', () => {
185
185
  expect((0, _.createValueValidation)({
@@ -20,6 +20,9 @@ const createValidation = rule => {
20
20
  case 'required':
21
21
  return (0, _validators.required)(rule);
22
22
 
23
+ case 'requiredArrayWithItem':
24
+ return (0, _validators.requiredArrayWithItem)(rule);
25
+
23
26
  case 'custom':
24
27
  return (0, _validators.custom)(rule);
25
28
 
@@ -8,12 +8,12 @@ const defaultDefaultMessages = {
8
8
  '##object': 'Must be an object',
9
9
  '##array': 'Must be an array',
10
10
  '##required': 'Required',
11
- '##equals': `Must be equal to {to}`,
12
- '##maxLength': `Must be {max} characters or less`,
13
- '##minLength': `Must be at least {min} characters`,
11
+ '##equals': 'Must be equal to {to}',
12
+ '##maxLength': 'Maximum number of characters - {max}',
13
+ '##minLength': 'Minimum number of characters - {min}',
14
14
  '##number': 'Must be a number',
15
- '##minValue': `Must be at least {min}`,
16
- '##maxValue': `Must be not more than {max}`,
15
+ '##minValue': 'Must be at least {min}',
16
+ '##maxValue': 'Must be not more than {max}',
17
17
  '##email': 'Invalid email format',
18
18
  '##alphanumeric': 'Only alphanumeric characters',
19
19
  '##string': 'Must be a string',
package/lib/operators.js CHANGED
@@ -11,20 +11,21 @@ const oneOf = (...validators) => {
11
11
  }
12
12
 
13
13
  return (value, allValues) => {
14
- const validationErrors = validators.reduce((validationErrors, validator) => {
15
- if (validationErrors === undefined) {
16
- return undefined;
17
- }
14
+ const validationErrors = [];
18
15
 
19
- const currentResult = validator(value, allValues);
16
+ for (const validator of validators) {
17
+ let currentResult = validator(value, allValues);
20
18
 
21
19
  if (currentResult.errors) {
22
- if (Array.isArray(currentResult.errors)) return [...validationErrors, ...currentResult.errors];
23
- return [...validationErrors, currentResult.errors];
20
+ if (Array.isArray(currentResult.errors)) {
21
+ validationErrors.push(...currentResult.errors);
22
+ } else {
23
+ validationErrors.push(currentResult.errors);
24
+ }
24
25
  } else {
25
- return undefined;
26
+ return currentResult;
26
27
  }
27
- }, []);
28
+ }
28
29
 
29
30
  if (validationErrors && validationErrors.length > 0) {
30
31
  return {
package/lib/validators.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.regex = exports.equalsToField = exports.boolean = exports.string = exports.alphanumeric = exports.email = exports.maxValue = exports.minValue = exports.number = exports.minLength = exports.maxLength = exports.equals = exports.required = exports.custom = void 0;
6
+ exports.regex = exports.equalsToField = exports.boolean = exports.string = exports.alphanumeric = exports.email = exports.maxValue = exports.minValue = exports.number = exports.minLength = exports.maxLength = exports.equals = exports.requiredArrayWithItem = exports.required = exports.custom = void 0;
7
7
 
8
8
  var _messageProcessor = _interopRequireDefault(require("./messageProcessor"));
9
9
 
@@ -36,6 +36,24 @@ const required = ({
36
36
 
37
37
  exports.required = required;
38
38
 
39
+ const requiredArrayWithItem = ({
40
+ getMessage
41
+ }) => value => {
42
+ if (Array.isArray(value)) {
43
+ return value.length > 0 ? {
44
+ data: value
45
+ } : {
46
+ errors: (0, _messageProcessor.default)(getMessage, '##required')
47
+ };
48
+ }
49
+
50
+ return {
51
+ errors: (0, _messageProcessor.default)(getMessage, `##array`)
52
+ };
53
+ };
54
+
55
+ exports.requiredArrayWithItem = requiredArrayWithItem;
56
+
39
57
  const equals = ({
40
58
  to,
41
59
  getMessage
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co.validation",
3
- "version": "2.3.2",
3
+ "version": "2.4.1",
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": {