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