File size: 6,122 Bytes
ca30ad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
diff --git a/test/document.validateAllPaths.test.js b/test/document.validateAllPaths.test.js
new file mode 100644
index 000000000..d6445486a
--- /dev/null
+++ b/test/document.validateAllPaths.test.js
@@ -0,0 +1,133 @@
+'use strict';
+
+const start = require('./common');
+
+const assert = require('assert');
+
+const mongoose = start.mongoose;
+const Schema = mongoose.Schema;
+
+describe('document validateAllPaths option', function() {
+  let modelCounter = 0;
+  function makeModel(definition, schemaOptions) {
+    const schema = new Schema(definition, schemaOptions);
+    return mongoose.model('validateAllPaths_' + (++modelCounter), schema);
+  }
+
+  describe('with schema-level validateModifiedOnly', function() {
+    let Test;
+    function makeInvalidDoc() {
+      return Test.hydrate({ name: '', age: -1, tags: ['z'], okField: 'fine' });
+    }
+
+    beforeEach(function() {
+      Test = makeModel({
+        name: { type: String, required: true },
+        age: { type: Number, min: [0, 'too small'] },
+        tags: [{ type: String, enum: ['a', 'b'] }],
+        okField: { type: String }
+      }, { validateModifiedOnly: true });
+    });
+
+    it('validateSync does not report unmodified paths by default', function() {
+      const doc = makeInvalidDoc();
+      assert.strictEqual(doc.modifiedPaths().length, 0);
+      assert.strictEqual(doc.validateSync(), undefined);
+    });
+
+    it('validateSync({ validateAllPaths: true }) validates every path', function() {
+      const doc = makeInvalidDoc();
+      const err = doc.validateSync({ validateAllPaths: true });
+      assert.ok(err);
+      assert.equal(err.name, 'ValidationError');
+      assert.ok(err.errors['name']);
+      assert.ok(err.errors['age']);
+      assert.ok(err.errors['tags.0']);
+      assert.ok(!err.errors['okField']);
+    });
+
+    it('validateSync({ validateAllPaths: true }) returns undefined when all paths are valid', function() {
+      const doc = Test.hydrate({ name: 'valid', age: 5, tags: ['a'], okField: 'fine' });
+      assert.strictEqual(doc.validateSync({ validateAllPaths: true }), undefined);
+    });
+
+    it('validate() does not report unmodified paths by default', async function() {
+      const doc = makeInvalidDoc();
+      await doc.validate();
+    });
+
+    it('validate({ validateAllPaths: true }) rejects with every failing path', async function() {
+      const doc = makeInvalidDoc();
+      const err = await doc.validate({ validateAllPaths: true }).then(() => null, e => e);
+      assert.ok(err);
+      assert.equal(err.name, 'ValidationError');
+      assert.ok(err.errors['name']);
+      assert.ok(err.errors['age']);
+      assert.ok(err.errors['tags.0']);
+      assert.ok(!err.errors['okField']);
+    });
+
+    it('validate({ validateAllPaths: true }) resolves when all paths are valid', async function() {
+      const doc = Test.hydrate({ name: 'valid', age: 5, tags: ['a'], okField: 'fine' });
+      await doc.validate({ validateAllPaths: true });
+    });
+  });
+
+  describe('error contracts', function() {
+    let Test;
+    beforeEach(function() {
+      Test = makeModel({
+        name: { type: String, required: true },
+        age: { type: Number }
+      });
+    });
+
+    it('validateSync throws when combining validateAllPaths and pathsToSkip', function() {
+      const doc = Test.hydrate({ name: 'x', age: 1 });
+      assert.throws(
+        () => doc.validateSync({ validateAllPaths: true, pathsToSkip: 'name' }),
+        err => err instanceof TypeError &&
+          err.message === 'Cannot set both `validateAllPaths` and `pathsToSkip`'
+      );
+    });
+
+    it('validateSync throws when combining validateAllPaths and pathsToValidate', function() {
+      const doc = Test.hydrate({ name: 'x', age: 1 });
+      assert.throws(
+        () => doc.validateSync(['name'], { validateAllPaths: true }),
+        err => err instanceof TypeError &&
+          err.message === 'Cannot set both `validateAllPaths` and `pathsToValidate`'
+      );
+    });
+
+    it('validateSync throws when combining validateAllPaths and validateModifiedOnly', function() {
+      const doc = Test.hydrate({ name: 'x', age: 1 });
+      assert.throws(
+        () => doc.validateSync({ validateAllPaths: true, validateModifiedOnly: true }),
+        err => err instanceof TypeError &&
+          err.message === 'Cannot set both `validateAllPaths` and `validateModifiedOnly`'
+      );
+    });
+
+    it('validate rejects when combining validateAllPaths and pathsToSkip', async function() {
+      const doc = Test.hydrate({ name: 'x', age: 1 });
+      const err = await doc.validate({ validateAllPaths: true, pathsToSkip: 'name' }).then(() => null, e => e);
+      assert.ok(err instanceof TypeError);
+      assert.equal(err.message, 'Cannot set both `validateAllPaths` and `pathsToSkip`');
+    });
+
+    it('validate rejects when combining validateAllPaths and pathsToValidate', async function() {
+      const doc = Test.hydrate({ name: 'x', age: 1 });
+      const err = await doc.validate(['name'], { validateAllPaths: true }).then(() => null, e => e);
+      assert.ok(err instanceof TypeError);
+      assert.equal(err.message, 'Cannot set both `validateAllPaths` and `pathsToValidate`');
+    });
+
+    it('validate rejects when combining validateAllPaths and validateModifiedOnly', async function() {
+      const doc = Test.hydrate({ name: 'x', age: 1 });
+      const err = await doc.validate({ validateAllPaths: true, validateModifiedOnly: true }).then(() => null, e => e);
+      assert.ok(err instanceof TypeError);
+      assert.equal(err.message, 'Cannot set both `validateAllPaths` and `validateModifiedOnly`');
+    });
+  });
+});
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+
+
+
+
+set -euo pipefail
+HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+cd /testbed
+
+
+
+
+
+
+
+export MONGOMS_VERSION=8.0.0
+
+
+npx mocha --exit ./test/document.test.js
+
+
+npx mocha --exit ./test/document.validateAllPaths.test.js