8000 fix: middleware.validate throwing uncaught when data is undefined · maritz/nohm@819865b · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 819865b

Browse files
committed
fix: middleware.validate throwing uncaught when data is undefined
1 parent 35aeef1 commit 819865b

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

docs/api/ts_universalValidators.js.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,13 @@ <h1 class="page-title">ts/universalValidators.js</h1>
225225
if (options.optional &amp;&amp; !value) {
226226
return cb(key, true);
227227
}
228-
func(value, options).then((result) => {
229-
cb(key, result, name);
230-
});
228+
try {
229+
func(value, options).then((result) => {
230+
cb(key, result, name);
231+
});
232+
} catch (e) {
233+
cb(key, false, name);
234+
}
231235
});
232236
};
233237

test/middlewareTests.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,28 @@ exports.middleware = {
263263
t.done();
264264
});
265265
},
266+
267+
middlewareValidateUndefined: function(t) {
268+
setup(t, 2, undefined, async (sandbox) => {
269+
var val = sandbox.nohmValidations.validate;
270+
try {
271+
const result = await val('UserMiddlewareMockup', {
272+
name: undefined,
273+
});
274+
t.same(
275+
result.result,
276+
false,
277+
'Validating with name undefined succeeded',
278+
);
279+
t.same(
280+
result.errors,
281+
{ name: ['length', 'notEmpty'] },
282+
'Validating with name undefined had wrong errors object.',
283+
);
284+
} catch (e) {
285+
t.ok(false, 'Validate threw an error on undefined data.');
286+
}
287+
t.done();
288+
});
289+
},
266290
};

ts/universalValidators.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@
186186
if (options.optional && !value) {
187187
return cb(key, true);
188188
}
189-
func(value, options).then((result) => {
190-
cb(key, result, name);
191-
});
189+
try {
190+
func(value, options).then((result) => {
191+
cb(key, result, name);
192+
});
193+
} catch (e) {
194+
cb(key, false, name);
195+
}
192196
});
193197
};
194198

0 commit comments

Comments
 (0)
0