co.validation 1.0.0-RC.6 → 2.1.0

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.
@@ -71,6 +71,20 @@ describe('createValidation', () => {
71
71
  }]);
72
72
  expect(validationResult.data).toEqual([]);
73
73
  });
74
+ it('list of numbers stays list of numbers', () => {
75
+ const validatedObject = [0, 1, 2];
76
+ const validationResult = (0, _.createValidation)({
77
+ type: 'array',
78
+ itemRule: {
79
+ type: 'all',
80
+ validators: [{
81
+ type: 'number'
82
+ }]
83
+ }
84
+ })(validatedObject);
85
+ expect(validationResult.errors).toEqual(undefined);
86
+ expect(validationResult.data).toEqual(validatedObject);
87
+ });
74
88
  it('single valid item stays in data', () => {
75
89
  const validatedObject = [{
76
90
  login: 'hey'
@@ -39,6 +39,45 @@ describe('createValidation', () => {
39
39
  errors: undefined
40
40
  });
41
41
  });
42
+ it('supports custom', () => {
43
+ const biggerThanX = (value, allValues) => {
44
+ if (value < allValues.x) {
45
+ throw 'bigger';
46
+ }
47
+
48
+ return value;
49
+ };
50
+
51
+ expect((0, _.createValidation)({
52
+ type: 'custom',
53
+ validator: biggerThanX
54
+ })(5, {
55
+ x: 10
56
+ })).toEqual({
57
+ data: undefined,
58
+ errors: ['Error']
59
+ });
60
+ expect((0, _.createValidation)({
61
+ type: 'custom',
62
+ validator: biggerThanX,
63
+ getMessage: bigger => bigger
64
+ })(5, {
65
+ x: 10
66
+ })).toEqual({
67
+ data: undefined,
68
+ errors: ['bigger']
69
+ });
70
+ expect((0, _.createValidation)({
71
+ type: 'custom',
72
+ validator: biggerThanX,
73
+ getMessage: () => 'Required'
74
+ })(50, {
75
+ x: 10
76
+ })).toEqual({
77
+ data: 50,
78
+ errors: undefined
79
+ });
80
+ });
42
81
  it('supports equals', () => {
43
82
  expect((0, _.createValidation)({
44
83
  type: 'equals',
@@ -370,21 +409,21 @@ describe('createValidation', () => {
370
409
  type: 'boolean',
371
410
  getMessage: () => 'Boolean'
372
411
  })('true')).toEqual({
373
- data: 'true',
412
+ data: true,
374
413
  errors: undefined
375
414
  });
376
415
  expect((0, _.createValidation)({
377
416
  type: 'boolean',
378
417
  getMessage: () => 'Boolean'
379
418
  })('false')).toEqual({
380
- data: 'false',
419
+ data: false,
381
420
  errors: undefined
382
421
  });
383
422
  expect((0, _.createValidation)({
384
423
  type: 'boolean',
385
424
  getMessage: () => 'Boolean'
386
425
  })('FAlse')).toEqual({
387
- data: 'FAlse',
426
+ data: false,
388
427
  errors: undefined
389
428
  });
390
429
  expect((0, _.createValidation)({
@@ -20,6 +20,9 @@ const createValidation = rule => {
20
20
  case 'required':
21
21
  return (0, _validators.required)(rule);
22
22
 
23
+ case 'custom':
24
+ return (0, _validators.custom)(rule);
25
+
23
26
  case 'equals':
24
27
  return (0, _validators.equals)(rule);
25
28
 
@@ -114,7 +117,7 @@ const createArrayValidation = rule => {
114
117
  resultErrors.push(null);
115
118
  }
116
119
 
117
- if (validationResult.data) {
120
+ if (validationResult.data !== null && validationResult.data !== undefined) {
118
121
  resultData.push(validationResult.data);
119
122
  }
120
123
  });
package/lib/validators.js CHANGED
@@ -3,12 +3,29 @@
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 = 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.required = exports.custom = void 0;
7
7
 
8
8
  var _messageProcessor = _interopRequireDefault(require("./messageProcessor"));
9
9
 
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
12
+ const custom = ({
13
+ validator,
14
+ getMessage
15
+ }) => (value, allValues) => {
16
+ try {
17
+ return {
18
+ data: validator(value, allValues)
19
+ };
20
+ } catch (e) {
21
+ return {
22
+ errors: (0, _messageProcessor.default)(getMessage, 'Error', null, e)
23
+ };
24
+ }
25
+ };
26
+
27
+ exports.custom = custom;
28
+
12
29
  const required = ({
13
30
  getMessage
14
31
  }) => value => value || typeof value === 'number' ? {
@@ -118,11 +135,18 @@ exports.string = string;
118
135
 
119
136
  const boolean = ({
120
137
  getMessage
121
- }) => value => value === undefined || value === null || typeof value === 'boolean' || typeof value === 'string' && {
138
+ }) => value => value === undefined || value === null ? {
139
+ data: value
140
+ } : typeof value === 'boolean' ? {
141
+ data: value
142
+ } : typeof value === 'string' && {
122
143
  'true': true,
123
144
  'false': true
124
145
  }[value.toLowerCase()] ? {
125
- data: value
146
+ data: {
147
+ 'true': true,
148
+ 'false': false
149
+ }[value.toLowerCase()]
126
150
  } : {
127
151
  errors: (0, _messageProcessor.default)(getMessage, 'Must be a boolean')
128
152
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co.validation",
3
- "version": "1.0.0-RC.6",
3
+ "version": "2.1.0",
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": {
@@ -11,7 +11,7 @@
11
11
  "keywords": [
12
12
  "validation"
13
13
  ],
14
- "author": "collaboracia",
14
+ "author": "Collaboracia OÜ",
15
15
  "license": "ISC",
16
16
  "devDependencies": {
17
17
  "@babel/cli": "^7.4.4",